package  
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author beisik
	 */
	public class Tail extends Sprite 
	{
		[Embed(source = 'textures/1.png')] private const TAIL: Class;
		[Embed(source = 'textures/2.png')] private const PART: Class;
		
		public var head: Sprite;
		
		private var velocity: Point;
		
		public var imgs: Vector.<Bitmap>;
		
		public var shadow: Shadow;
		
		public var son: Tail;
		
		public function Tail(parent: Sprite) 
		{
			head = parent;
			son = null;
			if (parent is Tail)
				Tail(parent).son = this;
			else
				Snake(parent).son = this;
			
			rotation = parent.rotation;
			velocity = new Point();
			
			x = parent.x;
			y = parent.y;
			
			imgs = new Vector.<Bitmap>(2);
			imgs[0] = new TAIL();
			imgs[0].x = -25;
			imgs[0].y = -25;
			imgs[0].smoothing = true;
			imgs[0].visible = true;
			addChild(imgs[0]);
			imgs[1] = new PART();
			imgs[1].x = -25;
			imgs[1].y = -25;
			imgs[1].smoothing = true;
			imgs[1].visible = false;
			addChild(imgs[1]);
			
			if (parent is Tail)
			{
				Tail(parent).imgs[1].visible = true;
				Tail(parent).imgs[0].visible = false;
			}
			
			shadow = new Shadow();
			shadow.move(x, y);
			
			// баудинг бокс
			/*graphics.lineStyle(2, 0xFFFFFF);
			graphics.beginFill(0, 0.5);
			graphics.drawCircle(0, 0, Snake.radius);
			graphics.endFill();*/
		}
		
		public function Update(dt: Number): void
		{
			var tx: Number = head.x - x;
			var ty: Number = head.y - y;
			var tl: Number = Global.Distance(head.x, head.y, x, y);
			if (tl >= Snake.radius * 2)
			{
				velocity.x = tx / tl;
				velocity.y = ty / tl;
			}
			x += velocity.x * Snake.speed * dt;
			y += velocity.y * Snake.speed * dt;
			
			var angle: Number = Global.Angle2D(x, y, head.x, head.y) * 180 / Math.PI + 180;
			angle = Global.normalDegrees(angle);
			rotation = Global.AspireDegrees(rotation, angle, 180 * Snake.speed / 60 * dt);
			
			shadow.move(x, y);
		}
		
	}

}