home *** CD-ROM | disk | FTP | other *** search
- package core.objects
- {
- import Box2D.Collision.Shapes.b2Shape;
- import Box2D.Collision.Shapes.b2ShapeDef;
- import Box2D.Collision.b2ContactPoint;
- import Box2D.Common.Math.b2Mat22;
- import Box2D.Common.Math.b2Math;
- import Box2D.Common.Math.b2Vec2;
- import Box2D.Dynamics.b2Body;
- import core.BContactListener;
- import core.Input;
- import core.events.CoreEvent;
- import flash.display.Sprite;
- import flash.events.KeyboardEvent;
-
- public class PlayerBall extends Ball
- {
-
-
- public var sensorShape:b2Shape;
-
- private var input:Input;
-
- private var skinJump:Sprite;
-
- private var skinInner:Sprite;
-
- public var mainShape:b2Shape;
-
- private var onPlatform:Boolean;
-
- private var maxJumpPower:uint = 40;
-
- private var jumpPower:int;
-
- public function PlayerBall(def:BallDef)
- {
- super(def);
- }
-
- override protected function createShapeDef() : b2ShapeDef
- {
- return super.createShapeDef();
- }
-
- override protected function updateHandler(e:CoreEvent) : void
- {
- super.updateHandler(e);
- updateJump();
- }
-
- private function collideRemoveHandler(e:CoreEvent) : void
- {
- var otherBody:b2Body = null;
- var thisShape:b2Shape = null;
- var point:b2ContactPoint = e.data as b2ContactPoint;
- var body1:b2Body = point.shape1.m_body;
- var body2:b2Body = point.shape2.m_body;
- if(body1.m_userData == this || body2.m_userData == this)
- {
- otherBody = body1.m_userData == this ? body2 : body1;
- thisShape = body1.m_userData == this ? point.shape1 : point.shape2;
- if(thisShape == sensorShape && otherBody.m_userData is Platform)
- {
- onPlatform = false;
- }
- }
- }
-
- override public function destroy(e:CoreEvent = null) : void
- {
- super.destroy(e);
- engine.canvas.objectsContainer.stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
- engine.canvas.objectsContainer.stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
- }
-
- private function keyUpHandler(e:KeyboardEvent) : void
- {
- var platformAngle:Number = NaN;
- var mat:b2Mat22 = null;
- var upImpulse:b2Vec2 = null;
- var impulse:b2Vec2 = null;
- if(e.keyCode == Input.KEY_DOWN)
- {
- if(onPlatform)
- {
- platformAngle = engine.platform.getBody().GetAngle();
- mat = new b2Mat22(platformAngle);
- upImpulse = new b2Vec2(0,-2 * jumpPower);
- impulse = b2Math.b2MulMV(mat,upImpulse);
- body.ApplyImpulse(impulse,body.GetWorldCenter());
- }
- skinJump.visible = false;
- jumpPower = 0;
- }
- }
-
- protected function collectBonus(bonus:BonusBall) : void
- {
- bonus.bonusCollect();
- }
-
- override protected function createSkin() : Sprite
- {
- var SkinClass:Class = engine.assets.getAssetClass("PlayerBallAsset");
- var skin:Sprite = new SkinClass();
- skin.width = skin.height = ballDef.r * 2;
- skinInner = skin.getChildByName("inner") as Sprite;
- skinJump = skinInner.getChildByName("jump") as Sprite;
- engine.canvas.playerContainer.addChild(skin);
- return skin;
- }
-
- private function createSensor() : void
- {
- var shapeDef:b2ShapeDef = createShapeDef();
- shapeDef.density = 1;
- shapeDef.isSensor = true;
- shapeDef.filter.categoryBits = 4;
- shapeDef.filter.maskBits = 65535;
- sensorShape = body.CreateShape(shapeDef);
- }
-
- private function updateJump() : void
- {
- if(jumpPower > 0)
- {
- if(jumpPower < maxJumpPower)
- {
- ++jumpPower;
- }
- skinJump.scaleX = jumpPower / maxJumpPower;
- }
- }
-
- override protected function init() : void
- {
- super.init();
- mainShape = body.m_shapeList;
- createSensor();
- input = Input.getInstance();
- jumpPower = 0;
- skinJump.visible = false;
- onPlatform = false;
- engine.canvas.objectsContainer.stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
- engine.canvas.objectsContainer.stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
- engine.contactListener.dispatcher.addEventListener(BContactListener.COLLIDE_ADD,collideHandler);
- engine.contactListener.dispatcher.addEventListener(BContactListener.COLLIDE_REMOVE,collideRemoveHandler);
- }
-
- private function keyDownHandler(e:KeyboardEvent) : void
- {
- if(e.keyCode == Input.KEY_DOWN && jumpPower == 0)
- {
- jumpPower = 1;
- skinJump.visible = true;
- }
- }
-
- protected function collideHandler(e:CoreEvent) : void
- {
- var otherBody:b2Body = null;
- var thisShape:b2Shape = null;
- var point:b2ContactPoint = e.data as b2ContactPoint;
- var body1:b2Body = point.shape1.m_body;
- var body2:b2Body = point.shape2.m_body;
- if(body1.m_userData == this || body2.m_userData == this)
- {
- otherBody = body1.m_userData == this ? body2 : body1;
- thisShape = body1.m_userData == this ? point.shape1 : point.shape2;
- if(thisShape == sensorShape && otherBody.m_userData is BonusBall)
- {
- collectBonus(otherBody.m_userData as BonusBall);
- }
- else if(thisShape == sensorShape && otherBody.m_userData is Circle)
- {
- (otherBody.m_userData as Circle).collidePlayer();
- }
- else if(thisShape == sensorShape && otherBody.m_userData is Rect)
- {
- (otherBody.m_userData as Rect).collidePlayer();
- }
- else if(thisShape == sensorShape && otherBody.m_userData is Star)
- {
- (otherBody.m_userData as Star).collidePlayer();
- }
- else if(thisShape == sensorShape && otherBody.m_userData is Platform)
- {
- onPlatform = true;
- }
- }
- }
-
- override public function updateSkin() : void
- {
- var pos:b2Vec2 = body.GetWorldCenter();
- skin.x = pos.x * engine.m_physScale;
- skin.y = pos.y * engine.m_physScale;
- skinInner.rotation = body.GetAngle() * 180 / Math.PI;
- }
- }
- }
-