Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package;
- import flixel.FlxG;
- import flixel.FlxObject;
- import flixel.group.FlxTypedGroup;
- import flixel.system.FlxSound;
- import flixel.util.FlxColor;
- #if debug_draw
- using flixel.util.FlxSpriteUtil;
- #end
- /**
- * ...
- * @author Keith Weatherby II
- */
- enum MovementModes
- {
- IDLE;
- LEFT;
- RIGHT;
- JUMP;
- FALL;
- DO_JUMP;
- DO_FALL;
- }
- class Player extends GameObject
- {
- public var lives : Int = 3;
- public var actionCount : Int = 0;
- public var actionTime : Int = 10;
- public var timeUntilRun : Int = 9;
- public var actions : Map<String, Bool> = new Map();
- public var canDoHypnoAction : Bool = true;
- public var vMotion : MovementModes;
- public var hMotion : MovementModes;
- public var doAction : Bool = false;
- public var turnOnBolt : Bool = true;
- public var sounds : Map<String, FlxSound> = new Map();
- public var bolt : GameObject;
- public var carriedEnemy : Enemy;
- public function new( gameLevel : PlayState, object : Dynamic )
- {
- super( gameLevel, object );
- actions[ "action" ] = false;
- actions[ "jump" ] = false;
- actions[ "fall" ] = false;
- actions[ "left" ] = false;
- actions[ "right" ] = false;
- hMotion = vMotion = IDLE;
- }
- override public function update():Void
- {
- this.ProcessInput();
- this.DoActions();
- this.Animate();
- super.update();
- }
- public function ProcessInput()
- {
- doAction = actions[ "action" ];
- if ( actions[ "left" ] )
- hMotion = LEFT;
- else
- if ( actions[ "right" ] )
- hMotion = RIGHT;
- else
- hMotion = IDLE;
- if ( actions[ "jump" ] )
- vMotion = DO_JUMP;
- else
- if ( actions[ "fall" ] )
- vMotion = DO_FALL;
- else
- {
- if( this.velocity.y > 0 )
- vMotion = FALL;
- else
- if( this.velocity.y < 0 )
- vMotion = JUMP;
- else
- if( this.velocity.y == 0 )
- vMotion = IDLE;
- }
- }
- public function DoActions()
- {
- this.acceleration.x = 0;
- if ( doAction )
- {
- if ( canDoHypnoAction )
- {
- // stop it from doing it more than one press
- canDoHypnoAction = false;
- TryHypnosis();
- TryLevitating();
- }
- turnOnBolt = true;
- actionCount++;
- if ( actionCount > actionTime )
- turnOnBolt = false;
- else
- if ( actionCount > timeUntilRun )
- this.speed = this.startSpeed * 1.75;
- if( turnOnBolt )
- sounds[ "hypno001" ].play();
- }
- else
- {
- canDoHypnoAction = true;
- actionCount = 0;
- turnOnBolt = false;
- this.speed = this.startSpeed;
- }
- if ( hMotion == LEFT )
- this.move( FlxObject.LEFT );
- else
- if ( hMotion == RIGHT )
- this.move( FlxObject.RIGHT );
- else
- if( this.velocity.y == 0 )
- this.move( FlxObject.NONE );
- if ( vMotion == DO_JUMP )
- {
- if ( onGround && isTouching( FlxObject.DOWN ) )
- {
- this.velocity.y = -this.maxVelocity.y / 2;
- sounds[ "jump001" ].play();
- }
- }
- else
- if ( vMotion == DO_FALL )
- {
- var stillOnMap : Bool = false;
- if ( level.gameMap != null )
- {
- if ( y < level.gameMap.heightInPixels - level.gameMap.tileHeight )
- stillOnMap = true;
- }
- if ( isTouching( FlxObject.DOWN ) && stillOnMap )
- {
- if( level.gameMap.IsTouchingTile( this.x + ( this.width / 2 ), this.y + this.height, "passthrough" ) )
- {
- y += level.gameMap.tileHeight;
- sounds[ "fall001" ].play();
- }
- }
- }
- }
- /*
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #if debug_draw
- var end = this.getMidpoint().x + dist;
- var lineStyle : LineStyle = { color: FlxColor.RED, thickness: 1 };
- Reg.debugCanvas.drawLine( this.getMidpoint().x, this.getMidpoint().y, end, this.getMidpoint().y, lineStyle);
- #end
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- */
- public function TryHypnosis()
- {
- var dist = this.facing == FlxObject.LEFT ? -36 : 36;
- var rayLength = 0;
- var closestEnemy : Enemy = null;
- var closestHit : Int = 640;
- for ( enemy in level.enemyGroup )
- {
- if ( enemy.hypnoMode == AWAKE )
- {
- var isFacingEnemy : Bool =
- ( this.facing == FlxObject.LEFT && enemy.facing == FlxObject.RIGHT ) ||
- ( this.facing == FlxObject.RIGHT && enemy.facing == FlxObject.LEFT );
- if ( isFacingEnemy )
- {
- rayLength = this.HorizontalRayHit( enemy, dist );
- if ( rayLength > 0 )
- {
- if ( rayLength <= closestHit )
- {
- closestHit = rayLength;
- closestEnemy = enemy;
- }
- }
- }
- }
- }
- if ( closestEnemy != null )
- {
- closestEnemy.SetHypnoMode( HYPNOTIZED );
- closestEnemy.hypnoTimeCounter = 0;
- }
- }
- public function TryLevitating()
- {
- var dist = this.facing == FlxObject.LEFT ? -36 : 36;
- var rayLength = 0;
- var closestEnemy : Enemy = null;
- var closestHit : Int = 640;
- for ( enemy in level.enemyGroup )
- {
- if ( enemy.hypnoMode == HYPNOTIZED )
- {
- rayLength = this.HorizontalRayHit( enemy, dist );
- if ( rayLength > 0 )
- {
- if ( rayLength <= closestHit )
- {
- closestHit = rayLength;
- closestEnemy = enemy;
- }
- }
- }
- }
- if ( closestEnemy != null )
- {
- closestEnemy.SetHypnoMode( LEVITATE );
- carriedEnemy = closestEnemy;
- }
- }
- /*
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #if debug_draw
- if ( closestEnemy != null )
- {
- var end = dist < 0 ? ( this.getMidpoint().x - closestHit ) : ( this.getMidpoint().x + closestHit );
- var lineStyle : LineStyle = { color: FlxColor.GREEN, thickness: 1 };
- Reg.debugCanvas.drawLine( this.getMidpoint().x, this.getMidpoint().y, end, this.getMidpoint().y, lineStyle);
- }
- #end
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- */
- public function ControlPlayer( action : Bool, LEFT : Bool, RIGHT : Bool, JUMP : Bool, FALL : Bool )
- {
- actions[ "action" ] = action;
- actions[ "left" ] = LEFT;
- actions[ "right" ] = RIGHT;
- actions[ "jump" ] = JUMP;
- actions[ "fall" ] = FALL;
- }
- public function Animate()
- {
- this.bolt.visible = this.turnOnBolt;
- if ( this.hMotion == LEFT || this.hMotion == RIGHT )
- this.animation.play( "walking" );
- else
- this.animation.play( "idle_01" );
- if ( this.doAction )
- {
- if( this.actionCount > this.timeUntilRun )
- if( this.hMotion != IDLE )
- this.animation.play( "running" );
- if ( this.facing == FlxObject.RIGHT )
- {
- this.bolt.x = this.x + this.width;
- this.bolt.offset.x = 0;
- }
- else
- this.bolt.x = x - this.bolt.width;
- this.bolt.y = this.y + ( ( this.height - this.bolt.height ) / 4 );
- }
- if ( this.vMotion == DO_JUMP || this.vMotion == JUMP )
- this.animation.play( "jumping" );
- else
- if ( this.vMotion == FALL )
- this.animation.play( "dropping" );
- }
- public function CommitSuicide() : Void
- {
- if ( lives > 0 )
- {
- lives--;
- level.UpdateHud();
- level.reset = true;
- }
- else
- {
- level.SetGameOver( true );
- }
- }
- public function AddExtraLife() : Void
- {
- lives++;
- level.UpdateHud();
- }
- public function LoadSounds( soundList : String )
- {
- var ext : String = "ogg";
- #if flash
- ext = "mp3";
- #end
- // create an empty token array, this will store each sound name
- var tokens : Array< String > = [];
- tokens = soundList.split( ", " );
- for ( token in tokens )
- {
- sounds[ token ] = FlxG.sound.load( "assets/sounds/" + token + "." + ext );
- }
- }
- public function CreateBolt( boltData : Dynamic )
- {
- if ( level == null ) return;
- bolt = new GameObject( level, boltData );
- bolt.set_visible( false );
- bolt.x = this.x + this.width;
- bolt.y = this.y + ( ( this.height - bolt.height ) / 4 );
- }
- public override function Regen()
- {
- super.Regen();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement