Guest User

Untitled

a guest
Oct 16th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package {
  2.  
  3.     import org.flixel.*;
  4.     import TSDevice;
  5.     import PlayState;
  6.  
  7.     public class Player extends FlxSprite {
  8.  
  9.         private var _jump:Number;
  10.         private var _fall:Number;
  11.         private var wallJump:Number;
  12.         private var doubleJump:Boolean;
  13.         private var dash:Boolean;
  14.         private var jumpType:int; //0 = normal jump, 1 = double jump, 2 = wall jump
  15.         private var jumpDuration:Number = .32; //Maximum jump duration from rest
  16.         private var ts:TSDevice;
  17.         private var atk:int;
  18.         private var hp:int;
  19.         private var maxHealth:int; //Health and HP are two separate but closely related values.  See relevant methods for details.
  20.         public var maxHP:int;
  21.         private var glide:Number; //Fall speed while gliding
  22.         public var debug:int;
  23.        
  24.         private const AGILITY:Number = 4; //Walking acceleration
  25.         private const GRAVITY:Number = 1000; //Overall gravity
  26.         private const TERMINAL:Number = 800; //Terminal velocity (Y)
  27.         private const WALKSPEED:Number = 120; //Normal walking speed
  28.         private const FRICTION:Number = 900; //Horizontal friction
  29.         private const WALLJUMPX:Number = 200; //Wall jump velocity (away from wall)
  30.         private const RSMODIFIER:Number = 1000; //Jump improvement from running start (higher is smaller)
  31.         private const PEAK:Number = 160; //Y-velocity of jump after releasing
  32.         private const JUMPBASESPEED:Number = 150; //Initial speed of jump
  33.         private const JUMPACCEL:Number = 220; //Acceleration of jump
  34.         private const MAXJUMPVELOCITY:Number = 220; //Maximum jump velocity
  35.          
  36.         public function Player(x:Number, y:Number, clone:Player = null)
  37.         {
  38.            
  39.             super(x, y);           
  40.             acceleration.y = GRAVITY;
  41.             maxVelocity.y = TERMINAL;
  42.             maxVelocity.x = WALKSPEED;
  43.             drag.x = FRICTION;
  44.             ts = new TSDevice;
  45.             makeGraphic(12, 18, ts.color);
  46.             health = maxHP = ts.getPower(1,0) + ts.getPower(1,3) + ts.getPower(1,5) + 5;
  47.        
  48.         }
  49.        
  50.        
  51.        
  52.         public function getTSStatus():String {
  53.             return ts.tsTest();
  54.         }
  55.        
  56.        
  57.         public function setVars():void
  58.         {
  59.            
  60.             if (isTouching(FLOOR))
  61.             {      
  62.                 _jump = 0;
  63.                 _fall = -1;
  64.                 doubleJump = true;
  65.                 jumpType = 0;
  66.            
  67.            
  68.                 if (FlxG.keys.X)
  69.                 {  
  70.                 maxVelocity.x = WALKSPEED *  ts.getPower(2,1);
  71.                 drag.x = FRICTION *  ts.getPower(2,1);
  72.                 }
  73.                 else
  74.                 {
  75.                 maxVelocity.x = WALKSPEED;
  76.                 drag.x = FRICTION;
  77.                 }
  78.             }
  79.         }
  80.        
  81.         public function jumpPhysics():void
  82.         {
  83.            
  84.            
  85.             wallJump = 0;
  86.                 if (isTouching(LEFT) && !isTouching(FLOOR))
  87.                 {
  88.                     wallJump = 1;
  89.                 }
  90.                 if (isTouching(RIGHT) && !isTouching(FLOOR))
  91.                 {
  92.                     wallJump = -1;
  93.                    
  94.                 }
  95.                 if (wallJump != 0 && FlxG.keys.justPressed("Z"))
  96.                 {
  97.                 velocity.x = wallJump * maxVelocity.x * (ts.getPower(2,3) * 4);
  98.                 velocity.y = 0;
  99.                 _jump = 0;
  100.                 jumpType = 2;
  101.                 if (FlxG.keys.X)
  102.                 {  
  103.                     maxVelocity.x = WALKSPEED *  ts.getPower(2,1);
  104.                     drag.x = FRICTION *  ts.getPower(2,1);
  105.                 }
  106.                 else
  107.                 {
  108.                     maxVelocity.x = WALKSPEED;
  109.                     drag.x = FRICTION;
  110.                 }
  111.                 }
  112.                
  113.                 //Double jumping
  114.                 if (FlxG.keys.justPressed("Z") && !isTouching(FLOOR) && wallJump == 0 && doubleJump)
  115.                 {
  116.                     _jump = 0;
  117.                     jumpType = 1;
  118.                     velocity.y = 0;
  119.                     doubleJump = false;
  120.                 }
  121.                
  122.                 //Determine jump duration based on jump type and relevant ability
  123.                 switch (jumpType)
  124.                 {
  125.                     case 0:
  126.                     jumpDuration = ts.getPower(2, 0);
  127.                     break;
  128.                     case 1:
  129.                     jumpDuration = ts.getPower(2, 2);
  130.                     break;
  131.                     case 2:
  132.                     jumpDuration = ts.getPower(2, 3);
  133.                     break;
  134.                 }
  135.                
  136.             if(((_jump > 0) && (FlxG.keys.Z)) || (_jump == 0 && FlxG.keys.justPressed("Z")))
  137.             {
  138.                
  139.                 _jump += FlxG.elapsed;
  140.                 if (_jump > (Math.abs(velocity.x / RSMODIFIER )+ 1) * jumpDuration) //Maximum jump duration is based on current x velocity to allow for running jumps.
  141.                 _jump = -1;
  142.                 else
  143.                 _fall = 1;
  144.             }              
  145.             else _jump = -1;
  146.  
  147.            
  148.             if (isTouching(CEILING))
  149.             {
  150.                 _jump = -1;
  151.             }
  152.            
  153.             if (_jump > 0)
  154.             {
  155.                 _fall = 0;
  156.                 if(_jump < 0.017)
  157.                     velocity.y = -JUMPBASESPEED;
  158.                 else
  159.                     acceleration.y = -JUMPACCEL;
  160.             }
  161.             else//Falling  
  162.             {
  163.                 if (_fall == 0)
  164.                 {
  165.                 velocity.y = -PEAK;
  166.                 _fall = 1;
  167.                 }
  168.                 acceleration.y = GRAVITY;
  169.             }
  170.            
  171.             //Gliding
  172.             if (FlxG.keys.SHIFT && velocity.y > ts.getPower(2,5))
  173.             velocity.y = ts.getPower(2,5);
  174.            
  175.             if (velocity.y <= -MAXJUMPVELOCITY)
  176.             velocity.y = -MAXJUMPVELOCITY;
  177.            
  178.         }
  179.        
  180.         public function attack():void {
  181.            
  182.            
  183.             var direction:int;
  184.             if (facing == LEFT)
  185.             direction = -320;
  186.             else
  187.             direction = 320;
  188.             Projectile.fire(x,y+8,direction,ts.getPower(0,0) + atk);
  189.            
  190.         }
  191.        
  192.         public function move() :void
  193.         {
  194.             acceleration.x = 0;
  195.             if (FlxG.keys.LEFT && !FlxG.keys.RIGHT)
  196.             {
  197.                 facing = LEFT;
  198.                 acceleration.x = -maxVelocity.x * AGILITY;
  199.             }
  200.             if (FlxG.keys.RIGHT && !FlxG.keys.LEFT)
  201.             {
  202.                 facing = RIGHT;
  203.                 acceleration.x = maxVelocity.x * AGILITY;
  204.             }
  205.            
  206.             }
  207.        
  208.         override public function update():void {
  209.             setVars();
  210.             if (FlxG.keys.Z || !isTouching(FLOOR))
  211.                 jumpPhysics();
  212.            
  213.             move();
  214.            
  215.             if (FlxG.keys.justPressed("C") && ts.getPower(0, 0) != 0)
  216.             attack();
  217.            
  218.            
  219.             //Standard trishifting
  220.             if (FlxG.keys.A)
  221.                 shift(0, false);
  222.             if (FlxG.keys.S)
  223.                 shift(1, false);
  224.             if (FlxG.keys.D)
  225.                 shift(2, false);
  226.            
  227.    
  228.            
  229.  
  230.            
  231.             //super.update();
  232.         }
  233.      
  234.         public function getTSData(i:int):int {
  235.             return ts.getEnergy(i);
  236.         }
  237.        
  238.    
  239.         public function energyIncrease():void
  240.         {
  241.             ts.addEnergy();
  242.        }
  243.        
  244.        public function getTS():TSDevice {
  245.           return ts;
  246.        }
  247.  
  248.        public  function shift(which:int,inverse:Boolean = false):void {
  249.          
  250.          
  251.            
  252.            ts.trishift(which, false);
  253.            maxHP = ts.getPower(1, 0) + ts.getPower(1, 3) + ts.getPower(1, 5) + 5;
  254.            atk = ts.getPower(0, 1) + ts.getPower(0, 3) + ts.getPower(0, 6)
  255.            makeGraphic(12, 18, ts.color);
  256.        }
  257.        
  258.        public function takeHit(damage:int,knockback:Hazard):void {
  259.            if (!flickering)
  260.            {
  261.             hurt(damage);
  262.             flicker(.7);
  263.            }
  264.            
  265.          
  266.            
  267.        }
  268.        
  269.        override public function kill():void {
  270.             (FlxG.state as PlayState).restart();
  271.            
  272.             }
  273.     }
  274. }
Add Comment
Please, Sign In to add comment