Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 3.50 KB | None | 0 0
  1. package ;
  2.  
  3. import com.khapunk.graphics.Animator;
  4. import com.khapunk.graphics.Spritemap;
  5. import com.khapunk.KP;
  6. import com.khapunk.utils.Touch;
  7. import kha.Loader;
  8.  
  9. import com.khapunk.utils.Input;
  10. import com.khapunk.utils.PunkKey;
  11. import Physics;
  12.  
  13. private enum JumpStyle
  14. {
  15.     Normal;
  16.     Gravity;
  17.     Disable;
  18. }
  19.  
  20. // Example character class using simple physics
  21. class Player extends Physics
  22. {
  23.  
  24.     public var sprite:Spritemap;
  25.    
  26.     public function new(x:Float, y:Float)
  27.     {
  28.         super(x, y);
  29.  
  30.         setHitbox(16, 32, -8);
  31.    
  32.         // Set physics properties
  33.         gravity.y = 1.0;
  34.         maxVelocity.y = kJumpForce;
  35.         maxVelocity.x = kMoveSpeed * 4;
  36.         friction.x = 0.82; // floor friction
  37.         friction.y = 0.99; // wall friction
  38.  
  39.         sprite = new Spritemap(Loader.the.getImage("character"),32, 32);
  40.  
  41.         sprite.add("norm_idle", [8, 8, 8, 9], 3, true);
  42.         sprite.add("norm_walk", [0, 1, 2, 3, 4, 5, 6, 7], 19, true);
  43.         sprite.add("norm_jump", [10]);
  44.  
  45.         sprite.add("grav_idle", [19, 19, 19, 20], 2, true);
  46.         sprite.add("grav_walk", [11, 12, 13, 14, 15, 16, 17, 18], 19, true);
  47.         sprite.add("grav_jump", [21]);
  48.  
  49.         graphic = sprite;
  50.        
  51.         sprite.play("norm_walk");
  52.        
  53.         // Define input keys
  54.         Input.define("left", [PunkKey.A, PunkKey.LEFT]);
  55.         Input.define("right", [PunkKey.D, PunkKey.RIGHT]);
  56.         Input.define("jump", [PunkKey.W, PunkKey.SPACE, PunkKey.UP]);
  57.    
  58.     }
  59.  
  60.    
  61.     public function doJump()
  62.     {
  63.    
  64.         if (!onGround) return;
  65.         switch (jumpStyle)
  66.         {
  67.             case Normal:
  68.                 #if !flash
  69.                 //var sfx = new Sfx("sfx/jump.wav");
  70.                 //sfx.play(0.8);
  71.                 #end
  72.                 acceleration.y = -KP.sign(gravity.y) * kJumpForce;
  73.             case Gravity:
  74.                 gravity.y = -gravity.y;
  75.             case Disable:
  76.         }
  77.     }
  78.    
  79.     private function switchJumpStyle()
  80.     {
  81.         switch (jumpStyle)
  82.         {
  83.             case Normal:  jumpStyle = Gravity;
  84.             case Gravity: jumpStyle = Normal;
  85.             case Disable: trace('disabled');
  86.         }
  87.     }
  88.    
  89.     override public function update()
  90.     {
  91.        
  92.         acceleration.x = acceleration.y = 0;
  93.         KP.camera.x = this.x - KP.halfWidth;
  94.         KP.camera.y = this.y - KP.halfHeight;
  95.         if (Input.leftMouseDown)
  96.         {
  97.             /*if (scene.mouseX < x)
  98.                 acceleration.x = -kMoveSpeed;
  99.             if (scene.mouseX > x)
  100.                 acceleration.x = kMoveSpeed;*/
  101.         }
  102.        
  103.         if (Input.leftMouseClicked)
  104.         {
  105.             if (scene.mouseY < y - 32)
  106.                 doJump();
  107.         }
  108.  
  109.         if (Input.check("left"))
  110.             acceleration.x = -kMoveSpeed;
  111.  
  112.         if (Input.check("right"))
  113.             acceleration.x = kMoveSpeed;
  114.  
  115.         if (Input.pressed(PunkKey.J))
  116.         {
  117.             switchJumpStyle();
  118.         }
  119.  
  120.        
  121.         if (Input.pressed("jump"))
  122.         {
  123.             doJump();
  124.         }
  125.  
  126.         // Make animation changes here
  127.         setAnimation();
  128.  
  129.         super.update();
  130.  
  131.         // Always face the direction we were last heading
  132.         if (velocity.x < 0)
  133.         {
  134.                 //trace("<0");
  135.             sprite.flippedX = true; // left
  136.         }
  137.         else if (velocity.x > 0)
  138.         {
  139.                 //trace(">0");
  140.             sprite.flippedX = false; // right
  141.         }
  142.    
  143.     }
  144.    
  145.     function onTouchInput(touch:Touch) : Bool
  146.     {
  147.         if (collidePoint(x, y, touch.sceneX, touch.sceneY))
  148.         switchJumpStyle();
  149.         return false;
  150.     }
  151.  
  152.     private function setAnimation()
  153.     {
  154.         var anim:String = "norm_";
  155.         if (gravity.y < 0)
  156.         {
  157.             anim = "grav_";
  158.         }
  159.  
  160.         if (onGround)
  161.         {
  162.             if (velocity.x == 0)
  163.             {
  164.                 sprite.play(anim + "idle",false, false);
  165.             }
  166.             else
  167.             {
  168.                 sprite.play(anim + "walk",false,false);
  169.             }
  170.         }
  171.         else
  172.         {
  173.             sprite.play(anim + "jump");
  174.         }
  175.     }
  176.  
  177.     private static var jumpStyle:JumpStyle = Normal;
  178.     private static inline var kMoveSpeed:Float = 0.8;
  179.     private static inline var kJumpForce:Int = 13;
  180.     var switched:Bool = false;
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement