Advertisement
YMM

FlashPunk Platformer Actor

YMM
Jun 1st, 2012
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.     import net.flashpunk.Entity;
  4.    
  5.     public class Actor extends Entity
  6.     {
  7.         private var hCounter:Number = 0;
  8.         private var vCounter:Number = 0;
  9.        
  10.         public function Actor(x:Number = 0, y:Number = 0)
  11.         {
  12.             super(x, y);
  13.             type = "Actor";
  14.         }
  15.        
  16.         public function moveExactH(amount:int, onCollide:Function = null):void
  17.         {
  18.             var sign:int = amount > 0 ? 1 : -1;
  19.            
  20.             while (amount != 0)
  21.             {
  22.                 if (collide("Solid", x + sign, y) != null)
  23.                 {
  24.                     if (onCollide != null)
  25.                         onCollide();
  26.                     return;
  27.                 }
  28.                
  29.                 x += sign;
  30.                 amount -= sign;
  31.             }
  32.         }
  33.        
  34.         public function moveExactV(amount:int, onCollide:Function = null):void
  35.         {
  36.             var sign:int = amount > 0 ? 1 : -1;
  37.            
  38.             while (amount != 0)
  39.             {
  40.                 if (collide("Solid", x, y + sign) != null)
  41.                 {
  42.                     if (onCollide != null)
  43.                         onCollide();
  44.                     return;
  45.                 }
  46.                
  47.                 y += sign;
  48.                 amount -= sign;
  49.             }
  50.         }
  51.        
  52.         public function moveH(amount:Number, onCollide:Function = null):void
  53.         {
  54.             hCounter += amount;
  55.             var move:int = Math.round(hCounter);
  56.            
  57.             if (move != 0)
  58.             {
  59.                 hCounter -= move;
  60.                 moveExactH(move, onCollide);
  61.             }
  62.         }
  63.        
  64.         public function moveV(amount:Number, onCollide:Function = null):void
  65.         {
  66.             vCounter += amount;
  67.             var move:int = Math.round(vCounter);
  68.            
  69.             if (move != 0)
  70.             {
  71.                 vCounter -= move;
  72.                 moveExactV(move, onCollide);
  73.             }
  74.         }
  75.        
  76.         public function snapH():void { hCounter = 0; }
  77.         public function snapV():void { vCounter = 0; }
  78.        
  79.         public function get actualX():Number { return x + hCounter; }
  80.         public function get actualY():Number { return y + vCounter; }
  81.        
  82.         public function set left(to:Number):void { x = to + originX; }
  83.         public function set right(to:Number):void { x = to + originX - width; }
  84.         public function set top(to:Number):void { y = to + originY; }
  85.         public function set bottom(to:Number):void { y = to + originY - height; }
  86.        
  87.         /*
  88.          * Called when a solid pushes this actor into another solid.
  89.          * This would be a good time to remove the actor from the world,
  90.          * because it will now be inside of a solid.
  91.          */
  92.         public function squish():void
  93.         {
  94.             //Override me!
  95.         }
  96.        
  97.         /*
  98.          * Called when an upward-moving solid "catches" the actor as it moves up.
  99.          * This is usually when you set the actor's vertical speed to zero
  100.          * if she is moving upward, to avoid jitter.
  101.          */
  102.         public function platformCatch():void
  103.         {
  104.             //Override me!
  105.         }
  106.     }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement