Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package;
- import flixel.addons.util.FlxFSM;
- import flixel.FlxG;
- import flixel.FlxSprite;
- import flixel.FlxObject;
- import flixel.util.FlxColor;
- class Player extends FlxSprite
- {
- // Finite state machine
- public var fsm:FlxFSM<Player>;
- // Current State
- var state:PlayState;
- public function new(?X:Float = 0, ?Y:Float = 0, STATE:PlayState)
- {
- super(X, Y);
- state = STATE;
- initGraphics();
- initPhysics();
- initFSM();
- debug();
- }
- private function initGraphics():Void
- {
- loadGraphic(AssetPaths.char__png, true);
- setFacingFlip(FlxObject.LEFT, true, false);
- setFacingFlip(FlxObject.RIGHT, false, false);
- facing = FlxObject.RIGHT;
- //scale.scale(2);
- //updateHitbox();
- setSize(28, 44);
- offset.set(11, 16);
- animation.add("idle", [0, 1], 5, true);
- animation.add("run", [2, 3, 4], 15, true);
- animation.add("jump", [5]);
- animation.add("dropkick", [6]);
- animation.add("land", [7]);
- animation.play("idle");
- }
- private function initPhysics():Void
- {
- acceleration.y = Reg.gravity;
- maxVelocity.set(Reg.speed / 1.2, Reg.gravity);
- drag.set(Reg.dragX, 0);
- }
- private function initFSM():Void
- {
- fsm = new FlxFSM<Player>(this);
- fsm.transitions
- .add(Idle, Jump, Conditions.jump)
- .add(Jump, Idle, Conditions.grounded)
- .add(Jump, DropKick, Conditions.ctrlPressed)
- .add(Jump, ArcJump, Conditions.jumpReleased)
- .add(ArcJump, Idle, Conditions.grounded)
- .add(ArcJump, DropKick, Conditions.ctrlPressed)
- .add(DropKick, DropKickFinish, Conditions.grounded)
- .add(DropKickFinish, BackStep, Conditions.ctrlPressed)
- .add(DropKickFinish, Idle, Conditions.grounded)
- .add(BackStep, Idle, Conditions.animationFinished)
- .start(Idle);
- }
- private function debug():Void
- {
- FlxG.watch.add(this.fsm, "stateClass");
- FlxG.watch.add(this, "velocity");
- }
- override public function update(elapsed:Float):Void
- {
- fsm.update(elapsed);
- super.update(elapsed);
- }
- }
- class Conditions
- {
- public static function jump(Owner:Player):Bool
- {
- return (FlxG.keys.justPressed.SPACE && Owner.isTouching(FlxObject.DOWN));
- }
- public static function jumpReleased(Owner:Player):Bool
- {
- return (FlxG.keys.justReleased.SPACE && !Owner.isTouching(FlxObject.DOWN));
- }
- public static function grounded(Owner:Player):Bool
- {
- return Owner.isTouching(FlxObject.DOWN);
- }
- public static function ctrlPressed(Owner:Player):Bool
- {
- return FlxG.keys.justPressed.D;
- }
- public static function groundSlam(Owner:Player):Bool
- {
- return FlxG.keys.justPressed.DOWN && !Owner.isTouching(FlxObject.DOWN);
- }
- public static function animationFinished(Owner:Player):Bool
- {
- return Owner.animation.finished;
- }
- }
- class Idle extends FlxFSMState<Player>
- {
- override public function enter(owner:Player, fsm:FlxFSM<Player>):Void
- {
- owner.animation.play("idle");
- }
- override public function update(elapsed:Float, owner:Player, fsm:FlxFSM<Player>):Void
- {
- owner.acceleration.x = 0;
- if (FlxG.keys.pressed.LEFT && !FlxG.keys.pressed.RIGHT)
- {
- owner.facing = FlxObject.LEFT;
- owner.animation.play("run");
- owner.acceleration.x = -owner.maxVelocity.x * 6;
- }
- else if (FlxG.keys.pressed.RIGHT && !FlxG.keys.pressed.LEFT)
- {
- owner.facing = FlxObject.RIGHT;
- owner.animation.play("run");
- owner.acceleration.x = owner.maxVelocity.x * 6;
- }
- else
- {
- owner.animation.play("idle");
- }
- }
- }
- class Jump extends FlxFSMState<Player>
- {
- override public function enter(owner:Player, fsm:FlxFSM<Player>):Void
- {
- owner.animation.play("jump");
- owner.velocity.y = -Reg.jumpPower;
- FlxG.sound.play(AssetPaths.jump__mp3, Reg.sfxVolume);
- }
- override public function update(elapsed:Float, owner:Player, fsm:FlxFSM<Player>):Void
- {
- owner.acceleration.x = 0;
- if (FlxG.keys.pressed.LEFT && !FlxG.keys.pressed.RIGHT)
- {
- owner.facing = FlxObject.LEFT;
- owner.acceleration.x = -owner.maxVelocity.x * 6;
- }
- else if (FlxG.keys.pressed.RIGHT && !FlxG.keys.pressed.LEFT)
- {
- owner.facing = FlxObject.RIGHT;
- owner.acceleration.x = owner.maxVelocity.x * 6;
- }
- }
- }
- class ArcJump extends Jump
- {
- override public function enter(owner:Player, fsm:FlxFSM<Player>):Void
- {
- owner.animation.play("jump");
- if (owner.velocity.y < -50)
- {
- owner.velocity.y = (owner.velocity.y / 2);
- trace("arc successful");
- }
- else
- {
- trace("arc unsuccessful");
- }
- }
- }
- class DropKick extends FlxFSMState<Player>
- {
- private var _ticks:Float;
- override public function enter(owner:Player, fsm:FlxFSM<Player>):Void
- {
- owner.animation.play("dropkick");
- owner.velocity.x = 0;
- owner.acceleration.x = 0;
- _ticks = 0;
- }
- override public function update(elapsed:Float, owner:Player, fsm:FlxFSM<Player>):Void
- {
- _ticks++;
- if (_ticks < 10)
- {
- owner.velocity.y = 0;
- }
- else
- {
- switch (owner.facing)
- {
- case FlxObject.LEFT:
- owner.velocity.y = Reg.gravity;
- owner.velocity.x = -Reg.gravity;
- case FlxObject.RIGHT:
- owner.velocity.y = Reg.gravity;
- owner.velocity.x = Reg.gravity;
- }
- }
- }
- }
- class DropKickFinish extends FlxFSMState<Player>
- {
- override public function enter(owner:Player, fsm:FlxFSM<Player>):Void
- {
- owner.animation.play("land");
- owner.velocity.x = 0;
- owner.acceleration.x = 0;
- }
- }
- class BackStep extends FlxFSMState<Player>
- {
- private var _ticks:Int = 0;
- override public function enter(owner:Player, fsm:FlxFSM<Player>):Void
- {
- _ticks = 0;
- owner.animation.play("land");
- }
- override public function update(elapsed:Float, owner:Player, fsm:FlxFSM<Player>):Void
- {
- _ticks++;
- if (_ticks < 15)
- {
- owner.velocity.x = 0;
- }
- else
- {
- if (owner.facing == FlxObject.LEFT)
- {
- owner.velocity.x = -100;
- }
- else if (owner.facing == FlxObject.RIGHT)
- {
- owner.velocity.x = 100;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement