Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ;
- import com.khapunk.graphics.Animator;
- import com.khapunk.graphics.Spritemap;
- import com.khapunk.KP;
- import com.khapunk.utils.Touch;
- import kha.Loader;
- import com.khapunk.utils.Input;
- import com.khapunk.utils.PunkKey;
- import Physics;
- private enum JumpStyle
- {
- Normal;
- Gravity;
- Disable;
- }
- // Example character class using simple physics
- class Player extends Physics
- {
- public var sprite:Spritemap;
- public function new(x:Float, y:Float)
- {
- super(x, y);
- setHitbox(16, 32, -8);
- // Set physics properties
- gravity.y = 1.0;
- maxVelocity.y = kJumpForce;
- maxVelocity.x = kMoveSpeed * 4;
- friction.x = 0.82; // floor friction
- friction.y = 0.99; // wall friction
- sprite = new Spritemap(Loader.the.getImage("character"),32, 32);
- sprite.add("norm_idle", [8, 8, 8, 9], 3, true);
- sprite.add("norm_walk", [0, 1, 2, 3, 4, 5, 6, 7], 19, true);
- sprite.add("norm_jump", [10]);
- sprite.add("grav_idle", [19, 19, 19, 20], 2, true);
- sprite.add("grav_walk", [11, 12, 13, 14, 15, 16, 17, 18], 19, true);
- sprite.add("grav_jump", [21]);
- graphic = sprite;
- sprite.play("norm_walk");
- // Define input keys
- Input.define("left", [PunkKey.A, PunkKey.LEFT]);
- Input.define("right", [PunkKey.D, PunkKey.RIGHT]);
- Input.define("jump", [PunkKey.W, PunkKey.SPACE, PunkKey.UP]);
- }
- public function doJump()
- {
- if (!onGround) return;
- switch (jumpStyle)
- {
- case Normal:
- #if !flash
- //var sfx = new Sfx("sfx/jump.wav");
- //sfx.play(0.8);
- #end
- acceleration.y = -KP.sign(gravity.y) * kJumpForce;
- case Gravity:
- gravity.y = -gravity.y;
- case Disable:
- }
- }
- private function switchJumpStyle()
- {
- switch (jumpStyle)
- {
- case Normal: jumpStyle = Gravity;
- case Gravity: jumpStyle = Normal;
- case Disable: trace('disabled');
- }
- }
- override public function update()
- {
- acceleration.x = acceleration.y = 0;
- KP.camera.x = this.x - KP.halfWidth;
- KP.camera.y = this.y - KP.halfHeight;
- if (Input.leftMouseDown)
- {
- /*if (scene.mouseX < x)
- acceleration.x = -kMoveSpeed;
- if (scene.mouseX > x)
- acceleration.x = kMoveSpeed;*/
- }
- if (Input.leftMouseClicked)
- {
- if (scene.mouseY < y - 32)
- doJump();
- }
- if (Input.check("left"))
- acceleration.x = -kMoveSpeed;
- if (Input.check("right"))
- acceleration.x = kMoveSpeed;
- if (Input.pressed(PunkKey.J))
- {
- switchJumpStyle();
- }
- if (Input.pressed("jump"))
- {
- doJump();
- }
- // Make animation changes here
- setAnimation();
- super.update();
- // Always face the direction we were last heading
- if (velocity.x < 0)
- {
- //trace("<0");
- sprite.flippedX = true; // left
- }
- else if (velocity.x > 0)
- {
- //trace(">0");
- sprite.flippedX = false; // right
- }
- }
- function onTouchInput(touch:Touch) : Bool
- {
- if (collidePoint(x, y, touch.sceneX, touch.sceneY))
- switchJumpStyle();
- return false;
- }
- private function setAnimation()
- {
- var anim:String = "norm_";
- if (gravity.y < 0)
- {
- anim = "grav_";
- }
- if (onGround)
- {
- if (velocity.x == 0)
- {
- sprite.play(anim + "idle",false, false);
- }
- else
- {
- sprite.play(anim + "walk",false,false);
- }
- }
- else
- {
- sprite.play(anim + "jump");
- }
- }
- private static var jumpStyle:JumpStyle = Normal;
- private static inline var kMoveSpeed:Float = 0.8;
- private static inline var kJumpForce:Int = 13;
- var switched:Bool = false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement