package  
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author beisik
	 */
	public class Saw extends Sprite 
	{
		[Embed(source = 'textures/s1.png')] private const SAW1: Class;
		[Embed(source = 'textures/s2.png')] private const SAW2: Class;
		[Embed(source = 'textures/s3.png')] private const SAW3: Class;
		[Embed(source = 'textures/s4.png')] private const SAW4: Class;
		
		public var updown: Boolean;
		
		public var direction: Boolean;
		
		public var speed: Number;
		
		public var images: Vector.<Bitmap>;
		
		private var frame: uint;
		private var timeStep: Number;
		
		public var shadow: Shadow;
		
		public function Saw(x: Number, y: Number, updown: Boolean, direction: Boolean) 
		{
			this.x = x;
			this.y = y;
			this.updown = updown;
			
			this.direction = direction;
			
			speed = 40;
			
			if (!updown)
				rotation = 90;
			
			images = new Vector.<Bitmap>(4);
			images[0] = new SAW1();
			images[1] = new SAW2();
			images[2] = new SAW3();
			images[3] = new SAW4();
			for (var i: uint = 0; i < 4; i++)
			{
				images[i].x = -30;
				images[i].y = -90;
				images[i].visible = false;
				addChild(images[i]);
			}
			
			frame = 0;
			images[frame].visible = true;
			
			timeStep = 0;
			
			shadow = new Shadow(2);
			if (!updown)
				shadow.rotation = rotation;
			shadow.move(x, y);
			
			// баудинг бокс
			/*graphics.lineStyle(2, 0xFFFFFF);
			graphics.beginFill(0, 0.5);
			graphics.drawRect( -20, -70, 40, 140);
			graphics.endFill();*/
		}
		
		public function Update(dt: Number): void
		{
			if (updown)
			{
				if (y <= 90 + Game(parent.parent).level.y)
					direction = true;
				if (y >= Game.Height - 90 + Game(parent.parent).level.y)
					direction = false;
				if (direction)
					y += speed * dt;
				else
					y -= speed * dt;
			}
			else
			{
				if (x <= 90 + Game(parent.parent).level.x)
					direction = true;
				if (x >= Game.Width - 90 + Game(parent.parent).level.x)
					direction = false;
				if (direction)
					x += speed * dt;
				else
					x -= speed * dt;
			}
			
			timeStep += dt;
			if (timeStep >= 0.025)
			{
				var lastFrame: uint = frame;
				frame++;
				if (frame == 4)
					frame = 0;
				images[frame].visible = true;
				images[lastFrame].visible = false;
				timeStep = 0;
			}
			
			shadow.move(x, y);
		}
		
	}

}