package  
{
	import flash.geom.Point;
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;
	import net.flashpunk.FP;
	
	public class Hero extends Entity
	{
		public var speed:Number=5;
		public var xvel:Number=0;
		public var yvel:Number=0;
		public var acc:Number = .1;
		public var hp:Number = 100;
		public var legs:Image;
		public var playerimage:Image;
		public var collides:Boolean = false;
		public var eggs:Array;
		public var selegg:uint;
		
		
		[Embed(source = 'assets/player.png')]
		private const PLAYER:Class;
		
		[Embed(source = 'assets/legs.png')]
		private const LEGS:Class;
		
		public function Hero() 
		{
			
			eggs = new Array();
			for (var c:Number = 1; c <= 6; c++)
			{
				eggs[c] = 0;
			}
			eggs[1] = 1;
			selegg = 1;
			layer = 1;
			playerimage  = new Image(PLAYER);
			graphic = playerimage;
			legs = new Image(LEGS);
			legs.originX = 8;
			legs.originY = 8;
			Input.define("Shoot", Key.SPACE, Key.X, Key.C);
			Input.define("Jump", Key.Z);
			setHitbox(31, 31, 0, 0);
		}
		override public function update():void 
		{
			var b:Entity = collide("enemy", x, y)
			var nme:Enemy = b as Enemy;
			if (b && !collides)
			{
				collides = true;
				xvel = ( -xvel + nme.xvel);
				yvel = ( -yvel + nme.yvel);
				(FP.world as MyWorld).wings.emit("feath", x + (playerimage.width / 2), y + (playerimage.height / 2));
				(FP.world as MyWorld).dust.emit("dust", x , y);
				hp -= nme.props.atk;
				//(FP.world as MyWorld).wings.emit("feath", x + (playerimage.width / 2), y + (props.image.height / 2));
				
			} else 
			{
				collides = false;
			}
			
			if (Input.check(Key.LEFT) && xvel>-speed)
			{
				xvel -= acc;
				playerimage.flipped = true;
				legs.flipped = true;
			} else if (Input.check(Key.RIGHT) && xvel<speed)
			{
				xvel += acc;
				playerimage.flipped = false;
				legs.flipped = false;
			} else xvel *= .95;
			

				yvel += Main.Gravity;
		
			
			if (!collide("level",x,y+yvel))
			{
				y += yvel;
			} else yvel=0;
			
			if (!collide("level",x+xvel,y))
			{
				x += xvel;
			} else xvel = 0;
			
			legs.angle -= xvel*4;
			
/*
			if (Input.check(Key.UP) && !collide("level",x,y-speed))
			{
				y -= speed;
			}
			*/
		
				var d:Entity;
				d = collide("hive", x, y);
				if (d) 
				{
					FP.world.remove(d);
					
				}
				d = collide("egg", x, y);
				if (d)
				{
					trace("egg collision");
					var e:Egg = d as Egg;
				  if (eggs[e.mark] < 9)
					{
					eggs[e.mark]++;
					FP.world.remove(d);
					}
				}
			if (Input.pressed(Key.DIGIT_1)) selegg = 1;
			if (Input.pressed(Key.DIGIT_2)) selegg = 2;
			if (Input.pressed(Key.DIGIT_3)) selegg = 3;
			if (Input.pressed(Key.DIGIT_4)) selegg = 4;
			if (Input.pressed(Key.DIGIT_5)) selegg = 5;
			if (Input.pressed(Key.DIGIT_6)) selegg = 6;
			

			if (Input.pressed(Key.SPACE) && eggs[selegg]) { FP.world.add(new Enemy(selegg, x, y - 40, 12 * (playerimage.flipped ? -1 : 1), true)); eggs[selegg]--;}
			
			
			if (Input.check("Jump") && yvel == 0 )
			{
				// One (or several) of the "Shoot" keys is being held down.
				yvel = -8;
			}
			
			if (hp <= 0)
			{
			  x = (FP.world as MyWorld).spawnx;
				y = (FP.world as MyWorld).spawny;
				hp = 100;
			
			}
			
			FP.camera.x = x - (FP.screen.width/2);
			FP.camera.y = y - (FP.screen.height / 2);
			if (FP.camera.x < 0) FP.camera.x = 0;
			if (FP.camera.y < 0) FP.camera.y = 0;
			
			if (FP.camera.x >  (FP.world as MyWorld).level.tilemap.w * (FP.world as MyWorld).level.tilemap.s - (FP.screen.width))
			    FP.camera.x =  (FP.world as MyWorld).level.tilemap.w * (FP.world as MyWorld).level.tilemap.s - (FP.screen.width);
			if (FP.camera.y >  (FP.world as MyWorld).level.tilemap.h * (FP.world as MyWorld).level.tilemap.s - (FP.screen.height))
			    FP.camera.y =  (FP.world as MyWorld).level.tilemap.h * (FP.world as MyWorld).level.tilemap.s - (FP.screen.height);
			
			 
		}
		
		override public function render():void 
		{
			
			var l_point:Point = new Point;
			var l_camera:Point = new Point;
				l_point.x = x + (legs.flipped ? 12 : 4);
				l_point.y = y + 16;
				l_camera.x = FP.camera.x;
				l_camera.y = FP.camera.y;
				legs.render(l_point, l_camera);
				y -= 8;
				super.render();
				y += 8;
		}
		
	}
}
