Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package example.platformer
- {
- import nape.callbacks.CbType;
- import nape.callbacks.InteractionCallback;
- import nape.callbacks.PreFlag;
- import nape.constraint.PivotJoint;
- import nape.dynamics.Arbiter;
- import nape.dynamics.ArbiterList;
- import nape.dynamics.CollisionArbiter;
- import nape.geom.Vec2;
- import nape.phys.Body;
- import nape.phys.BodyType;
- import nape.phys.Compound;
- import nape.shape.Circle;
- import nape.shape.Polygon;
- import nape.space.Space;
- /**
- * ...
- * @author Liu Wong
- *
- */
- public class physPlayer
- {
- private const radius:Number = 16;
- private const height:Number = radius * 2;
- private const jumpPower:Number = 60;
- private const movePower:Number = 10;
- private const dampingFactor:Number = 0.75;
- private const maxVelocity:Number = 250;
- private const maxJumpSteps:int = 40;
- private var firstBody:Body;
- private var secondBody:Body;
- private var joint:PivotJoint;
- private var jumpCounter:int = 0;
- private var gv:Vec2;//first vector for check poss. jump
- private var gv2:Vec2;//second vector for check poss. jump
- private var gv3:Vec2;
- private var gv4:Vec2;
- private var jumpVector:Vec2;
- private var sleepFirstTime:Boolean = false;
- private var compound:Compound;
- private var jumpTimeCounter:int = -1;
- private static var magicCounter:int = 0;
- public function physPlayer(space:Space, pos:Vec2, cbtype:CbType, cbtypeEx:CbType):void
- {
- compound = new Compound();
- firstBody = new Body(BodyType.DYNAMIC, pos);
- firstBody.shapes.add(new Circle(radius));
- firstBody.compound = compound;
- firstBody.cbTypes.add(cbtypeEx);
- secondBody = new Body(BodyType.DYNAMIC, new Vec2(pos.x, pos.y - height * 0.5));
- secondBody.shapes.add(new Polygon(Polygon.box(radius * 0.95 * 2, height)));
- secondBody.allowRotation = false;
- secondBody.compound = compound;
- joint = new PivotJoint(firstBody, secondBody, new Vec2(0, 0), secondBody.worldPointToLocal(firstBody.position));
- joint.ignore = true;
- joint.compound = compound;
- gv = Vec2.fromPolar(1, Math.PI / 3);
- gv2 = Vec2.fromPolar(1, 2 * Math.PI / 3);
- gv3 = gv.mul( -1);
- gv4 = gv2.mul( -1);
- jumpVector = new Vec2(0, -jumpPower);
- compound.cbTypes.add(cbtype);
- compound.space = space;
- }
- public function getCompound():Compound
- {
- return compound;
- }
- public function onCollision(event:InteractionCallback):void
- {
- var tmp:ArbiterList = event.arbiters;
- if (tmp == null)
- return;
- var k:int = tmp.length;
- if (k > 0)
- {
- var i:int;
- for (i = 0; i < k; i++)
- {
- var tmp2:Arbiter = tmp.at(i);
- if (tmp2.isCollisionArbiter() == false)
- continue;
- if (tmp2.state == PreFlag.IGNORE)
- continue;
- var tmp3:CollisionArbiter = tmp2.collisionArbiter;
- if (gv.dot(tmp3.normal) > 0)
- {
- if (gv2.dot(tmp3.normal) > 0)
- {
- jumpCounter++;
- return;
- }
- }
- else//check inverse normal, need for kinematic body
- {
- if (gv3.dot(tmp3.normal) > 0)
- if (gv4.dot(tmp3.normal) > 0)
- {
- jumpCounter++;
- return;
- }
- }
- }
- }
- }
- private function checkCollision():void
- {
- var tmp:ArbiterList = firstBody.arbiters;
- if (tmp == null)
- return;
- var k:int = tmp.length;
- if (k > 0)
- {
- var i:int;
- for (i = 0; i < k; i++)
- {
- var tmp2:Arbiter = tmp.at(i);
- if (tmp2.isCollisionArbiter() == false)
- continue;
- if (tmp2.state == PreFlag.IGNORE)
- continue;
- var tmp3:CollisionArbiter = tmp2.collisionArbiter;
- if (gv.dot(tmp3.normal) > 0)
- {
- if (gv2.dot(tmp3.normal) > 0)
- {
- jumpCounter++;
- return;
- }
- }
- else//check inverse normal, need for kinematic body
- {
- if (gv3.dot(tmp3.normal) > 0)
- if (gv4.dot(tmp3.normal) > 0)
- {
- jumpCounter++;
- return;
- }
- }
- }
- }
- }
- public function resetCounter():void
- {
- jumpCounter = 0;
- }
- public function left():void
- {
- var len:Number = firstBody.velocity.length;
- if (len <= maxVelocity)
- firstBody.applyImpulse(new Vec2( -movePower, 0));
- }
- public function right():void
- {
- var len:Number = firstBody.velocity.length;
- if (len <= maxVelocity)
- firstBody.applyImpulse(new Vec2(movePower, 0));
- }
- public function applyDamping():void
- {
- firstBody.angularVel *= dampingFactor;
- }
- public function jump():void
- {
- if (jumpTimeCounter >= 0)
- return;
- if (jumpCounter > 0)
- {
- sleepFirstTime = false;
- jumpTimeCounter = 0;
- }
- else
- if (firstBody.isSleeping)
- {
- if (sleepFirstTime == false)
- {
- sleepFirstTime = true;
- checkCollision();
- }
- if (jumpCounter > 0)
- jumpTimeCounter = 0;
- }
- }
- public function resetJump():void
- {
- jumpTimeCounter = -1;
- }
- public function update():void
- {
- magicCounter++;
- if (jumpTimeCounter >= 0)
- {
- jumpTimeCounter++;
- if (jumpTimeCounter >= maxJumpSteps)
- {
- jumpTimeCounter = -1;
- return;
- }
- var jp:Number = (maxJumpSteps - jumpTimeCounter) / maxJumpSteps;
- firstBody.applyImpulse(jumpVector.mul(jp));
- }
- }
- public function free():void
- {
- compound.space = null;
- compound = null;
- joint = null;
- firstBody = null;
- secondBody = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement