package 
{
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.display.Bitmap;
	import flash.geom.Matrix;
	import flash.media.Sound;
	
	public class MapPart extends Sprite
	{
		public var radius : Number = 20;
		public var dead : Boolean;
		public var fade_out : Boolean;
		private var speed : Number;
		private var target : Point = new Point;
		public var extra : Boolean;
		private static var extra_counter : int = 7;
		
		[Embed(source = '../lib/ring.png')]
		public var bmp_ring_embed : Class;
		public var bmp_ring : Bitmap = new bmp_ring_embed() as Bitmap;
		
		[Embed(source = '../lib/ring_extra.png')]
		public var bmp_ring_extra_embed : Class;
		public var bmp_ring_extra : Bitmap = new bmp_ring_extra_embed() as Bitmap;
		
		[Embed(source="../lib/ring.mp3")]
        public var sound_ring_embed : Class;
		private var sound_ring : Sound = new sound_ring_embed() as Sound;
		
		public function MapPart():void {
			Draw();
			Reset();
		}
		
		private function Draw():void {
			var m : Matrix = new Matrix(); m.translate(20, 20);
			
			graphics.clear();
			if (extra)
				graphics.beginBitmapFill(bmp_ring_extra.bitmapData, m);
			else
				graphics.beginBitmapFill(bmp_ring.bitmapData, m);
			graphics.drawRect( -20, -20, 40, 40);
			graphics.endFill();	
			rotationZ = Math.random() * 360;
		}
		
		public function Reset():void {
			fade_out = dead = false;
			speed = 0;
			scaleX = scaleY = 0;
			
			extra = (extra_counter > 0) && (Global.RandomBySeed() > 0.91);
			if (extra) extra_counter--;
			Draw();
			
			do {
				var z : Number = 10 + radius;
				x = z + Global.RandomBySeed() * (Global.WIDTH - z * 2);
				y = z + Global.RandomBySeed() * (Global.HEIGHT - z * 2);
				var b : Boolean = false;
				for (var i : int; i < Player.player.parts.length; i++) {
					z = 5;
					if (i == 0) z = 100;
					if (Global.Sqr(x - Player.player.parts[i].x) + Global.Sqr(y - Player.player.parts[i].y) < Global.Sqr(radius + z)){
						b = true;
						break;
					}
				}
			} while (b);
		}
		
		public function Collide():Boolean {
			var d : Number;
			
			//x = Global.AspireSqr(x, target.x, 0.2);
			//y = Global.AspireSqr(y, target.y, 0.2);
			//if (speed < 25)	speed += 0.08;
			
			if (!dead) {
				if (scaleX < 1) scaleX += 0.01;
				alpha = scaleY = scaleX;				
				
				d = Global.Sqr(x - Player.player.parts[0].x) + Global.Sqr(y - Player.player.parts[0].y);
				rotationZ += 2;
				if (d < Global.Sqr(Player.player.part_width)){
					Player.player.AddPart();
					target.x = Player.player.parts[0].x;
					target.y = Player.player.parts[0].y;
					rotationZ = Player.player.parts[0].rotationZ;					
					dead = true;
					sound_ring.play();
					return true;
				}
			}else
				if (fade_out) {
					if (scaleX > 0) 
						scaleX -= 0.01
					else
						Reset();
					alpha = scaleY = scaleX;
				}else{
					x = Global.AspireSqr(x, target.x, 0.2);
					y = Global.AspireSqr(y, target.y, 0.2);
				
					alpha = scaleX = 1;
					if (Global.time_360 < Math.PI)
						scaleX = 1 - Math.abs(Math.sin(Global.time_360 * 2) * 0.1);
					scaleY = scaleX;			
					
					if (Global.Sqr(x - Player.player.parts[Player.player.parts.length - 1].x) + Global.Sqr(y - Player.player.parts[Player.player.parts.length - 1].y) < 1)
						fade_out = true;				
			}
			
			return false;
		}
	}
	
}