package  
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author beisik
	 */
	public class Snake extends Sprite 
	{
		[Embed(source = 'textures/3.png')] private const HEAD1: Class;
		[Embed(source = 'textures/4.png')] private const HEAD2: Class;
		[Embed(source = 'textures/5.png')] private const HEAD3: Class;
		[Embed(source = 'textures/6.png')] private const HEAD4: Class;
		[Embed(source = 'textures/bubble.png')] private const BUBBLE: Class;
		
		public static var speed: Number = 70;
		public static var radius: Number = 10;
		
		public var shadow: Shadow;
		
		private var images: Vector.<Bitmap>;
		
		private var lastImg: uint;
		
		public var son: Tail;
		
		public var life: int;
		
		public var kickVelocity: Point;
		public var invulnerable: Boolean;
		public var invulnerableTime: Number;
		
		public var whiteCircle: Sprite;
		public var blackCircle: Sprite;
		
		public function Snake() 
		{
			x = 300;
			y = 200;
			
			rotation = -90;
			
			blackCircle = new Sprite();
			blackCircle.graphics.beginFill(0);
			blackCircle.graphics.drawCircle(-2, 0, radius * 1.8);
			blackCircle.graphics.endFill();
			addChild(blackCircle);
			blackCircle.visible = true;
			
			images = new Vector.<Bitmap>(4);
			images[0] = new HEAD1();
			images[1] = new HEAD2();
			images[2] = new HEAD3();
			images[3] = new HEAD4();
			for (var i: uint = 0; i < 4; i++)
			{
				images[i].x = -25;
				images[i].y = -25;
				images[i].smoothing = true;
				images[i].visible = false;
				addChild(images[i]);
			}
			images[3].visible = true;
			lastImg = 3;
			
			shadow = new Shadow();
			shadow.move(x, y);
			
			son = null;
			
			life = 5;
			
			kickVelocity = new Point();
			invulnerable = false;
			invulnerableTime = 0;
			
			whiteCircle = new Sprite();
			/*whiteCircle.graphics.beginFill(0xFFFFFF, 0.5);
			whiteCircle.graphics.drawCircle(0, 0, radius * 2);
			whiteCircle.graphics.endFill();*/
			var img: Bitmap = new BUBBLE();
			img.x = -img.width / 2;
			img.y = -img.height / 2;
			img.smoothing = true;
			whiteCircle.addChild(img);
			addChild(whiteCircle);
			whiteCircle.visible = false;
			
			// баудинг бокс
			/*graphics.lineStyle(2, 0xFFFFFF);
			graphics.beginFill(0, 0.5);
			graphics.drawCircle(0, 0, radius);
			graphics.endFill();*/
		}
		
		public function Update(dt: Number): void
		{
			x += Math.cos(rotation * Math.PI / 180) * speed * dt + kickVelocity.x * dt;
			y += Math.sin(rotation * Math.PI / 180) * speed * dt + kickVelocity.y * dt;
			
			kickVelocity.x = Global.Aspire(kickVelocity.x, 0, 2 * speed * dt);
			kickVelocity.y = Global.Aspire(kickVelocity.y, 0, 2 * speed * dt);
			//if (kickVelocity.x != 0 || kickVelocity.y != 0)
			if (invulnerableTime <= 0)
				invulnerable = false;
			else
			{
				invulnerable = true;
				invulnerableTime -= dt;
			}
			// моргание
			if (invulnerable)
				whiteCircle.visible = true;
			else
				whiteCircle.visible = false;
			
			/*if (Keys.Down(Keys.KEY_D))
				rotation = Global.AspireDegrees(rotation, 0, 90 * dt);
			if (Keys.Down(Keys.KEY_W))
				rotation = Global.AspireDegrees(rotation, -90, 90 * dt);
			if (Keys.Down(Keys.KEY_A))
				rotation = Global.AspireDegrees(rotation, -180, 90 * dt);
			if (Keys.Down(Keys.KEY_S))
				rotation = Global.AspireDegrees(rotation, 90, 90 * dt);*/
			if (Keys.Down(Keys.KEY_D) || Keys.Down(Keys.KEY_RIGHT))
				rotation += 180 * Math.min(speed / 60, 1.5) * dt;
			if (Keys.Down(Keys.KEY_A) || Keys.Down(Keys.KEY_LEFT))
				rotation -= 180 * Math.min(speed / 60, 1.5) * dt;
				
			// картинка головы
			var currentImg: uint;
			if (rotation >= -45 && rotation <= 45)
				currentImg = 1;
			else
				if (rotation >= -135 && rotation < -45)
					currentImg = 3;
				else
					if (rotation > 45 && rotation <= 135)
						currentImg = 0;
					else
						currentImg = 2;
			if (lastImg != currentImg)
			{
				images[currentImg].visible = true;
				images[lastImg].visible = false;
				lastImg = currentImg;
			}
			
			if (son == null)
			{
				if (!blackCircle.visible)
					blackCircle.visible = true;
			}
			else
			{
				if (blackCircle.visible)
					blackCircle.visible = false;
			}
				
			shadow.move(x, y);
		}
		
	}

}