Advertisement
liuwong

nape example: platformer - player

Feb 25th, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package example.platformer
  2. {
  3.     import nape.callbacks.CbType;
  4.     import nape.callbacks.InteractionCallback;
  5.     import nape.callbacks.PreFlag;
  6.     import nape.constraint.PivotJoint;
  7.     import nape.dynamics.Arbiter;
  8.     import nape.dynamics.ArbiterList;
  9.     import nape.dynamics.CollisionArbiter;
  10.     import nape.geom.Vec2;
  11.     import nape.phys.Body;
  12.     import nape.phys.BodyType;
  13.     import nape.phys.Compound;
  14.     import nape.shape.Circle;
  15.     import nape.shape.Polygon;
  16.     import nape.space.Space;
  17.    
  18.     /**
  19.      * ...
  20.      * @author Liu Wong
  21.      *
  22.      */
  23.    
  24.     public class physPlayer
  25.     {
  26.         private const radius:Number = 16;
  27.         private const height:Number = radius * 2;
  28.        
  29.         private const jumpPower:Number = 60;
  30.         private const movePower:Number = 10;
  31.        
  32.         private const dampingFactor:Number = 0.75;
  33.        
  34.         private const maxVelocity:Number = 250;
  35.        
  36.         private const maxJumpSteps:int = 40;
  37.        
  38.         private var firstBody:Body;
  39.         private var secondBody:Body;
  40.        
  41.         private var joint:PivotJoint;
  42.        
  43.         private var jumpCounter:int = 0;
  44.        
  45.         private var gv:Vec2;//first vector for check poss. jump
  46.         private var gv2:Vec2;//second vector for check poss. jump
  47.         private var gv3:Vec2;
  48.         private var gv4:Vec2;
  49.        
  50.         private var jumpVector:Vec2;
  51.        
  52.         private var sleepFirstTime:Boolean = false;
  53.        
  54.         private var compound:Compound;
  55.        
  56.         private var jumpTimeCounter:int = -1;
  57.        
  58.         private static var magicCounter:int = 0;
  59.        
  60.         public function physPlayer(space:Space, pos:Vec2, cbtype:CbType, cbtypeEx:CbType):void
  61.         {
  62.             compound = new Compound();
  63.            
  64.             firstBody = new Body(BodyType.DYNAMIC, pos);
  65.             firstBody.shapes.add(new Circle(radius));
  66.             firstBody.compound = compound;
  67.             firstBody.cbTypes.add(cbtypeEx);
  68.            
  69.             secondBody = new Body(BodyType.DYNAMIC, new Vec2(pos.x, pos.y - height * 0.5));
  70.             secondBody.shapes.add(new Polygon(Polygon.box(radius * 0.95 * 2, height)));
  71.             secondBody.allowRotation = false;
  72.             secondBody.compound = compound;
  73.            
  74.             joint = new PivotJoint(firstBody, secondBody, new Vec2(0, 0), secondBody.worldPointToLocal(firstBody.position));
  75.             joint.ignore = true;
  76.             joint.compound = compound;
  77.            
  78.             gv = Vec2.fromPolar(1, Math.PI / 3);
  79.             gv2 = Vec2.fromPolar(1, 2 * Math.PI / 3);
  80.             gv3 = gv.mul( -1);
  81.             gv4 = gv2.mul( -1);
  82.            
  83.             jumpVector = new Vec2(0, -jumpPower);
  84.            
  85.             compound.cbTypes.add(cbtype);
  86.             compound.space = space;
  87.         }
  88.        
  89.         public function getCompound():Compound
  90.         {
  91.             return compound;
  92.         }
  93.        
  94.         public function onCollision(event:InteractionCallback):void
  95.         {  
  96.             var tmp:ArbiterList = event.arbiters;
  97.             if (tmp == null)
  98.                 return;
  99.                
  100.             var k:int = tmp.length;
  101.             if (k > 0)
  102.             {
  103.                 var i:int;
  104.                 for (i = 0; i < k; i++)
  105.                 {
  106.                     var tmp2:Arbiter = tmp.at(i);
  107.                     if (tmp2.isCollisionArbiter() == false)
  108.                         continue;
  109.                    
  110.                     if (tmp2.state == PreFlag.IGNORE)
  111.                         continue;
  112.                        
  113.                     var tmp3:CollisionArbiter = tmp2.collisionArbiter;
  114.                    
  115.                     if (gv.dot(tmp3.normal) > 0)
  116.                         {
  117.                             if (gv2.dot(tmp3.normal) > 0)
  118.                             {
  119.                                 jumpCounter++;
  120.                                
  121.                                 return;
  122.                             }
  123.                         }
  124.                     else//check inverse normal, need for kinematic body
  125.                     {
  126.                         if (gv3.dot(tmp3.normal) > 0)
  127.                             if (gv4.dot(tmp3.normal) > 0)
  128.                             {
  129.                                 jumpCounter++;
  130.                                
  131.                                 return;
  132.                             }
  133.                     }
  134.                 }
  135.             }
  136.         }
  137.        
  138.         private function checkCollision():void
  139.         {
  140.             var tmp:ArbiterList = firstBody.arbiters;
  141.             if (tmp == null)
  142.                 return;
  143.                
  144.             var k:int = tmp.length;
  145.             if (k > 0)
  146.             {
  147.                 var i:int;
  148.                 for (i = 0; i < k; i++)
  149.                 {
  150.                     var tmp2:Arbiter = tmp.at(i);
  151.                     if (tmp2.isCollisionArbiter() == false)
  152.                         continue;
  153.                        
  154.                     if (tmp2.state == PreFlag.IGNORE)
  155.                         continue;
  156.                    
  157.                     var tmp3:CollisionArbiter = tmp2.collisionArbiter;
  158.                    
  159.                     if (gv.dot(tmp3.normal) > 0)
  160.                         {
  161.                             if (gv2.dot(tmp3.normal) > 0)
  162.                             {
  163.                                 jumpCounter++;
  164.                                
  165.                                 return;
  166.                             }
  167.                         }
  168.                     else//check inverse normal, need for kinematic body
  169.                     {
  170.                         if (gv3.dot(tmp3.normal) > 0)
  171.                             if (gv4.dot(tmp3.normal) > 0)
  172.                             {
  173.                                 jumpCounter++;
  174.                                
  175.                                 return;
  176.                             }
  177.                     }
  178.                 }
  179.             }
  180.         }
  181.        
  182.         public function resetCounter():void
  183.         {
  184.             jumpCounter = 0;
  185.         }
  186.        
  187.         public function left():void
  188.         {
  189.             var len:Number = firstBody.velocity.length;
  190.            
  191.             if (len <= maxVelocity)
  192.                 firstBody.applyImpulse(new Vec2( -movePower, 0));
  193.         }
  194.        
  195.         public function right():void
  196.         {
  197.             var len:Number = firstBody.velocity.length;
  198.            
  199.             if (len <= maxVelocity)
  200.                 firstBody.applyImpulse(new Vec2(movePower, 0));
  201.         }
  202.        
  203.         public function applyDamping():void
  204.         {
  205.             firstBody.angularVel *= dampingFactor;
  206.         }
  207.        
  208.         public function jump():void
  209.         {
  210.             if (jumpTimeCounter >= 0)
  211.                 return;
  212.                
  213.             if (jumpCounter > 0)
  214.             {
  215.                 sleepFirstTime = false;
  216.                
  217.                 jumpTimeCounter = 0;
  218.             }
  219.             else
  220.                 if (firstBody.isSleeping)
  221.                 {
  222.                     if (sleepFirstTime == false)
  223.                     {
  224.                         sleepFirstTime = true;
  225.                        
  226.                         checkCollision();
  227.                     }
  228.                    
  229.                     if (jumpCounter > 0)
  230.                         jumpTimeCounter = 0;
  231.                 }
  232.         }
  233.        
  234.         public function resetJump():void
  235.         {
  236.             jumpTimeCounter = -1;
  237.         }
  238.        
  239.         public function update():void
  240.         {
  241.             magicCounter++;
  242.            
  243.             if (jumpTimeCounter >= 0)
  244.             {
  245.                 jumpTimeCounter++;
  246.                
  247.                 if (jumpTimeCounter >= maxJumpSteps)
  248.                 {
  249.                     jumpTimeCounter = -1;
  250.                    
  251.                     return;
  252.                 }
  253.                
  254.                 var jp:Number = (maxJumpSteps - jumpTimeCounter) / maxJumpSteps;
  255.                
  256.                 firstBody.applyImpulse(jumpVector.mul(jp));
  257.             }
  258.         }
  259.        
  260.         public function free():void
  261.         {
  262.             compound.space = null;
  263.             compound = null;
  264.            
  265.             joint = null;
  266.            
  267.             firstBody = null;
  268.            
  269.             secondBody = null;
  270.         }
  271.     }
  272.    
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement