Advertisement
Guest User

PlayerEntity - Code

a guest
Apr 6th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package  
  2. {
  3.     import net.flashpunk.*;
  4.     import net.flashpunk.FP;
  5.     import net.flashpunk.Entity;
  6.    
  7.     import net.flashpunk.graphics.Image;
  8.     import net.flashpunk.utils.Input;
  9.     import net.flashpunk.utils.Key;
  10.     import net.flashpunk.graphics.Spritemap;
  11.    
  12.     import flash.display.BitmapData;
  13.     import flash.geom.Point;
  14.    
  15.     /**
  16.      * ...
  17.      * @author JaydenB
  18.      */
  19.    
  20.     public class PlayerReference extends Entity
  21.     {
  22.         private var v:Point = new Point();
  23.         private var a:Point = new Point();
  24.        
  25.         public var speed:Number;
  26.         public var jumpvelocity:Number;
  27.         public var colour:uint;
  28.        
  29.         private var doubleJump:int = 0;
  30.         private var _gravity:Number;
  31.        
  32.         public function PlayerReference()
  33.         {
  34.             _gravity = GameWorld.gravity;
  35.             a.y = _gravity;
  36.            
  37.             type = "player";
  38.             setHitbox(16, 16);
  39.             graphic = new Image(new BitmapData(16, 16, false, colour));
  40.         }
  41.        
  42.         override public function update():void
  43.         {
  44.             var hInput:int;
  45.            
  46.             if (Input.check("left"))
  47.             {
  48.                 hInput -= 1;
  49.             }
  50.             if (Input.check("right"))
  51.             {
  52.                 hInput += 1;
  53.             }
  54.            
  55.             if (Input.pressed("jump"))
  56.             {
  57.                 jump();
  58.             }
  59.            
  60.            
  61.             v.x = hInput * speed;
  62.             v.y += a.y;
  63.            
  64.             updateCollision();
  65.            
  66.             super.update();
  67.         }
  68.        
  69.         private function updateCollision():void
  70.         {
  71.             x += v.x * FP.elapsed;
  72.             if (collide("level", x, y))
  73.             {
  74.                 if (FP.sign(v.x) > 0)
  75.                 {
  76.                     //hit left of block
  77.                     v.x = 0;
  78.                     x = Math.floor(x / GameWorld.tileSize) * GameWorld.tileSize;
  79.                 }
  80.                 else
  81.                 {
  82.                     //hit right of block
  83.                     v.x = 0;
  84.                     x = (Math.floor(x / GameWorld.tileSize) * GameWorld.tileSize) + GameWorld.tileSize;
  85.                 }
  86.             }
  87.            
  88.             y += v.y * FP.elapsed;
  89.             if (collide("level", x, y))
  90.             {
  91.                 if (FP.sign(v.y) > 0)
  92.                 {
  93.                     //land on top of block
  94.                     v.y = 0;
  95.                     y = Math.floor(y / GameWorld.tileSize) * GameWorld.tileSize;
  96.                     doubleJump = 0;
  97.                    
  98.                 }
  99.                 else
  100.                 {
  101.                     //hit bottom of block
  102.                     v.y = 0;
  103.                     y = (Math.floor(y / GameWorld.tileSize) * GameWorld.tileSize) + GameWorld.tileSize;
  104.                 }
  105.             }
  106.         }
  107.        
  108.         private function jump():void
  109.         {
  110.             if (doubleJump < 2)
  111.             {
  112.                 v.y = jumpvelocity;
  113.                 doubleJump++;
  114.             }
  115.         }
  116.     }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement