package  
{
	import flash.display.BitmapData;
	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;
	import net.flashpunk.Sfx;
	import net.flashpunk.graphics.Spritemap

	public class Fish extends Entity 
	{
		/**********************************************************************************/
		private var power:Number=0.2;
		private var jumpPower:Number=10;
		private var hFriction:Number=0.95;
		private var vFriction:Number=0.99;
		private var xSpeed:Number=0;
		private var ySpeed:Number=0;
		private var gravity:Number = 0.3;
		private var _v:Point; 
		internal var pressed:Boolean = false;
		internal var direction:String;
		internal var number:int;
		internal var shooting:Boolean = false;
		
		/*fish graphics*/
		[Embed(source = '../assets/gfx/fish frames.png')] private const FISH:Class;
		public var image:Image;		
				
		public var fishSprite:Spritemap = new Spritemap(FISH, 48, 48);
		
		/*fish sound effects*/
		[Embed(source = '../assets/sfx/charlieHaloShoot.mp3')]		
		private const HALO_SOUND:Class;
		public var Halo:Sfx = new Sfx(HALO_SOUND);
		
		[Embed(source = '../assets/sfx/charlieHit.mp3')]		
		private const HIT:Class;
		public var hitSound:Sfx = new Sfx(HIT);
		/**********************************************************************************/		
		public function Fish(p:Point, d:String, pl:int)
		{
			x = p.x;
			y = p.y; 
			direction = d;
			number = pl;
			_v = new Point; 
			type = "player";
			
			fishSprite.add("turnR", [6, 3], 20, false);
			fishSprite.add("turnL", [6, 0], 20, false);			
			fishSprite.add("faceR", [3, 4, 5, 4],15, true);
			fishSprite.add("faceL", [0, 1, 2, 1], 15, true);	
			fishSprite.add("shootR", [3,4,5,6,5,4,3], 30, false);
			fishSprite.add("shootL", [3, 2, 1, 0], 30, false);	
			fishSprite.add("hit", [7], 30, false);
			fishSprite.play(direction);
			graphic = fishSprite;
			setHitbox(47, 40, 0, 0);
			type = "player";
		}
		/**********************************************************************************/
		override public function update():void
		{
			updateMovement(); 
			updateAtck();
			updateHit();
			super.update();
		}
		/**********************************************************************************/
		private function updateMovement():void 
		{
			//player 2 controls
			if (number != 1)
			{
				//move left
				if (Input.check(Key.LEFT)) 
				{
					if (direction == "faceR")
					{
						fishSprite.play("turnL");
						direction = "faceL";
					}
					else
					{
						fishSprite.play("faceL");
					}
					if (!collide("level", x - 1.5, y))
					{
						x -= 1.5;
					}
				}
				//move right
				if (Input.check(Key.RIGHT)) 
				{
					if (direction == "faceL")
					{
						fishSprite.play("turnR");
						direction = "faceR";
					}
					else
					{
						fishSprite.play("faceR");
					}
					if (!collide("level", x + 1.5, y))
					{
						x += 1.5;
					}
				}
				//move up
				if (Input.check(Key.UP)) 
				{
					if (!collide("level", x, y - 1.5))
					{
						y -= 1.5;
					}
				}
				//move down
				if (Input.check(Key.DOWN))
				{
					if (!collide("level", x, y + 1.5))
					{
						y += 1.5;
					}
				}
			}
			else//player 1 controls
			{
				//move left
				if (Input.check(Key.A)) 
				{
					if (direction == "faceR")
					{
						fishSprite.play("turnL");
						direction = "faceL";
					}
					else
					{
						fishSprite.play("faceL");
					}
					if (!collide("level", x - 1.5, y))
					{
						x -= 1.5;
					}
				}
				//move right
				if (Input.check(Key.D)) 
				{
					if (direction == "faceL")
					{
						fishSprite.play("turnR");
						direction = "faceR";
					}
					else
					{
						fishSprite.play("faceR");
					}
					if (!collide("level", x + 1.5, y))
					{
						x += 1.5;
					}
				}
				//move up
				if (Input.check(Key.W)) 
				{
					if (!collide("level", x, y - 1.5))
					{
						y -= 1.5;
					}
				}
				//move down
				if (Input.check(Key.S))
				{
					if (!collide("level", x, y + 1.5))
					{
						y += 1.5;
					}
				}
			}//end player 1 controls					
		}	
		/**********************************************************************************/   
		private function updateAtck():void 
		{
			//player 2 attack controls
			if (number != 1)
			{
				if (Input.pressed(Key.P))
				{
					if (GameWorld.haloNum < 10)
					{
						shooting = true;
						Halo.play();  // Play the sound when the spacebar is pressed.
					}
				}
				else
				{
					shooting = false;
				}
			}
			else//player 1 attack controls
			{
				if (Input.pressed(Key.V))
				{
					if (GameWorld.haloNum < 10)
					{
						shooting = true;
						//haloSprite.play("shootR", true);
						Halo.play();  // Play the sound when the spacebar is pressed.
					}
				}
				else
				{
					shooting = false;
				}
			}
		}		
		/**********************************************************************************/ 
		private function updateHit():void
		{
			if (GameWorld.fIsHit)
			{
				fishSprite.play("hit");
				GameWorld.fIsHit = false;
				hitSound.play();
			}
		}
	}

}