package  
{
	import engine.Keys;
	import engine.MathExt;
	import engine.Utils;
	import flash.display.Sprite;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Point;
	import interfaces.IGameObject;
	
	/**
	 * ...
	 * @author Fiodar Paulavets
	 */
	public class Hero extends Sprite implements IGameObject 
	{		
		private var _clip: HeroClip;
		private var _maskClip: HeroClip;
		private var _lastShot: Number = 0;
		
		public var radius: Number;
		
		private var _damaged: Number = 0;
		private var _maskInc: Boolean;
		
		public var life: int;
		
		public function Hero() 
		{
			_clip = new HeroClip();
			_clip.scaleX = _clip.scaleY = 60 / _clip.width;
			addChild(_clip);
			_clip.play();
			
			_maskClip = new HeroClip();
			_maskClip.scaleX = _maskClip.scaleY = 60 / _maskClip.width;
			addChild(_maskClip);
			_maskClip.play();
			var matrix:Array = new Array();
            matrix = matrix.concat([1, 0, 0, 0, 0]); // red
            matrix = matrix.concat([1, 1, 0, 0, 0]); // green
            matrix = matrix.concat([1, 1, 1, 0, 0]); // blue
            matrix = matrix.concat([0, 0, 0, 1, 0]); // alpha
			var filter: ColorMatrixFilter = new ColorMatrixFilter(matrix);
			_maskClip.filters = [filter];
			_maskClip.visible = false;
			
			x = 100;
			y = 100;
			
			radius = 22;
			
			life = 3;
			
			//graphics.beginFill(0xFFFFFF);
			//graphics.drawCircle(0, 0, radius);
		}
		
		/* INTERFACE interfaces.IGameObject */
		
		public function Update(dt:Number):void 
		{
			var target: Point = Keys.cursor;
			
			var s: Point = MathExt.Speed(x, y, target.x, target.y);
			var d: Number = MathExt.Distance(x, y, target.x, target.y);
			
			x = Utils.Aspire(x, target.x, Math.abs(s.x) * Math.max(250, d) * dt);
			y = Utils.Aspire(y, target.y, Math.abs(s.y) * Math.max(250, d) * dt);
			
			if ((d < 50) || (s.x == 0))
				rotation = Utils.Aspire(rotation, 0, 90 * dt);
			else
				if (s.x > 0)
					rotation = Utils.Aspire(rotation, 25, 90 * dt);
				else if (s.x < 0)
					rotation = Utils.Aspire(rotation, -25, 90 * dt);
					
			var fire: Boolean = Keys.mouseDown;
			
			_lastShot -= dt;
			if (fire && _lastShot <= 0)
			{
				World.world.bulletsList.add(new Bullet(x, y - 5, true, 1, 0));
				_lastShot = 0.3;
				//Sounds.manager.playSound('shoot');
			}
			
			// мигание
			if (_damaged > 0)
			{
				_damaged -= dt;
				if (!_maskInc)
					_maskClip.alpha -= dt * 4;
				else
					_maskClip.alpha += dt * 4;
				if (_maskInc && _maskClip.alpha >= 1)
					_maskInc = false;
				if (!_maskInc && _maskClip.alpha <= 0)
					_maskInc = true;
				if (_damaged <= 0)
					_maskClip.visible = false;
			}
		}
		
		public function GetDamage(): void
		{
			if (_damaged > 0)
				return;
			life--;
			World.world.hud.hp();
			_damaged = 2;
			_maskClip.alpha = 1;
			_maskClip.visible = true;
			_maskInc = false;
			Sounds.manager.playSound("damaged");
		}
		
	}

}