
package  
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Spritemap;
		import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;
	import net.flashpunk.FP;
	import net.flashpunk.Sfx;
		import net.flashpunk.graphics.Image;

	import net.flashpunk.graphics.Text;
	
	public class Peddler extends Entity
	{
		[Embed(source = 'gfx/pedd.png')]
		private const PEDD:Class;
		
		[Embed(source = 'gfx/piu.mp3')]
		private const SND1:Class;
		
		[Embed(source = 'gfx/ouch.mp3')]
		private const SND2:Class;
		
		[Embed(source = 'gfx/hop.mp3')]
		private const SND3:Class;
		
		[Embed(source = 'gfx/txtbox.png')]
		private const TXT:Class;
		
		public var txt:Text;
		public var box:Image = new Image(TXT);
		
		public var shoot:Sfx = new Sfx(SND1);
		public var hit:Sfx = new Sfx(SND2);
		public var hop:Sfx = new Sfx(SND3);
		
		public var state:String;
		public var motion:String;
		
		public var pedd:Spritemap = new Spritemap(PEDD, 32, 32);
		public var vx:Number =0;
		public var vy:Number = 0;
		
		public var sx:Number =0;
		public var sy:Number = 0;
		
		public var hp:Number = 3;
		
		public var immune:uint=  0;
		
		public var camoffset_x:Number = 0.0;
		public var camoffset_y:Number=0.0;
		public var max_vx:Number=5.0;
		public var max_vy:Number=10.0;
		public var vjump:Number = - 15.0;
		public var vjacc:Number = -1.0;
		public var jumphold:uint=  0;
		public var vfall:Number = .6;
		public var vacc:Number = .6;
		public var max_midair_jumps:uint = 0;
		public var midair_jumps:uint = 0;
		public var collision_count:uint = 0;
		public var cash:uint = 0;
		public var woo:uint = 0;
		public var weapons:Vector.<Weapon> = new Vector.<Weapon>();
		
		public var start:Boolean = true;
		public var out:Boolean = false;
		public function Peddler() 
		{
			pedd.add("idle_wee", [5], 12, true);
			pedd.add("run_wee", [0,1], 12, true);
			
			pedd.add("idle_woo", [3], 12, true);
			pedd.add("run_woo", [2,3], 12, true);
			
			pedd.add("idle_takethis", [4,5], 12, true);
			pedd.add("run_takethis", [5, 6], 12, true);
			graphic = pedd;
			pedd.play("run_wee");
			setHitbox(32, 32);
			type = "player";
			motion = "idle";
			state = "wee";
			x = 10 * 32; y = 120 * 32;
			//weapons.push(new Shotgun);
			//weapons.push(new MiniGun);
			//weapons.push(new Gun);
			//weapons.push(new Rocks);
			//weapons.push(new Flamethrower);
			//weapons.push(new Panzerfaust);
			//weapons.push(new RocketLauncher);
			//weapons.push(new Bouncer);
			//weapons.push(new Lazor);
			//weapons.push(new Nukelauncher);
			
			
		}
		
		override public function update():void 
		{
			if (out)
			{
				if (box.alpha > 0) box.alpha -= .02;
				if (txt.alpha>0)txt.alpha -= .02;
			}
			if (start)
			{
				txt  = new Text("Press [ARROWS] to move\n[Z] to jump, [X] to shoot\nMission: Get teh nuke!\nWarning! Obtained weapons\ncan't be disabled!", -128, -96, 32 * 9, 32 * 3);
			
			FP.world.addGraphic(box, 0, x-6-128, y-4-96);
			FP.world.addGraphic(txt, 0, x, y);
			start = false;
			}
			state = "wee";
			if (Input.check(Key.LEFT))
			{
				out = true;
				vx -= vacc;
				pedd.flipped = true;
				motion = "run";
				if (collide("level", x+vx, y)) 
				{
					//x += Bazaar.level.tilemap.s - ((x-v) % Bazaar.level.tilemap.s);
					vx = 0;
				}
			} else if (Input.check(Key.RIGHT))
			{
				
				out = true;
				vx += vacc;
				pedd.flipped = false;
				motion = "run";
				if (collide("level", x+vx, y)) 
				{
					//x -= ((x+1) % Bazaar.level.tilemap.s);
					vx = 0;
				}
			} else 
			{
				motion = "idle";
				vx *= .5;
			}
			
			if (midair_jumps)
			{
				if (Input.pressed(Key.Z))
				{
					out = true;
					hop.play();
					vy = vjump;
					midair_jumps--;
				}
			}

			if (!collide("level", x, y))
			{
				
				vy += vfall;
				
				if (collide("level", x, y + vy)) 
				{
					vy = 0;
					midair_jumps = 0;
					if (Input.check(Key.Z))
					{
						out = true;
						hop.play();
						vy = vjump;
						jumphold = 15;
						midair_jumps = max_midair_jumps;
					}
				}
				
			} else {
				
				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;
				//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);
			
			
			var tempoffset_x:Number = vx * 10;
			var tempoffset_y:Number = vy * 10;
			camoffset_x = camoffset_x + (tempoffset_x - camoffset_x) * .03;
			camoffset_y = camoffset_y + (tempoffset_y - camoffset_y) * .03;
			FP.camera.x = x - (FP.screen.width/2) + camoffset_x;
			FP.camera.y = y - (FP.screen.height/2) + camoffset_y;
			if (FP.camera.x < 0) FP.camera.x = 0;
			if (FP.camera.y < 0) FP.camera.y = 0;
			if (FP.camera.x > Bazaar.level.tilemap.sw - FP.screen.width) FP.camera.x = Bazaar.level.tilemap.sw - FP.screen.width;
			if (FP.camera.y > Bazaar.level.tilemap.sh-FP.screen.height) FP.camera.y = Bazaar.level.tilemap.sh-FP.screen.height;
			//trace(x, y);
			
			if (Input.check(Key.X))
			{
				out = true;
				if (!shoot.playing) shoot.loop();
				for each (var wpn:Weapon in weapons)
				{
					wpn.shoot();
				}
			} else shoot.stop();
			
			for each (var wpn2:Weapon in weapons)
				{
					wpn2.update();
				}
			
			if (!immune)
			{
			var e:Enemy = collide("enemy", x, y)  as Enemy;
			if (e)
			{
				vx = e.x - x;
				vy = e.y - y;
				var d:Number = Math.sqrt(vx * vx + vy * vy);
				if (vx) vx /= d;
				if (vy) vy /= d;
				vx = vx < 0 ? -3 : 3;
				vy = vjump * .5;
				Bazaar.emit("blood", 1, x, y);
				woo = 30;
				(FP.world as Bazaar).removeHeart();
				immune = 120;
				hit.play();
			}
			} else {
				pedd.alpha = immune & 1;
				immune--;
			}
			if (woo) woo--;
			if (woo) state = "woo";
			pedd.play(motion + "_" + state);
		}
	}
}