LudumDare23 / src / Player.as

package
{
	import flash.geom.Point;
	import flash.ui.Keyboard;
	
	import org.flixel.*;
	import org.osflash.signals.Signal;

	public class Player extends WorldEntity
	{	
		public static const WALK_SPEED:Number = 0.4;
		public static const BULLET_SPEED:Number = 0.8;
		public static const DAMAGE_FROM_ENEMY:Number = 0.2;
		public static const INITIAL_HEALTH:Number = 1;
		
		public var shot:Signal = new Signal();
		public var dead:Signal = new Signal();
		
		override public function get groupName():String {
			return PlayState.GROUP_PLAYER;
		}
		
		public function Player(world:World) {
			super(world);
			
			var sprite:FlxSprite = new FlxSprite();
			sprite.loadGraphic(EmbeddedAssets.ImgPlayer, true, true, 16, 20);
			sprite.addAnimation("idle", [0]);
			sprite.addAnimation("walk", [0, 1, 2, 3], 8, true);
			sprite.addAnimation("jump", [4]);
			sprite.origin = new FlxPoint(0, sprite.height);
			_view = sprite;
			_view.health = INITIAL_HEALTH;
			
			FlxG.watch(this, "worldAngle", "player world angle");
			FlxG.watch(_view, "health", "player health");
		}
		
		override public function update():void {
			super.update();
			
			var anim:String = "idle";
			var sprite:FlxSprite = _view as FlxSprite;
			
			if (FlxG.keys.LEFT) {
				sprite.facing = FlxObject.LEFT;
				worldAngle -= WALK_SPEED * FlxG.elapsed;
				anim = "walk";
			} else if (FlxG.keys.RIGHT) {
				sprite.facing = FlxObject.RIGHT;
				worldAngle += WALK_SPEED * FlxG.elapsed;
				anim = "walk";
			}
			if (FlxG.keys.justPressed("UP")) {
				if (!isJumping) FlxG.play(EmbeddedAssets.SndJump);
				jump();
			}
			if (FlxG.keys.justPressed("SPACE"))
				shoot();
			
			if (isJumping) anim = "jump";
			
			sprite.play(anim);
		}
		
		override public function collided(other:Entity):void {
			if (other is Enemy && !_view.flickering) {
				_view.health -= DAMAGE_FROM_ENEMY;
				_view.flicker();
				
				if (_view.health < 0) {
					addParticleFX(new Explosion(midPoint.x, midPoint.y, EmbeddedAssets.ImgBlood, new Point(-20, -20), new Point(20, 20)));
					dead.dispatch();
				}
			}
		}
		
		private function shoot():void {
			const facingRight:Boolean = (view as FlxSprite).facing == FlxObject.RIGHT;
			var bullet:Bullet = new Bullet(_world);
			bullet.worldAngle = worldAngle + (facingRight ? 0.04 : -0.01);
			bullet.worldHeight = worldHeight + 14 + Math.random() * 2;
			bullet.speed = facingRight ? BULLET_SPEED : -BULLET_SPEED;
			addEntity(bullet);
			
			shot.dispatch();
			
			FlxG.play(EmbeddedAssets.SndShoot);
		}
	}
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.