package 
{
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.display.BlendMode;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.media.Sound;
	import flash.net.SharedObject;

	public class Player extends Sprite 
	{
		public var angle : Number;
		private var angle_speed : Number;
		private var angle_target : Number;
		public var angle_control : int;
		public var speed : Number;
		public var speed_v : Point = new Point();		
		public var pos : Point = new Point(0, 600);
		public var parts : Array = new Array();	
		public var parts_dead : Array = new Array();	
		private var timer_part : Number = 1;
		public var part_width : Number = 20;
		private var head : Sprite = new Sprite();
		
		public var parts_max : int = 0;
		
		public static var player : Player;
		
		[Embed(source = '../lib/part.png')]
		public var bmp_part_embed : Class;
		public var bmp_part : Bitmap = new bmp_part_embed() as Bitmap;
		
		[Embed(source="../lib/collision.mp3")]
        public var sound_collision_embed : Class;
		private var sound_collision : Sound = new sound_collision_embed() as Sound;
		
		public var shared : SharedObject;
		
		private function DrawPart(p : Sprite):void {
			var m : Matrix = new Matrix(); m.translate(45/2, 10);
			with (p.graphics){
				beginBitmapFill(bmp_part.bitmapData, m);
				drawRect( -5, -part_width / 2, 10, part_width);
				moveTo( -40, 0);
				lineTo( -5, part_width / 3);
				lineTo( -5, -part_width / 3);
				lineTo( -40, 0);
				endFill();	
				
				beginBitmapFill(bmp_part.bitmapData, m);
				moveTo( -5, part_width / 2);
				lineTo( 3, part_width / 2 + 2);
				lineTo( 5, part_width / 2);
				lineTo( -5, part_width / 2);
				
				moveTo( -5, -part_width / 2);
				lineTo( 3, -part_width / 2 - 2);
				lineTo( 5, -part_width / 2);
				lineTo( -5, -part_width / 2);					
				endFill();	
			}
		}
		
		public function Player() {
			angle = -Math.PI / 6;
			speed = 1;
			angle_speed = 0.02 * 20 / part_width; 
			angle_control = angle_target = 0;	
			
			var m : Matrix = new Matrix(); m.translate(45 / 2, 10);
			
			addChild(head);
			head.y = -50;
			with (head.graphics) {
				beginBitmapFill(bmp_part.bitmapData, m);
				moveTo( 25, 0);
				lineTo( 0, part_width * 0.8);
				lineTo( -25, 0);
				lineTo( -0, -part_width * 0.8);
				lineTo( 25, 0);
				endFill();	
				
				beginBitmapFill(bmp_part.bitmapData, m);
				moveTo( 0, part_width / 2);
				lineTo( 15, part_width / 2 + 2);
				lineTo( 18, 0);
				lineTo( 0, part_width / 2);
				
				moveTo( 0, -part_width / 2);
				lineTo( 15, -part_width / 2 - 2);
				lineTo( 18, 0);
				lineTo( 0, -part_width / 2);	
				endFill();					
			}						
			
			for (var i : int = 0; i < 10; i++) {
				parts[i] = new Sprite();	
				addChild(parts[i]);				
				parts[i].x = -100 - i * 20;
				DrawPart(parts[i]);
			}
			
			shared = SharedObject.getLocal("oueo_high_flying_kite");
			if (!shared.data.parts_max)
				parts_max = parts.length;
			else
				parts_max = shared.data.parts_max;
		}
		
		public function AddPart():void {
			parts.push(new Sprite());
			var m : Matrix = new Matrix(); m.translate(45/2, 10);
			
			addChild(parts[parts.length - 1]);
			DrawPart(parts[parts.length - 1]);
			
			with (parts[parts.length - 1]) {
				if (parts.length > 1){
					x = parts[parts.length - 2].x;
					y = parts[parts.length - 2].y;
					rotationZ = parts[parts.length - 2].rotationZ;					
				}		
				
				graphics.beginBitmapFill(bmp_part.bitmapData, m);
				graphics.drawRect( -5, -part_width / 2, 10, part_width);
				graphics.moveTo( -40, 0);
				graphics.lineTo( -5, part_width / 3);
				graphics.lineTo( -5, -part_width / 3);
				graphics.lineTo( -40, 0);
				graphics.endFill();
			}
		}
		
		public function Update():void {
			var i : int;

			head.x = pos.x;
			head.y = pos.y;
			head.rotationZ = angle * 180 / Math.PI;
			
			//angle_target = Global.Aspire(angle_target, angle_control, 0.02);
			//angle += angle_target * angle_speed * speed;
			//angle = Global.NormalizeAngle(angle);
			//angle_target = Global.NormalizeAngle(angle_target);
			angle = Global.AspireAngle(angle, angle + angle_control, angle_speed * speed);
			
			i = parts.length; if (i > 200) i = 200;
			speed = Global.Aspire(speed, 1.5 + i / 30, 0.1);
			speed_v.x = speed * Math.cos(angle);
			speed_v.y = speed * Math.sin(angle);
			pos.x += speed_v.x;
			pos.y += speed_v.y;
			
			if (pos.x < -part_width / 2) pos.x = Global.WIDTH + part_width / 2;
			if (pos.x > Global.WIDTH + part_width / 2) pos.x = -part_width / 2;
			if (pos.y < -part_width / 2) pos.y = Global.HEIGHT + part_width / 2;
			if (pos.y > Global.HEIGHT + part_width / 2) pos.y = -part_width / 2;
			
			/*if (pos.x < 0) pos.x = Global.WIDTH;
			if (pos.x > Global.WIDTH) pos.x = 0;
			if (pos.y < 0) pos.y = Global.HEIGHT + part_width / 2;
			if (pos.y > Global.HEIGHT) pos.y = 0;*/
			
			for (i = 1; i < parts.length - 1; i++){
				parts[i].scaleX = 1 + 0.2 * Math.sin(0.3 * i / speed + Global.time_360);
				parts[i].scaleY = 1 + 0.07 * Math.cos(0.6 * i / speed + Global.time_360);
			}
				
			//if (parts[part_first].scaleX < 1) parts[part_first].scaleX += speed / 10;
			//parts[part_first].scaleY = parts[part_first].scaleX;
			
			for (i = 1; i < 5; i++)
				if (parts.length - i >= 0)
					parts[parts.length - i].alpha = 0.2 * i;
			//parts[parts.length - 1].scaleY = parts[parts.length - 1].scaleX;
			
			timer_part -= 1;
			if (timer_part < 0) {
				timer_part = 10 / speed;
				
				parts.unshift(parts.pop());
				with (parts[0]){
					x = pos.x;
					y = pos.y;
					rotationZ = angle * 180 / Math.PI;
					alpha = 1;
				}
				
				/*part_first--;
				if (part_first < 0) part_first = parts.length - 1;

				part_last = part_first - 1;
				if (part_last < 0) part_last = parts.length - 1;					
	
				with (parts[part_first]){
					x = pos.x;
					y = pos.y;
					rotationZ = angle * 180 / Math.PI;
					//scaleX = scaleY = 0.8;
					young = false;
				}*/
			}
			
			if (parts_dead)
				for (i = 0; i < parts_dead.length; i++)
					if (parts_dead[i].alpha > 0){
						parts_dead[i].alpha -= 0.01;
						parts_dead[i].x -= Math.cos(i * 0.5) * alpha * 0.2;
						parts_dead[i].y -= Math.sin(i * 0.5) * alpha * 0.2;
					}else {
						removeChild(parts_dead[i]);
						parts_dead.splice(i, 1);
					}	
					
			graphics.clear();
			graphics.lineStyle(1.5, 0xAAAAAA, 0.3);
			graphics.moveTo(100, 610);
			graphics.lineTo(pos.x, pos.y);
			
			if (parts_max < parts.length){
				parts_max = parts.length;
				shared.data.parts_max = parts_max;
				shared.flush();
			}
		}
		
		public function SelfCollide():Boolean {			
			for (var i : int = 10; i < parts.length; i++)
				if (Math.abs(parts[i].x - parts[0].x) + Math.abs(parts[i].y - parts[0].y) < 10) {
					parts_dead = parts_dead.concat(parts.splice(i, parts.length - i + 1));
					sound_collision.play();
					return true;
				}
			return false;
		}
	}
	
}