Advertisement
Guest User

APE Player class

a guest
Aug 17th, 2010
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package source.Objects {
  2.     import flash.geom.Point;
  3.     import net.flashpunk.Entity;
  4.     import net.flashpunk.FP;
  5.     import net.flashpunk.graphics.Image;
  6.     import net.flashpunk.graphics.Spritemap;
  7.     import net.flashpunk.utils.Input;
  8.     import net.flashpunk.utils.Key;
  9.     import source.Global;
  10.  
  11.     public class Player extends Physics {
  12.  
  13.         [Embed(source='../../assets/graphics/player.png')]
  14.         public var imgPlayer:Class;
  15.         public var sprPlayer:Spritemap = new Spritemap(imgPlayer, 32, 32, animEnd);
  16.  
  17.         // normal gravity
  18.         public const NORMAL_GRAVITY:Number = 0.4;
  19.         public const SLOW_GRAVITY:Number = 0.2;
  20.  
  21.         //how fast we accelerate
  22.         public var movement:Number = 1;
  23.         public var jump:Number = 8;
  24.  
  25.         //current player direction (true = right, false = left)
  26.         public var direction:Boolean = true;
  27.  
  28.         //are we on the ground?
  29.         public var onground:Boolean = false;
  30.  
  31.         public var dead:Boolean = false;
  32.         public var start:Point;
  33.  
  34.         public var doubleJumpPw:Boolean = false;
  35.         public var doubleJump:Boolean = false;
  36.  
  37.         public var jumpFlag:Boolean = false;
  38.  
  39.         public var shootpw:Boolean = false;
  40.  
  41.         public var wallJumppPw:Boolean = false;
  42.         public var wallJump:int = 0;
  43.  
  44.         public const tipo:String = "Player";
  45.  
  46.         public function Player(x:int, y:int){
  47.             //set position
  48.             super(x, y);
  49.             start = new Point(x, y);
  50.  
  51.             //set different speeds and such
  52.             mGravity = NORMAL_GRAVITY;
  53.             mMaxspeed = new Point(4, 8);
  54.             mFriction = new Point(0.5, 0.5);
  55.  
  56.             //set up animations
  57.             sprPlayer.add("standLeft", [0], 0, false);
  58.             sprPlayer.add("standRight", [8], 0, false);
  59.             sprPlayer.add("walkLeft", [0, 1, 2, 3, 4, 5, 6, 7], 0.2, true);
  60.             sprPlayer.add("walkRight", [8, 9, 10, 11, 12, 13, 14, 15], 0.2, true);
  61.  
  62.             sprPlayer.add("jumpLeft", [2], 0, false);
  63.             sprPlayer.add("jumpRight", [10], 0, false);
  64.  
  65.             sprPlayer.play("standRight");
  66.  
  67.             //set hitbox & graphic
  68.             setHitbox(16, 24, -8, -8);
  69.             graphic = sprPlayer;
  70.             type = tipo;
  71.  
  72.  
  73.             FP.watch("jumpFlag", "shootpw");
  74.         }
  75.  
  76.         override public function update():void {
  77.  
  78.             //did we... die?
  79.             if (dead){
  80.                 sprPlayer.alpha -= 0.1;
  81.                 return;
  82.             } else if (sprPlayer.alpha < 1){
  83.                 sprPlayer.alpha += 0.1
  84.             }
  85.  
  86.             //are we on the ground?
  87.             onground = false;
  88.             if (collide(solid, x, y + 1)){
  89.                 onground = true;
  90.             }
  91.  
  92.             //set acceleration to nothing
  93.             acceleration.x = 0;
  94.  
  95.             //increase acceeration
  96.             if (Input.check(Global.keyLeft)){
  97.                 acceleration.x = -movement;
  98.                 direction = false;
  99.             }
  100.             if (Input.check(Global.keyRight)){
  101.                 acceleration.x = movement;
  102.                 direction = true;
  103.             }
  104.  
  105.             //friction
  106.             if (!Input.check(Global.keyLeft) && !Input.check(Global.keyRight)){
  107.                 friction(true, false);
  108.             }
  109.  
  110.             //jump
  111.             if (wallJump==0 && Input.pressed(Global.keyA) && onground) {
  112.                 trace("jump");
  113.                 speed.y = -jump;
  114.                 jumpFlag = true;
  115.             }
  116.  
  117.             //double jump
  118.             if (doubleJumpPw == true && Input.pressed(Global.keyA) && !(doubleJump) && !collide("Solid", x, y + 2)) {
  119.                 trace("double jump");
  120.                 speed.y = -jump;
  121.                 doubleJump = true;
  122.                 jumpFlag = true;
  123.             }
  124.             if (doubleJump){
  125.                 if (collide("Solid", x, y + 1)){
  126.                     doubleJump = false;
  127.                     jumpFlag = false;
  128.                 }
  129.             }
  130.            
  131.             // wall jump
  132.             if (wallJumppPw) {
  133.                 // sliding control
  134.                 if (!onground && collide("Solid", x+width+1, y) && !collide("Solid",x,y-1)){
  135.                     //trace("right slinding");
  136.                     wallJump = 2;
  137.                 }
  138.                 if (!onground && collide("Solid", x - 1, y) && !collide("Solid",x,y-1)){
  139.                     //trace("left slinding");
  140.                     wallJump = 1;
  141.                 }          
  142.                 // apply wall jump
  143.                 if (!onground && Input.pressed(Global.keyA) && wallJump == 1 && speed.y >= 0) {
  144.                     trace("i'm on left wall");
  145.                     speed.y = -jump;
  146.                     speed.x = 64;
  147.                     direction = true;
  148.                 }
  149.                 if (!onground && Input.pressed(Global.keyA) && wallJump == 2 && speed.y >= 0) {
  150.                     trace("i'm on right wall");
  151.                     speed.y = -jump;
  152.                     speed.x = -64;
  153.                     direction = false;
  154.                 }      
  155.             }  
  156.            
  157.             //shoot
  158.             if (shootpw){
  159.                 if (Input.pressed(Global.keyB)){
  160.                     var bullet:Bullet = new Bullet(x, y + 8);
  161.                     if (speed.x > 0 || direction){
  162.                         bullet.x += 36;
  163.                     }
  164.                     if (speed.x < 0 || !direction){
  165.                         bullet.x -= 8;
  166.                         bullet.movement = -8;
  167.                     }
  168.                     FP.world.add(bullet);
  169.                 }
  170.             }
  171.  
  172.             //set the gravity
  173.             gravity();
  174.  
  175.             //make sure we're not going too fast
  176.             maxspeed(true, true);
  177.  
  178.             //variable jumping (tripple gravity)
  179.             if (speed.y < 0 && !Input.check(Global.keyA)){
  180.                 gravity();
  181.                 gravity();
  182.             }
  183.  
  184.             //set the sprites according to if we're on the ground, and if we are moving or not
  185.             if (onground){
  186.                 if (speed.x < 0){
  187.                     sprPlayer.play("walkLeft");
  188.                 }
  189.                 if (speed.x > 0){
  190.                     sprPlayer.play("walkRight");
  191.                 }
  192.  
  193.                 if (speed.x == 0){
  194.                     if (direction){
  195.                         sprPlayer.play("standRight");
  196.                     } else {
  197.                         sprPlayer.play("standLeft");
  198.                     }
  199.                 }
  200.                 jumpFlag = false;
  201.                 mGravity = NORMAL_GRAVITY;
  202.             } else {
  203.                 if (direction){
  204.                     sprPlayer.play("jumpRight");
  205.                 } else {
  206.                     sprPlayer.play("jumpLeft");
  207.                 }
  208.             }
  209.  
  210.             //set the motion. We set this later so it stops all movement if we should be stopped
  211.             motion();
  212.  
  213.             wallJump = 0;
  214.            
  215.             collisionResponse();
  216.         }
  217.  
  218.         public function killme():void {
  219.             dead = true;
  220.             Global.restart = true;
  221.         }
  222.  
  223.         private function collisionResponse():void {
  224.             //did we just get.. KILLED? D:
  225.             if (collide("Spikes", x, y) && speed.y > 0){
  226.                 //killme!
  227.                 killme();
  228.             }
  229.            
  230.             var ent:Entity = null;
  231.  
  232.             // double jump power up collect
  233.             if (!doubleJumpPw){
  234.                 ent = collide("double", x, y)
  235.                 if (ent != null){
  236.                     doubleJumpPw = true;
  237.                     FP.world.remove(ent);
  238.                 }
  239.             }
  240.  
  241.             // shoot power up collect
  242.             if (!shootpw){
  243.                 ent = collide("shoot", x, y)
  244.                 if (ent != null){
  245.                     shootpw = true;
  246.                     FP.world.remove(ent);
  247.                 }
  248.             }
  249.  
  250.             // shoot power up collect
  251.             if (!wallJumppPw){
  252.                 ent = collide("wallJump", x, y)
  253.                 if (ent != null){
  254.                     wallJumppPw = true;
  255.                     FP.world.remove(ent);
  256.                 }
  257.             }          
  258.         }
  259.        
  260.         public function animEnd():void {
  261.         }
  262.  
  263.     }
  264.  
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement