
package  
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Spritemap;
	import net.flashpunk.FP;
	
	public class Heart extends Entity
	{
		
		[Embed(source = 'gfx/heart.png')]
		private const GFX2:Class;
		

		
		public var gfx:Spritemap;
		
		public var hp:Number = 0;
		
		public var vx:Number=0;
		public var vy:Number = 0;
		public var value:uint=10;
		
		public var max_vx:Number=5.0;
		public var max_vy:Number=10.0;
		public var vjump:Number = - 5.0;
		public var vjacc:Number = -1.0;
		public var vfall:Number = .6;
		public var vacc:Number = .6;
		public var max_midair_jumps:uint = 1;
		public var midair_jumps:uint = 0;
		public var collision_count:uint = 0;
		
		public var gravity:Boolean = false;
		
		public function Heart(xx:Number,yy:Number,value:uint=20) 
		{
	
			gfx = new Spritemap(GFX2, 16, 16);
			
			gfx.add("anim", [0, 1,2,1], 8+Math.random()*4, true);
			graphic = gfx;
			gfx.play("anim");
			setHitbox(16, 16);
			this.value = value;
			type = "coin";
			this.x = xx+8;
			this.y = yy+8;
			////trace(xx + " x "+ yy);
			
		}
		
		public function explode():void 
		{
			vx = -8 + (Math.random() * 16);
			vy = -8 + (Math.random() * 16);
		}
		
		override public function update():void 
		{
			if (!Bazaar.level.tilemap.inscreen(x, y)) return;
			//ai();
			//state = "wee";
			
			if (collide("player", x, y)) 
			{
				//trace(Bazaar.player.cash);
				//Bazaar.player.cash += value;
				FP.world.remove(this);
				Bazaar.emit("upgrade", 3, x, y);
				(FP.world as Bazaar).addHeart();
				return;
			}
			
			if (!gravity) 
			{
				if (collide("bullet", x, y))
				{
					explode();
					gravity = true;
				}
				return;
			}

				//motion = "idle";
				//vx *= .9;
			
			
			//if (vjump<.1) vx *= .9;

			if (!collide("level", x, y))
			{
				
				vy += vfall;
				
				
				if (collide("level", x, y + vy)) 
				{
					vy = 0;
					
					if (true)
					{
						vy = vjump;
						vjump *= .9;
						//jumphold = 15;
						
					}
				}
				
			} else {
				//move = -move;
				
				//trace("collision!");
				
				if (vy>=0)
				 {
					 //y += ((y-1) % Bazaar.level.tilemap.s);
					 vy = 0;
					 midair_jumps = 0;
					 
				 } else {
					 y +=  (y % Bazaar.level.tilemap.s)+1;
					 //y -= vy+1;
					 vy = .1;
					 
				 }
				 
				 vy = 0;
			}
			//if (vy>=max_vy) state = "woo";
			if (vy > max_vy) vy = max_vy;
			if (vx > max_vx) vx = max_vx;
			if (vx <- max_vx) vx = -max_vx;
			y += vy;
			x += vx;
			if (collide("level", x, y)) {
				x -= vx;
				vx = -vx;
				//y -= vy;
			}
			if (x < 0) x = 0;
			if (y < 0) y = 0;
			if (x > Bazaar.level.tilemap.s * (Bazaar.level.tilemap.w - 1)) x = Bazaar.level.tilemap.s * (Bazaar.level.tilemap.w - 1);
			if (y > Bazaar.level.tilemap.s * (Bazaar.level.tilemap.h - 1)) y = Bazaar.level.tilemap.s * (Bazaar.level.tilemap.h - 1);
			
			

			
		}
		
	}
}