home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Diversos / Beez.swf / scripts / core / objects / PlayerBall.as < prev    next >
Encoding:
Text File  |  2008-09-03  |  6.8 KB  |  203 lines

  1. package core.objects
  2. {
  3.    import Box2D.Collision.Shapes.b2Shape;
  4.    import Box2D.Collision.Shapes.b2ShapeDef;
  5.    import Box2D.Collision.b2ContactPoint;
  6.    import Box2D.Common.Math.b2Mat22;
  7.    import Box2D.Common.Math.b2Math;
  8.    import Box2D.Common.Math.b2Vec2;
  9.    import Box2D.Dynamics.b2Body;
  10.    import core.BContactListener;
  11.    import core.Input;
  12.    import core.events.CoreEvent;
  13.    import flash.display.Sprite;
  14.    import flash.events.KeyboardEvent;
  15.    
  16.    public class PlayerBall extends Ball
  17.    {
  18.        
  19.       
  20.       public var sensorShape:b2Shape;
  21.       
  22.       private var input:Input;
  23.       
  24.       private var skinJump:Sprite;
  25.       
  26.       private var skinInner:Sprite;
  27.       
  28.       public var mainShape:b2Shape;
  29.       
  30.       private var onPlatform:Boolean;
  31.       
  32.       private var maxJumpPower:uint = 40;
  33.       
  34.       private var jumpPower:int;
  35.       
  36.       public function PlayerBall(def:BallDef)
  37.       {
  38.          super(def);
  39.       }
  40.       
  41.       override protected function createShapeDef() : b2ShapeDef
  42.       {
  43.          return super.createShapeDef();
  44.       }
  45.       
  46.       override protected function updateHandler(e:CoreEvent) : void
  47.       {
  48.          super.updateHandler(e);
  49.          updateJump();
  50.       }
  51.       
  52.       private function collideRemoveHandler(e:CoreEvent) : void
  53.       {
  54.          var otherBody:b2Body = null;
  55.          var thisShape:b2Shape = null;
  56.          var point:b2ContactPoint = e.data as b2ContactPoint;
  57.          var body1:b2Body = point.shape1.m_body;
  58.          var body2:b2Body = point.shape2.m_body;
  59.          if(body1.m_userData == this || body2.m_userData == this)
  60.          {
  61.             otherBody = body1.m_userData == this ? body2 : body1;
  62.             thisShape = body1.m_userData == this ? point.shape1 : point.shape2;
  63.             if(thisShape == sensorShape && otherBody.m_userData is Platform)
  64.             {
  65.                onPlatform = false;
  66.             }
  67.          }
  68.       }
  69.       
  70.       override public function destroy(e:CoreEvent = null) : void
  71.       {
  72.          super.destroy(e);
  73.          engine.canvas.objectsContainer.stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
  74.          engine.canvas.objectsContainer.stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
  75.       }
  76.       
  77.       private function keyUpHandler(e:KeyboardEvent) : void
  78.       {
  79.          var platformAngle:Number = NaN;
  80.          var mat:b2Mat22 = null;
  81.          var upImpulse:b2Vec2 = null;
  82.          var impulse:b2Vec2 = null;
  83.          if(e.keyCode == Input.KEY_DOWN)
  84.          {
  85.             if(onPlatform)
  86.             {
  87.                platformAngle = engine.platform.getBody().GetAngle();
  88.                mat = new b2Mat22(platformAngle);
  89.                upImpulse = new b2Vec2(0,-2 * jumpPower);
  90.                impulse = b2Math.b2MulMV(mat,upImpulse);
  91.                body.ApplyImpulse(impulse,body.GetWorldCenter());
  92.             }
  93.             skinJump.visible = false;
  94.             jumpPower = 0;
  95.          }
  96.       }
  97.       
  98.       protected function collectBonus(bonus:BonusBall) : void
  99.       {
  100.          bonus.bonusCollect();
  101.       }
  102.       
  103.       override protected function createSkin() : Sprite
  104.       {
  105.          var SkinClass:Class = engine.assets.getAssetClass("PlayerBallAsset");
  106.          var skin:Sprite = new SkinClass();
  107.          skin.width = skin.height = ballDef.r * 2;
  108.          skinInner = skin.getChildByName("inner") as Sprite;
  109.          skinJump = skinInner.getChildByName("jump") as Sprite;
  110.          engine.canvas.playerContainer.addChild(skin);
  111.          return skin;
  112.       }
  113.       
  114.       private function createSensor() : void
  115.       {
  116.          var shapeDef:b2ShapeDef = createShapeDef();
  117.          shapeDef.density = 1;
  118.          shapeDef.isSensor = true;
  119.          shapeDef.filter.categoryBits = 4;
  120.          shapeDef.filter.maskBits = 65535;
  121.          sensorShape = body.CreateShape(shapeDef);
  122.       }
  123.       
  124.       private function updateJump() : void
  125.       {
  126.          if(jumpPower > 0)
  127.          {
  128.             if(jumpPower < maxJumpPower)
  129.             {
  130.                ++jumpPower;
  131.             }
  132.             skinJump.scaleX = jumpPower / maxJumpPower;
  133.          }
  134.       }
  135.       
  136.       override protected function init() : void
  137.       {
  138.          super.init();
  139.          mainShape = body.m_shapeList;
  140.          createSensor();
  141.          input = Input.getInstance();
  142.          jumpPower = 0;
  143.          skinJump.visible = false;
  144.          onPlatform = false;
  145.          engine.canvas.objectsContainer.stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
  146.          engine.canvas.objectsContainer.stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
  147.          engine.contactListener.dispatcher.addEventListener(BContactListener.COLLIDE_ADD,collideHandler);
  148.          engine.contactListener.dispatcher.addEventListener(BContactListener.COLLIDE_REMOVE,collideRemoveHandler);
  149.       }
  150.       
  151.       private function keyDownHandler(e:KeyboardEvent) : void
  152.       {
  153.          if(e.keyCode == Input.KEY_DOWN && jumpPower == 0)
  154.          {
  155.             jumpPower = 1;
  156.             skinJump.visible = true;
  157.          }
  158.       }
  159.       
  160.       protected function collideHandler(e:CoreEvent) : void
  161.       {
  162.          var otherBody:b2Body = null;
  163.          var thisShape:b2Shape = null;
  164.          var point:b2ContactPoint = e.data as b2ContactPoint;
  165.          var body1:b2Body = point.shape1.m_body;
  166.          var body2:b2Body = point.shape2.m_body;
  167.          if(body1.m_userData == this || body2.m_userData == this)
  168.          {
  169.             otherBody = body1.m_userData == this ? body2 : body1;
  170.             thisShape = body1.m_userData == this ? point.shape1 : point.shape2;
  171.             if(thisShape == sensorShape && otherBody.m_userData is BonusBall)
  172.             {
  173.                collectBonus(otherBody.m_userData as BonusBall);
  174.             }
  175.             else if(thisShape == sensorShape && otherBody.m_userData is Circle)
  176.             {
  177.                (otherBody.m_userData as Circle).collidePlayer();
  178.             }
  179.             else if(thisShape == sensorShape && otherBody.m_userData is Rect)
  180.             {
  181.                (otherBody.m_userData as Rect).collidePlayer();
  182.             }
  183.             else if(thisShape == sensorShape && otherBody.m_userData is Star)
  184.             {
  185.                (otherBody.m_userData as Star).collidePlayer();
  186.             }
  187.             else if(thisShape == sensorShape && otherBody.m_userData is Platform)
  188.             {
  189.                onPlatform = true;
  190.             }
  191.          }
  192.       }
  193.       
  194.       override public function updateSkin() : void
  195.       {
  196.          var pos:b2Vec2 = body.GetWorldCenter();
  197.          skin.x = pos.x * engine.m_physScale;
  198.          skin.y = pos.y * engine.m_physScale;
  199.          skinInner.rotation = body.GetAngle() * 180 / Math.PI;
  200.       }
  201.    }
  202. }
  203.