Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package {
- import org.flixel.*;
- import TSDevice;
- import PlayState;
- public class Player extends FlxSprite {
- private var _jump:Number;
- private var _fall:Number;
- private var wallJump:Number;
- private var doubleJump:Boolean;
- private var dash:Boolean;
- private var jumpType:int; //0 = normal jump, 1 = double jump, 2 = wall jump
- private var jumpDuration:Number = .32; //Maximum jump duration from rest
- private var ts:TSDevice;
- private var atk:int;
- private var hp:int;
- private var maxHealth:int; //Health and HP are two separate but closely related values. See relevant methods for details.
- public var maxHP:int;
- private var glide:Number; //Fall speed while gliding
- public var debug:int;
- private const AGILITY:Number = 4; //Walking acceleration
- private const GRAVITY:Number = 1000; //Overall gravity
- private const TERMINAL:Number = 800; //Terminal velocity (Y)
- private const WALKSPEED:Number = 120; //Normal walking speed
- private const FRICTION:Number = 900; //Horizontal friction
- private const WALLJUMPX:Number = 200; //Wall jump velocity (away from wall)
- private const RSMODIFIER:Number = 1000; //Jump improvement from running start (higher is smaller)
- private const PEAK:Number = 160; //Y-velocity of jump after releasing
- private const JUMPBASESPEED:Number = 150; //Initial speed of jump
- private const JUMPACCEL:Number = 220; //Acceleration of jump
- private const MAXJUMPVELOCITY:Number = 220; //Maximum jump velocity
- public function Player(x:Number, y:Number, clone:Player = null)
- {
- super(x, y);
- acceleration.y = GRAVITY;
- maxVelocity.y = TERMINAL;
- maxVelocity.x = WALKSPEED;
- drag.x = FRICTION;
- ts = new TSDevice;
- makeGraphic(12, 18, ts.color);
- health = maxHP = ts.getPower(1,0) + ts.getPower(1,3) + ts.getPower(1,5) + 5;
- }
- public function getTSStatus():String {
- return ts.tsTest();
- }
- public function setVars():void
- {
- if (isTouching(FLOOR))
- {
- _jump = 0;
- _fall = -1;
- doubleJump = true;
- jumpType = 0;
- if (FlxG.keys.X)
- {
- maxVelocity.x = WALKSPEED * ts.getPower(2,1);
- drag.x = FRICTION * ts.getPower(2,1);
- }
- else
- {
- maxVelocity.x = WALKSPEED;
- drag.x = FRICTION;
- }
- }
- }
- public function jumpPhysics():void
- {
- wallJump = 0;
- if (isTouching(LEFT) && !isTouching(FLOOR))
- {
- wallJump = 1;
- }
- if (isTouching(RIGHT) && !isTouching(FLOOR))
- {
- wallJump = -1;
- }
- if (wallJump != 0 && FlxG.keys.justPressed("Z"))
- {
- velocity.x = wallJump * maxVelocity.x * (ts.getPower(2,3) * 4);
- velocity.y = 0;
- _jump = 0;
- jumpType = 2;
- if (FlxG.keys.X)
- {
- maxVelocity.x = WALKSPEED * ts.getPower(2,1);
- drag.x = FRICTION * ts.getPower(2,1);
- }
- else
- {
- maxVelocity.x = WALKSPEED;
- drag.x = FRICTION;
- }
- }
- //Double jumping
- if (FlxG.keys.justPressed("Z") && !isTouching(FLOOR) && wallJump == 0 && doubleJump)
- {
- _jump = 0;
- jumpType = 1;
- velocity.y = 0;
- doubleJump = false;
- }
- //Determine jump duration based on jump type and relevant ability
- switch (jumpType)
- {
- case 0:
- jumpDuration = ts.getPower(2, 0);
- break;
- case 1:
- jumpDuration = ts.getPower(2, 2);
- break;
- case 2:
- jumpDuration = ts.getPower(2, 3);
- break;
- }
- if(((_jump > 0) && (FlxG.keys.Z)) || (_jump == 0 && FlxG.keys.justPressed("Z")))
- {
- _jump += FlxG.elapsed;
- if (_jump > (Math.abs(velocity.x / RSMODIFIER )+ 1) * jumpDuration) //Maximum jump duration is based on current x velocity to allow for running jumps.
- _jump = -1;
- else
- _fall = 1;
- }
- else _jump = -1;
- if (isTouching(CEILING))
- {
- _jump = -1;
- }
- if (_jump > 0)
- {
- _fall = 0;
- if(_jump < 0.017)
- velocity.y = -JUMPBASESPEED;
- else
- acceleration.y = -JUMPACCEL;
- }
- else//Falling
- {
- if (_fall == 0)
- {
- velocity.y = -PEAK;
- _fall = 1;
- }
- acceleration.y = GRAVITY;
- }
- //Gliding
- if (FlxG.keys.SHIFT && velocity.y > ts.getPower(2,5))
- velocity.y = ts.getPower(2,5);
- if (velocity.y <= -MAXJUMPVELOCITY)
- velocity.y = -MAXJUMPVELOCITY;
- }
- public function attack():void {
- var direction:int;
- if (facing == LEFT)
- direction = -320;
- else
- direction = 320;
- Projectile.fire(x,y+8,direction,ts.getPower(0,0) + atk);
- }
- public function move() :void
- {
- acceleration.x = 0;
- if (FlxG.keys.LEFT && !FlxG.keys.RIGHT)
- {
- facing = LEFT;
- acceleration.x = -maxVelocity.x * AGILITY;
- }
- if (FlxG.keys.RIGHT && !FlxG.keys.LEFT)
- {
- facing = RIGHT;
- acceleration.x = maxVelocity.x * AGILITY;
- }
- }
- override public function update():void {
- setVars();
- if (FlxG.keys.Z || !isTouching(FLOOR))
- jumpPhysics();
- move();
- if (FlxG.keys.justPressed("C") && ts.getPower(0, 0) != 0)
- attack();
- //Standard trishifting
- if (FlxG.keys.A)
- shift(0, false);
- if (FlxG.keys.S)
- shift(1, false);
- if (FlxG.keys.D)
- shift(2, false);
- //super.update();
- }
- public function getTSData(i:int):int {
- return ts.getEnergy(i);
- }
- public function energyIncrease():void
- {
- ts.addEnergy();
- }
- public function getTS():TSDevice {
- return ts;
- }
- public function shift(which:int,inverse:Boolean = false):void {
- ts.trishift(which, false);
- maxHP = ts.getPower(1, 0) + ts.getPower(1, 3) + ts.getPower(1, 5) + 5;
- atk = ts.getPower(0, 1) + ts.getPower(0, 3) + ts.getPower(0, 6)
- makeGraphic(12, 18, ts.color);
- }
- public function takeHit(damage:int,knockback:Hazard):void {
- if (!flickering)
- {
- hurt(damage);
- flicker(.7);
- }
- }
- override public function kill():void {
- (FlxG.state as PlayState).restart();
- }
- }
- }
Add Comment
Please, Sign In to add comment