
package  
{
	import flash.geom.Point;
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.graphics.Spritemap;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Text;
	import net.flashpunk.Sfx;
	public class Grandpa extends Entity
	{
		
		[Embed(source = 'gfx/grandpa.png')]
		private const GFX:Class;
		
		[Embed(source = 'gfx/txtbox.png')]
		private const TXT:Class;
		
		public var gfx:Spritemap = new Spritemap(GFX, 32,32);
		
		public var txt:Text;
		public var box:Image = new Image(TXT);
		
		public var upgrade:uint = 0;
		public var price:uint = 0;
		public var name:String = "";
		public var desc:String = "";
		public var bought:Boolean = false;
		
		[Embed(source = 'gfx/holler.mp3')]
		private const SND:Class;
		public var shoot:Sfx = new Sfx(SND);
		
		[Embed(source = 'gfx/rocks.mp3')]
		private const SND1:Class;
		public var rocks:Sfx = new Sfx(SND1);
		
		[Embed(source = 'gfx/gun.mp3')]
		private const SND11:Class;
		public var gun:Sfx = new Sfx(SND11);
		
		[Embed(source = 'gfx/shotgun.mp3')]
		private const SND12:Class;
		public var shotgun:Sfx = new Sfx(SND12);
		
		[Embed(source = 'gfx/minigun.mp3')]
		private const SND13:Class;
		public var minigun:Sfx = new Sfx(SND13);
		
		[Embed(source = 'gfx/flamethrower.mp3')]
		private const SND14:Class;
		public var flamethrower:Sfx = new Sfx(SND14);
		
		[Embed(source = 'gfx/panzerfaust.mp3')]
		private const SND15:Class;
		public var panzerfaust:Sfx = new Sfx(SND15);
		
		[Embed(source = 'gfx/rocketlauncher.mp3')]
		private const SND16:Class;
		public var rocketlauncher:Sfx = new Sfx(SND16);
		
		[Embed(source = 'gfx/bouncer.mp3')]
		private const SND17:Class;
		public var bouncer:Sfx = new Sfx(SND17);
		
		[Embed(source = 'gfx/laser.mp3')]
		private const SND18:Class;
		public var laser:Sfx = new Sfx(SND18);
		
		[Embed(source = 'gfx/nuke.mp3')]
		private const SND19:Class;
		public var nuke:Sfx = new Sfx(SND19);
		
		[Embed(source = 'gfx/dbljump.mp3')]
		private const SND111:Class;
		public var dbljump:Sfx = new Sfx(SND111);
		
		[Embed(source = 'gfx/infjump.mp3')]
		private const SND112:Class;
		public var infjump:Sfx = new Sfx(SND112);
		
		
		public var gravity:Boolean = false;
		
		public function Grandpa(xx:Number,yy:Number,weapon:uint) 
		{
			gfx.add("idle", [0, 1], 12, true);
			gfx.add("talk", [2, 3], 12, true);
			graphic = gfx;
			gfx.play("idle");
			setHitbox(32, 32);
			type = "grandpa";
			upgrade = weapon;
			x = xx;
			y = yy;
			if (upgrade == 0) 
			{
				price = 100;
				name = "rock";
				desc = "You can throw it and stuff!";
			} else if (upgrade == 1) 
			{
				price = 250;
				name = "pistol";
				desc = "You can shoot cats with that!";
			}else if (upgrade == 2) 
			{
				price = 500;
				name = "shotgun";
				desc = "You can hunt elephants with that!";
			}else if (upgrade == 3) 
			{
				price = 1000;
				name = "minigun";
				desc = "You can hunt predators with that!";
			}else if (upgrade == 4) 
			{
				price = 2000;
				name = "flamethrower";
				desc = "Good choice for a pleasant bbq!";
			}else if (upgrade == 5) 
			{
				price = 1500;
				name = "panzerfaust";
				desc = "It can break some walls!";
			}else if (upgrade == 6) 
			{
				price = 4000;
				name = "missile Launcher";
				desc = "MAANTRAAA MAGIC!";
			}else if (upgrade == 7) 
			{
				price = 5555;
				name = "bouncy thingie";
				desc = "It's a thingie, and it's bouncy!";
			}else if (upgrade == 8) 
			{
				price = 8000;
				name = "lazor";
				desc = "This one can break\nthe fourth wall!";
			}else if (upgrade == 9) 
			{
				price = 12000;
				name = "nuke";
				desc = "Uhm... you know what that does?";
			}else if (upgrade == 10) 
			{
				price = 333;
				name = "double jump";
				desc = "There's a free cake with it!";
			}else if (upgrade == 11) 
			{
				price = 10000;
				name = "4294967295-iple jump";
				desc = "You can jump -1 times in midair!";
			}else if (upgrade == 12 )
			{
				price = 666;
				name = "stat boost";
				desc = "It's kind of illegal...";
			}
			trace("It's dangerous to \n go alone! Take this " + name + " for only $" + price + " !!!\n " + desc);
			txt  = new Text("It's dangerous to go alone!\nTake this " + name + "\nIt's only $" + price + " !!!\n\n" + desc
			, -128, -96, 32 * 9, 32 * 3);
			
			FP.world.addGraphic(box, 0, x-6-128, y-4-96);
			FP.world.addGraphic(txt, 0, x, y);
			
			
			////trace(xx + " x "+ yy);
			
		}
		

		
		override public function update():void 
		{
			//trace("aaa");
			//if (!Bazaar.level.tilemap.inscreen(x, y)) return;
			//ai();
			//state = "wee";
			if (distanceFrom(Bazaar.player) < 200 && !bought)
			{
				gfx.play("talk");
				if (txt.alpha < .05) shoot.play();
				box.alpha += .1;
				if (box.alpha > 1) box.alpha = 1;
				txt.alpha += .1;
				if (txt.alpha > 1) txt.alpha = 1;
				
			} else {
				gfx.play("idle");
				box.alpha -= .1;
				if (box.alpha < 0) box.alpha = 0;
				txt.alpha -= .1;
				if (txt.alpha < 0) txt.alpha = 0;
			}
			
			
			if (collide("player", x, y)) {
				if (Bazaar.player.cash >= price)
				{
					bought = true;
					Bazaar.emit("upgrade", 15, Bazaar.player.x, Bazaar.player.y);
					Bazaar.player.cash -= price;
					
					if (upgrade == 0)
					{
						Bazaar.player.weapons.push(new Rocks);
						rocks.play();
					} else if (upgrade == 1)
					{
						Bazaar.player.weapons.push(new Gun);
						gun.play();
					} else if (upgrade == 2)
					{
						Bazaar.player.weapons.push(new Shotgun);
						shotgun.play();
					} else if (upgrade == 3)
					{
						Bazaar.player.weapons.push(new MiniGun);
						minigun.play();
					} else if (upgrade == 4)
					{
						Bazaar.player.weapons.push(new Flamethrower);
						flamethrower.play();
					} else if (upgrade == 5)
					{
						Bazaar.player.weapons.push(new Panzerfaust);
						panzerfaust.play();
					} else if (upgrade == 6)
					{
						Bazaar.player.weapons.push(new RocketLauncher);
						rocketlauncher.play();
					} else if (upgrade == 7)
					{
						Bazaar.player.weapons.push(new Bouncer);
						bouncer.play();
					} else if (upgrade == 8)
					{
						Bazaar.player.weapons.push(new Lazor);
						laser.play();
					} else if (upgrade == 9)
					{
						Bazaar.player.weapons.push(new Nukelauncher);
						nuke.play();
					} else if (upgrade == 10)
					{
						Bazaar.player.max_midair_jumps = 1;
						dbljump.play();
					} else if (upgrade == 11)
					{
						Bazaar.player.max_midair_jumps = -1;
						infjump.play();
					} else if (upgrade == 12)
					{
						// ???
					} 
					FP.world.remove(this);
						box.alpha = 0;
						txt.alpha = 0;
				}
			}
			
			
			

			
		}
		

		
		
	}
}