NaxeCode

Untitled

Aug 23rd, 2017
537
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package;
  2.  
  3. import flixel.addons.util.FlxFSM;
  4. import flixel.FlxG;
  5. import flixel.FlxSprite;
  6. import flixel.FlxObject;
  7. import flixel.util.FlxColor;
  8.  
  9. class Player extends FlxSprite
  10. {
  11.     // Finite state machine
  12.     public var fsm:FlxFSM<Player>;
  13.  
  14.     // Current State
  15.     var state:PlayState;
  16.  
  17.     public function new(?X:Float = 0, ?Y:Float = 0, STATE:PlayState)
  18.     {
  19.         super(X, Y);
  20.  
  21.         state = STATE;
  22.  
  23.         initGraphics();
  24.         initPhysics();
  25.         initFSM();
  26.  
  27.         debug();
  28.     }
  29.  
  30.     private function initGraphics():Void
  31.     {
  32.         loadGraphic(AssetPaths.char__png, true);
  33.  
  34.         setFacingFlip(FlxObject.LEFT, true, false);
  35.         setFacingFlip(FlxObject.RIGHT, false, false);
  36.  
  37.         facing = FlxObject.RIGHT;
  38.  
  39.         //scale.scale(2);
  40.         //updateHitbox();
  41.  
  42.         setSize(28, 44);
  43.         offset.set(11, 16);
  44.  
  45.         animation.add("idle", [0, 1], 5, true);
  46.         animation.add("run", [2, 3, 4], 15, true);
  47.         animation.add("jump", [5]);
  48.         animation.add("dropkick", [6]);
  49.         animation.add("land", [7]);
  50.  
  51.         animation.play("idle");
  52.     }
  53.  
  54.     private function initPhysics():Void
  55.     {
  56.         acceleration.y = Reg.gravity;
  57.         maxVelocity.set(Reg.speed / 1.2, Reg.gravity);
  58.         drag.set(Reg.dragX, 0);
  59.     }
  60.  
  61.     private function initFSM():Void
  62.     {
  63.         fsm = new FlxFSM<Player>(this);
  64.         fsm.transitions
  65.             .add(Idle, Jump, Conditions.jump)
  66.             .add(Jump, Idle, Conditions.grounded)
  67.             .add(Jump, DropKick, Conditions.ctrlPressed)
  68.             .add(Jump, ArcJump, Conditions.jumpReleased)
  69.             .add(ArcJump, Idle, Conditions.grounded)
  70.             .add(ArcJump, DropKick, Conditions.ctrlPressed)
  71.             .add(DropKick, DropKickFinish, Conditions.grounded)
  72.             .add(DropKickFinish, BackStep, Conditions.ctrlPressed)
  73.             .add(DropKickFinish, Idle, Conditions.grounded)
  74.             .add(BackStep, Idle, Conditions.animationFinished)
  75.             .start(Idle);
  76.     }
  77.  
  78.     private function debug():Void
  79.     {
  80.         FlxG.watch.add(this.fsm, "stateClass");
  81.         FlxG.watch.add(this, "velocity");
  82.     }
  83.  
  84.     override public function update(elapsed:Float):Void
  85.     {
  86.         fsm.update(elapsed);
  87.         super.update(elapsed);
  88.     }
  89. }
  90.  
  91. class Conditions
  92. {
  93.     public static function jump(Owner:Player):Bool
  94.     {
  95.         return (FlxG.keys.justPressed.SPACE && Owner.isTouching(FlxObject.DOWN));
  96.     }
  97.  
  98.     public static function jumpReleased(Owner:Player):Bool
  99.     {
  100.         return (FlxG.keys.justReleased.SPACE && !Owner.isTouching(FlxObject.DOWN));
  101.     }
  102.    
  103.     public static function grounded(Owner:Player):Bool
  104.     {
  105.         return Owner.isTouching(FlxObject.DOWN);
  106.     }
  107.  
  108.     public static function ctrlPressed(Owner:Player):Bool
  109.     {
  110.         return FlxG.keys.justPressed.D;
  111.     }
  112.    
  113.     public static function groundSlam(Owner:Player):Bool
  114.     {
  115.         return FlxG.keys.justPressed.DOWN && !Owner.isTouching(FlxObject.DOWN);
  116.     }
  117.    
  118.     public static function animationFinished(Owner:Player):Bool
  119.     {
  120.         return Owner.animation.finished;
  121.     }
  122. }
  123.  
  124. class Idle extends FlxFSMState<Player>
  125. {
  126.     override public function enter(owner:Player, fsm:FlxFSM<Player>):Void
  127.     {
  128.         owner.animation.play("idle");
  129.     }
  130.    
  131.     override public function update(elapsed:Float, owner:Player, fsm:FlxFSM<Player>):Void
  132.     {
  133.         owner.acceleration.x = 0;
  134.         if (FlxG.keys.pressed.LEFT && !FlxG.keys.pressed.RIGHT)
  135.         {
  136.             owner.facing = FlxObject.LEFT;
  137.  
  138.             owner.animation.play("run");
  139.  
  140.             owner.acceleration.x = -owner.maxVelocity.x * 6;
  141.         }
  142.         else if (FlxG.keys.pressed.RIGHT && !FlxG.keys.pressed.LEFT)
  143.         {
  144.             owner.facing = FlxObject.RIGHT;
  145.  
  146.             owner.animation.play("run");
  147.  
  148.             owner.acceleration.x = owner.maxVelocity.x * 6;
  149.         }
  150.         else
  151.         {
  152.             owner.animation.play("idle");
  153.         }
  154.     }
  155. }
  156.  
  157. class Jump extends FlxFSMState<Player>
  158. {
  159.     override public function enter(owner:Player, fsm:FlxFSM<Player>):Void
  160.     {
  161.         owner.animation.play("jump");
  162.         owner.velocity.y = -Reg.jumpPower;
  163.         FlxG.sound.play(AssetPaths.jump__mp3, Reg.sfxVolume);
  164.     }
  165.    
  166.     override public function update(elapsed:Float, owner:Player, fsm:FlxFSM<Player>):Void
  167.     {
  168.         owner.acceleration.x = 0;
  169.         if (FlxG.keys.pressed.LEFT && !FlxG.keys.pressed.RIGHT)
  170.         {
  171.             owner.facing = FlxObject.LEFT;
  172.             owner.acceleration.x = -owner.maxVelocity.x * 6;
  173.         }
  174.         else if (FlxG.keys.pressed.RIGHT && !FlxG.keys.pressed.LEFT)
  175.         {
  176.             owner.facing = FlxObject.RIGHT;
  177.             owner.acceleration.x = owner.maxVelocity.x * 6;
  178.         }
  179.     }
  180. }
  181.  
  182. class ArcJump extends Jump
  183. {
  184.     override public function enter(owner:Player, fsm:FlxFSM<Player>):Void
  185.     {
  186.         owner.animation.play("jump");
  187.         if (owner.velocity.y < -50)
  188.         {
  189.             owner.velocity.y = (owner.velocity.y / 2);
  190.             trace("arc successful");
  191.         }
  192.         else
  193.         {
  194.             trace("arc unsuccessful");
  195.         }
  196.     }
  197. }
  198.  
  199. class DropKick extends FlxFSMState<Player>
  200. {
  201.     private var _ticks:Float;
  202.    
  203.     override public function enter(owner:Player, fsm:FlxFSM<Player>):Void
  204.     {
  205.         owner.animation.play("dropkick");
  206.         owner.velocity.x = 0;
  207.         owner.acceleration.x = 0;
  208.         _ticks = 0;
  209.     }
  210.    
  211.     override public function update(elapsed:Float, owner:Player, fsm:FlxFSM<Player>):Void
  212.     {
  213.         _ticks++;
  214.         if (_ticks < 10)
  215.         {
  216.             owner.velocity.y = 0;
  217.         }
  218.         else
  219.         {
  220.             switch (owner.facing)
  221.             {
  222.                 case FlxObject.LEFT:
  223.                     owner.velocity.y = Reg.gravity;
  224.                     owner.velocity.x = -Reg.gravity;
  225.                 case FlxObject.RIGHT:
  226.                     owner.velocity.y = Reg.gravity;
  227.                     owner.velocity.x = Reg.gravity;
  228.             }
  229.         }
  230.     }
  231. }
  232.  
  233. class DropKickFinish extends FlxFSMState<Player>
  234. {
  235.     override public function enter(owner:Player, fsm:FlxFSM<Player>):Void
  236.     {
  237.         owner.animation.play("land");
  238.         owner.velocity.x = 0;
  239.         owner.acceleration.x = 0;
  240.     }
  241. }
  242.  
  243. class BackStep extends FlxFSMState<Player>
  244. {
  245.     private var _ticks:Int = 0;
  246.     override public function enter(owner:Player, fsm:FlxFSM<Player>):Void
  247.     {
  248.         _ticks = 0;
  249.         owner.animation.play("land");
  250.     }
  251.  
  252.     override public function update(elapsed:Float, owner:Player, fsm:FlxFSM<Player>):Void
  253.     {
  254.         _ticks++;
  255.         if (_ticks < 15)
  256.         {
  257.             owner.velocity.x = 0;
  258.         }
  259.         else
  260.         {
  261.             if (owner.facing == FlxObject.LEFT)
  262.             {
  263.                 owner.velocity.x = -100;
  264.             }
  265.             else if (owner.facing == FlxObject.RIGHT)
  266.             {
  267.                 owner.velocity.x = 100;
  268.             }
  269.         }
  270.     }
  271. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×