package Game.Objects
{
	import Engine.GameSprite;
	import flash.display.SimpleButton;
	import flash.geom.Point;
	import flash.media.Sound;
	import Game.Effects.Explosion;
	import Game.LibGraphics;
	import Game.LibSounds;
	import Game.Objects.Bullets.FireBullet;
	import Game.Objects.Bullets.SimpleBullet;
	import Game.Objects.Bullets.UFOBullet;
	import Game.SceneGame;
	
	/**
	 * ...
	 * @author ...
	 */
	public class UFOEnemy extends GameSprite
	{
		private var _health:uint = 3;
		private var _angle:Number = 0;
		
		private var _xShift:int = 0;
		
		private var _sndShot:Sound;
		
		private var _shotInterval:uint = 0;
		
		private var _fireEnabled:Boolean = false;
		
		public var Speed:Number = 1;
		public var init_y:int = 0;
		
		override public function get outside():Boolean
		{
			return super.outside;
		}
		
		override public function set outside(value:Boolean):void
		{
			if (globalX < 0) {
				Delete = true;
			}
			super.outside = value;
		}
		
		override public function Move():void
		{
			if ((Group != null) && (Group is SceneGame)) {
				if (x > 450 + _xShift) {
					x = x - 3;
				}else {
					_fireEnabled = true;
				}
				y = init_y + Math.sin(_angle) * 100;
				_angle += 0.05;
			}else {
				return;
			}
			
			if (_fireEnabled) {
				if (_shotInterval == 0) {
					
					_sndShot.play();
					_shotInterval = 20;
					
					var dx:Number = (Group as SceneGame).Player.x - x;
					var dy:Number = (Group as SceneGame).Player.y - y;
					var r:Number = Math.atan2(dy, dx);
					
					var Bullet:UFOBullet = new UFOBullet();
					Bullet.x = x;
					Bullet.y = y;
					Bullet.vector = new Point(Math.cos(r), Math.sin(r));
					
					Group.AddSprite(Bullet);
					
				}else {
					_shotInterval--;
				}
			}
			
			super.Move();
		}
		
		override public function Collision(Intersects:GameSprite):void
		{
			super.Collision(Intersects);
			
			if (Intersects is SimpleBullet) {
				if (_health == 0) {
					Delete = true;
					Intersects.Delete = true;
					Group.AddSprite(new Explosion(x, y));
				}else {
					var snd:Sound = new LibSounds.Hit1() as Sound;
					snd.play();
					_health--;
					Intersects.Delete = true;
				}
			}
			
			if (Intersects is FireBullet) {
				Delete = true;
				Intersects.Delete = true;
				Group.AddSprite(new Explosion(x, y));
			}
		}
		
		public function UFOEnemy()
		{
			super(new LibGraphics.UFOEnemy(), new Point(26, 15), true, 53, 31, 1);
			CollisionEnabled = true;
			_sndShot = new LibSounds.Shot2() as Sound;
			_angle = 360 * Math.random();
			_shotInterval = Math.floor(Math.random() * 20);
			
			_xShift = -50 + Math.floor(100 * Math.random());
		}
		
	}

}