package Game
{
	import Engine.Functions;
	import Engine.GameSprite;
	import Engine.SpriteGroup;
	import flash.display.Bitmap;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.media.Sound;
	import flash.ui.Keyboard;
	import flash.ui.Mouse;
	import flash.utils.setInterval;
	import Game.LibGraphics;
	import Game.Effects.*;
	import Game.Objects.Bullets.SimpleBullet;
	import Game.Objects.Bonuses.*;
	
	/**Game
	 * ...
	 * @author ...
	 */
	public class UserPlayer extends SpriteGroup
	{
		public const MIN_SPEED:Number = 5;
		public const MAX_SPEED:Number = 15;
		public const ACCELERATION:Number = 0.2;
		public const ACCELERATION_Y:Number = 0.3;
		
		public var Health:int = 1000;
		
		public var Speed:Number = MIN_SPEED;
		public var SpeedY:Number = 0;
		
		public var parentGame:Game.Game;
		
		public var shotDelay:int = 0;
		
		private var _maxShotDelay:int = 7;
		
		private var _gunType:int = 0;
		
		private var _Ship:GameSprite;
		private var _Gun:GameSprite;
		private var _Life:GameSprite;
		
		private var _sndShot:Sound;
		
		/**
		 * Вперед
		 */
		public function forceForward():void {
			
			Speed += ACCELERATION;
			
			if (Speed > MAX_SPEED) {
				Speed = MAX_SPEED;
			}
		}
		
		/**
		 * Назад
		 */
		public function forceBackward():void {
			
			Speed -= ACCELERATION;
			
			if (Speed < MIN_SPEED) {
				Speed = MIN_SPEED;
			}
		}
		
		/**
		 * Вверх
		 */
		public function forceUp():void {
			SpeedY -= ACCELERATION_Y;
			
			if (SpeedY < -4) {
				SpeedY = -4;
			}
		}
		
		/**
		 * Вниз
		 */
		public function forceDown():void {
			SpeedY += ACCELERATION_Y;
			
			if (SpeedY > 4) {
				SpeedY = 4;
			}
		}
		
		/**
		 * Выстрел
		 */
		public function Shot():void {

			if (shotDelay == 0) {
				var newBullet:GameSprite;
				
				if (_gunType == 0) {
					newBullet = new Game.Objects.Bullets.SimpleBullet();
				}
				
				if (_gunType == 1) {
					newBullet = new Game.Objects.Bullets.FireBullet();
				}
				
				newBullet.x = x;
				newBullet.y = y;
				newBullet.rotation = _Gun.rotation;
				Group.AddSprite(newBullet);
				
				shotDelay = _maxShotDelay;
				
				_sndShot.play();
			}else {
				shotDelay--;
			}
		}
		
		/**
		 * Движение мыши
		 * @param	stageX
		 * @param	stageY
		 */
		public function MouseMove(stageX:int, stageY:int):void {
			var dx:Number = stageX - globalX;
			var dy:Number = stageY - globalY - Group.y;
			
			var radians:Number = Math.atan2(dy, dx);
			var degrees:Number = radians * 180 / Math.PI;
			
			_Gun.rotation = radians;
		}
		
		/**
		 * Движение
		 */
		override public function Move():void
		{
			y += SpeedY;
			
			if ((!parentGame.KeysPressed[Keyboard.W]) && (!parentGame.KeysPressed[Keyboard.UP]) && (!parentGame.KeysPressed[Keyboard.S]) && (!parentGame.KeysPressed[Keyboard.DOWN])) {
				if (SpeedY < 0) {
					SpeedY += ACCELERATION_Y;
					
					if (SpeedY > 0) {
						SpeedY = 0;
					}
				}else if (SpeedY > 0) {
					SpeedY -= ACCELERATION_Y;
					
					if (SpeedY < 0) {
						SpeedY = 0;
					}
				}
			}
			
			if (y < 0) {
				y = 0;
			}
			
			if (y > 600) {
				y = 600;
			}
		}
	
		override public function Collision(Intersects:GameSprite):void
		{
			if (Intersects is Game.Objects.Bonuses.GunBonus) {
				Intersects.Delete = true;
				
				var snd:Sound = new Game.LibSounds.Bonus1();
				snd.play();
				
				if (_gunType == 0) {
					_gunType = 1;
					_maxShotDelay = 10;
				}else {
					_gunType = 0;
					_maxShotDelay = 7;
				}
			}
			
			if (Intersects is Game.Objects.Bullets.UFOBullet) {
				Intersects.Delete = true;
				
				Health -= 10;
			}
			
			if (Intersects is Game.Objects.Bullets.RocketBullet) {
				Intersects.Delete = true;
				
				Health -= 75;
			}
			
			_Life.currentFrame = Math.floor(((1000 - Health) / 1000) * 8);
			
			if (Health <= 0) {
				Group.AddSprite(new Game.Effects.Explosion(x, y));
				Delete = true;
				(Group as Game.SceneGame).Fail();
			}
		}
		
		override public function AddedToEngine():void
		{
		}
	
		public function UserPlayer()
		{
			_Ship = new GameSprite(new Game.LibGraphics.Ship(), new Point(20, 20));
			AddSprite(_Ship);
			
			_Gun = new GameSprite(new Game.LibGraphics.ShipGun(), new Point(9, 9));
			AddSprite(_Gun);
			
			_Life = new GameSprite(new Game.LibGraphics.Life(), new Point(0, 0), true, 62, 7, 1);
			_Life.Stop();
			_Life.x = -20;
			_Life.y = -30;
			
			AddSprite(_Life);
			
			x = 50;
			y = 200;
			
			SetClipRect(_Ship.Rect);
			
			CollisionEnabled = true;
			
			_sndShot = new Game.LibSounds.Shot1() as Sound;
		}
		
	}

}