
package  
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Emitter;
	import net.flashpunk.graphics.Spritemap;
	import net.flashpunk.graphics.Text;
	import net.flashpunk.World;
	import net.flashpunk.Sfx;
	
	public class Bazaar extends World
	{
		[Embed(source = 'gfx/particles.png')]
		private const PARTS:Class;
		
		[Embed(source = 'gfx/heart.png')]
		private const HEART:Class;
		
		[Embed(source = 'gfx/coin.png')]
		private const COIN:Class;
		
		[Embed(source = 'gfx/ld20.mp3')]
		private const MUSIC:Class;
		
		public var shoot:Sfx = new Sfx(MUSIC);
		
		[Embed(source = 'gfx/over.mp3')]
		private const OVER:Class;
		
		public var over:Sfx = new Sfx(OVER);
		
		public static var hearts:Vector.<Entity> = new Vector.<Entity>();
		
		public static var player:Peddler;
		public static var level:BazaarLevel;
		public static var parter:Emitter;
		public var txt:Text;
		public function Bazaar() 
		{
			
		}
		override public function begin():void 
		{
			shoot.loop();
			player = new Peddler;
			level = new BazaarLevel;
			
			add(player);
			add(level);
			parter =   new Emitter(PARTS, 32, 32);
			
			parter.newType("blood", [0]);
			parter.setAlpha("blood", 1, 0);
			parter.setMotion("blood", 0, 100, .5, 360, 50, 0);
			
			parter.newType("cash", [1]);
			parter.setAlpha("cash", 1, 0);
			parter.setMotion("cash", 0, 100, 1, 360, 50, 0);
			
			parter.newType("upgrade", [2]);
			parter.setAlpha("upgrade", 1, 0);
			parter.setMotion("upgrade", 0, 300, 2, 360, 200, 1);
			
			parter.newType("boom", [3]);
			parter.setAlpha("boom", .6, 0);
			parter.setMotion("boom", 0, 100, .5, 360, 50, 0);
			
			addGraphic(parter);
			
			for (var c:int = 0; c < 3; c++)
			{
				addHeart();
				
			}
			
			var h:Spritemap = new Spritemap(COIN, 16, 16);
				h.add("wave", [0, 1], 8+Math.random()*4, true);
				h.play("wave");
				var e:Entity = new Entity(4, 40, h);
				h.scrollX = 0;
				h.scrollY = 0;
				add(e);
				txt = new Text(player.cash + " ", 0, 0, 600, 60);
				txt.scrollX = 0;
				txt.scrollY = 0;
				addGraphic(txt,-3,20,40);
				
			
		}
		
		public static function emit(type:String, amount:uint, x:Number, y:Number):void
		{
			for (var c:uint = 0; c < amount; c++) parter.emit(type, x, y);
		}
		
		override public function update():void 
		{
			txt.text = player.cash + " ";
			super.update();
		}
		
		public function addHeart():void 
		{
				var h:Spritemap = new Spritemap(HEART, 16, 16);
				h.add("wave", [0, 1, 2,1], 8+Math.random()*4, true);
				h.play("wave");
				var e:Entity = new Entity(20 + 16 * hearts.length, 20, h);
				h.scrollX = 0;
				h.scrollY = 0;
				add(e);
				hearts.push(e);
			
		}
		
		public function removeHeart():void 
		{
			remove(hearts.pop());
			if (!hearts.length)
			{
				player.x = player.sx;
				player.y = player.sy;
				player.cash = 0;
				
				for (var c:int = 0; c < 3; c++)
			{
				addHeart();
				
				
			}
			over.play();
				
			}
		}

	}
}