
package  
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Spritemap;
	import net.flashpunk.FP;
	
	public class Turtle extends Enemy
	{
		
		[Embed(source = 'gfx/turtle.png')]
		private const GFX:Class;
		
		public var gfx:Spritemap = new Spritemap(GFX, 32, 32);
		
		//public var hp:Number = 0;
		
		public var vx:Number=0;
		public var vy:Number=0;
		
		public var max_vx:Number=2.0;
		public var max_vy:Number=10.0;
		public var vjump:Number = - 15.0;
		public var vjacc:Number = -1.0;
		public var vfall:Number = .6;
		public var vacc:Number = .1;
		public var max_midair_jumps:uint = 1;
		public var midair_jumps:uint = 0;
		public var collision_count:uint = 0;
		
		
		public var jumpism:uint = 0;
		public var iq:uint = 10;
		public var brain:uint = 1;
		
		public var jump:uint = 0;
		
		public var move:int = 0;
		
		public function Turtle(xx:Number,yy:Number) 
		{
			gfx.add("anim", [0, 1], 11, true);
			graphic = gfx;
			gfx.play("anim");
			setHitbox(32, 32);
			type = "enemy";
			this.x = xx;
			this.y = yy;
			////trace(xx + " x "+ yy);
			hp = 500;
			
		}
		
		public function ai():void 
		{
			brain--;
			if (jump) jump--;
			
			if (!brain) 
			{
				
				jump = Math.random() * jumpism;
				if (jump < 20) jump = 0;
				move = ((Math.random() > .5) ? 1 : -1);
				////trace(move);
				brain = iq + Math.random() * iq;
			}
		}
		
		override public function update():void 
		{
			if (!Bazaar.level.tilemap.inscreen(x, y)) return;
			ai();
			//state = "wee";
			if (move==1)
			{
				vx -= vacc;
				gfx.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 (move==-1)
			{
				vx += vacc;
				gfx.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 (jump)
				{
					vy = vjump;
					midair_jumps--;
				}
			}

			if (!collide("level", x, y))
			{
				
				vy += vfall;
				
				if (collide("level", x, y + vy)) 
				{
					vy = 0;
					midair_jumps = 0;
					if (jump)
					{
						vy = vjump;
						//jumphold = 15;
						midair_jumps = max_midair_jumps;
					}
				}
				
			} 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;
				//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);
			
			

			
		}
		
	}
}