home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Diversos / Beez.swf / scripts / core / bonuses / BonusPushField.as < prev   
Encoding:
Text File  |  2008-09-03  |  5.3 KB  |  155 lines

  1. package core.bonuses
  2. {
  3.    import Box2D.Collision.Shapes.b2CircleDef;
  4.    import Box2D.Collision.Shapes.b2Shape;
  5.    import Box2D.Collision.b2ContactPoint;
  6.    import Box2D.Common.Math.b2Math;
  7.    import Box2D.Common.Math.b2Vec2;
  8.    import Box2D.Dynamics.b2Body;
  9.    import core.BContactListener;
  10.    import core.events.CoreEvent;
  11.    import core.objects.Circle;
  12.    import core.objects.PlayerBall;
  13.    import core.objects.Rect;
  14.    import flash.display.Graphics;
  15.    import flash.geom.Point;
  16.    
  17.    public class BonusPushField extends AbstractBonus
  18.    {
  19.        
  20.       
  21.       private var pushFieldBodies:Array;
  22.       
  23.       private var pushFieldShape:b2Shape;
  24.       
  25.       private var player:PlayerBall;
  26.       
  27.       private var pushFieldRadius:Number;
  28.       
  29.       public function BonusPushField()
  30.       {
  31.          super(true);
  32.       }
  33.       
  34.       override protected function deactivate() : void
  35.       {
  36.          player.getBody().DestroyShape(pushFieldShape);
  37.          pushFieldShape = null;
  38.          engine.contactListener.dispatcher.removeEventListener(BContactListener.COLLIDE_PERSIST,collidePersistHandler);
  39.          super.deactivate();
  40.       }
  41.       
  42.       override protected function init() : void
  43.       {
  44.          super.init();
  45.          lifeTime = 40 * 7;
  46.          player = engine.playerBall;
  47.          pushFieldRadius = 200;
  48.          pushFieldBodies = [];
  49.       }
  50.       
  51.       private function doActivatePushField() : void
  52.       {
  53.          var shapeDef:b2CircleDef = new b2CircleDef();
  54.          shapeDef.radius = pushFieldRadius / engine.m_physScale;
  55.          shapeDef.isSensor = true;
  56.          pushFieldShape = player.getBody().CreateShape(shapeDef);
  57.          engine.contactListener.dispatcher.addEventListener(BContactListener.COLLIDE_PERSIST,collidePersistHandler);
  58.       }
  59.       
  60.       override protected function initSounds() : void
  61.       {
  62.          var PushfieldBonusClass:Class = engine.assets.getAssetClass("SndPushfieldBonus");
  63.          sndActivate = new PushfieldBonusClass();
  64.       }
  65.       
  66.       private function updatePushField() : void
  67.       {
  68.          var otherBody:b2Body = null;
  69.          var sub:b2Vec2 = null;
  70.          var len:Number = NaN;
  71.          var impulse:b2Vec2 = null;
  72.          while(pushFieldBodies.length > 0)
  73.          {
  74.             otherBody = pushFieldBodies.pop();
  75.             sub = b2Math.SubtractVV(otherBody.GetWorldCenter(),player.getBody().GetWorldCenter());
  76.             len = sub.Normalize();
  77.             if(len * engine.m_physScale < pushFieldRadius)
  78.             {
  79.                impulse = sub.Copy();
  80.                impulse.Multiply(pushFieldRadius / engine.m_physScale - len);
  81.                otherBody.ApplyImpulse(impulse,otherBody.GetWorldCenter());
  82.                drawPushFieldLine(otherBody);
  83.             }
  84.          }
  85.       }
  86.       
  87.       override protected function updateHandler(e:CoreEvent) : void
  88.       {
  89.          if(!pushFieldShape)
  90.          {
  91.             doActivatePushField();
  92.          }
  93.          else
  94.          {
  95.             updatePushField();
  96.          }
  97.          super.updateHandler(e);
  98.       }
  99.       
  100.       override public function activate(activateTime:Number) : void
  101.       {
  102.          super.activate(activateTime);
  103.       }
  104.       
  105.       private function collidePersistHandler(e:CoreEvent) : void
  106.       {
  107.          var otherBody:b2Body = null;
  108.          var playerBody:b2Body = null;
  109.          var playerShape:b2Shape = null;
  110.          var point:b2ContactPoint = e.data as b2ContactPoint;
  111.          var body1:b2Body = point.shape1.m_body;
  112.          var body2:b2Body = point.shape2.m_body;
  113.          if(body1.m_userData == player || body2.m_userData == player)
  114.          {
  115.             otherBody = body1.m_userData == player ? body2 : body1;
  116.             playerBody = body1.m_userData == player ? body1 : body2;
  117.             playerShape = body1.m_userData == player ? point.shape1 : point.shape2;
  118.             if(playerShape == pushFieldShape && (otherBody.m_userData is Circle || otherBody.m_userData is Rect))
  119.             {
  120.                if(pushFieldBodies.indexOf(otherBody) == -1)
  121.                {
  122.                   pushFieldBodies.push(otherBody);
  123.                }
  124.             }
  125.          }
  126.       }
  127.       
  128.       private function drawPushFieldLine(otherBody:b2Body) : void
  129.       {
  130.          var dx:Number = NaN;
  131.          var dy:Number = NaN;
  132.          var pos1:b2Vec2 = player.getBody().GetPosition();
  133.          var pos2:b2Vec2 = otherBody.GetPosition();
  134.          var p1:Point = new Point(pos1.x * engine.m_physScale,pos1.y * engine.m_physScale);
  135.          var p2:Point = new Point(pos2.x * engine.m_physScale,pos2.y * engine.m_physScale);
  136.          var dist:Number = pushFieldRadius - Point.distance(p1,p2);
  137.          var lineThickness:Number = dist / pushFieldRadius * 10;
  138.          var lineAlpha:Number = dist / pushFieldRadius;
  139.          var gr:Graphics = engine.canvas.effectsContainer.graphics;
  140.          gr.lineStyle(lineThickness,16777215,lineAlpha);
  141.          gr.moveTo(p1.x,p1.y);
  142.          gr.lineTo(p2.x,p2.y);
  143.          gr.lineStyle(0,16777215,lineAlpha);
  144.          var bounces:uint = 2 + Math.round(Math.random() * 6);
  145.          for(var i:uint = 0; i < bounces; i++)
  146.          {
  147.             gr.moveTo(p2.x,p2.y);
  148.             dx = -10 + Math.random() * 20;
  149.             dy = -10 + Math.random() * 20;
  150.             gr.lineTo(p2.x + dx,p2.y + dy);
  151.          }
  152.       }
  153.    }
  154. }
  155.