package Game
{
	import Engine.GameSprite;
	import Engine.ScrollingSprite;
	import Engine.SpriteEngine;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.ui.Keyboard;
	/**
	 * ...
	 * @author ...
	 */
	public class Game
	{
		public var Paused:Boolean = false;
		
		public var RenderEngine:SpriteEngine;
		
		//public var Ground:ScrollingSprite;
		public var Bg:ScrollingSprite;
		public var Clouds:ScrollingSprite;
		
		public var GameScene:Game.SceneGame;
		
		public var KeysPressed:Array = new Array(255);
		
		public var MouseDown:Boolean = false;
		
		
		
		private var _dat:Date = new Date();
		private var _time_delta:uint;
		private var _time_delta_last:uint = 0;
		private var _time_last:uint;
		
		private const MOVE_INTERVAL:uint = 40;
		
		private var cursor:GameSprite = new GameSprite(new Game.LibGraphics.Cursor(), new Point(13, 13));
		
		public function onKeyDown(e:KeyboardEvent):void {
			if ((e.keyCode == Keyboard.P) || (e.keyCode == Keyboard.PAUSE)) {
				Paused = !Paused;
			}
			KeysPressed[e.keyCode] = true;
		}
		
		public function onKeyUp(e:KeyboardEvent):void {
			KeysPressed[e.keyCode] = false;
			if (e.keyCode == Keyboard.SPACE) {
				GameScene.Player.shotDelay = 0;
			}
		}
		
		public function onMouseMove(e:MouseEvent):void {
			cursor.x = e.stageX;
			cursor.y = e.stageY;
			GameScene.Player.MouseMove(e.stageX, e.stageY);
		}
		
		public function onMouseDown(e:MouseEvent):void {
			MouseDown = true;
		}
		
		public function onMouseUp(e:MouseEvent):void {
			MouseDown = false;
			GameScene.Player.shotDelay = 0;
		}
		
		/**
		 * Обработка нажатых клавишь
		 */
		public function HandleControlls():void {
			if ((KeysPressed[Keyboard.LEFT]) || (KeysPressed[Keyboard.A])) {
				GameScene.Player.forceBackward();
			}
			
			if ((KeysPressed[Keyboard.RIGHT]) || (KeysPressed[Keyboard.D])) {
				GameScene.Player.forceForward();
			}
			
			if ((KeysPressed[Keyboard.UP]) || (KeysPressed[Keyboard.W])) {
				GameScene.Player.forceUp();
			}
			
			if ((KeysPressed[Keyboard.DOWN]) || (KeysPressed[Keyboard.S])) {
				GameScene.Player.forceDown();
			}
			
			if (MouseDown || (KeysPressed[Keyboard.SPACE])) {
				GameScene.Player.Shot();
			}
		}
		
		/**
		 * Прорисовка
		 */
		public function Render():void {
			
			_dat = new Date();
			
			HandleControlls();
			
			Bg.speedX = -GameScene.Player.Speed / 3;
			Clouds.speedX = -GameScene.Player.Speed / 2;

			var times:uint = Math.floor(_time_delta / MOVE_INTERVAL);
			
			if (times < 1) {
				if (_time_delta_last + _time_delta >= MOVE_INTERVAL) {
					if (!Paused) RenderEngine.Move();
					_time_delta_last = _time_delta;
				}else {
					_time_delta_last += _time_delta;
				}
			}else{
				for (var i:uint = 0; i < times; i++) {
					if (!Paused) RenderEngine.Move();
				}
			}
			
			RenderEngine.DrawBuffer();
	
			_time_delta = _dat.getTime() - _time_last;
			_time_last = _dat.getTime();
		}
		
	
		public function NewGame():void {
			GameScene = new Game.SceneGame();
			GameScene.Player.parentGame = this;
			
			Bg.zIndex = 0;
			Clouds.zIndex = 1;
			GameScene.zIndex = 2;
			cursor.zIndex = 10;
			
			RenderEngine.AddSprite(Bg);
			RenderEngine.AddSprite(GameScene);
			RenderEngine.AddSprite(cursor);
			
			
			
			_time_delta = 40;
			_time_last = _dat.getTime();
		}
		
		public function Game()
		{
			RenderEngine = new SpriteEngine();
			RenderEngine.InitSurface(640, 480);
			
			Bg = new ScrollingSprite(new Game.LibGraphics.Background());
			Clouds = new ScrollingSprite(new Game.LibGraphics.Clouds());
			
			NewGame();
		}
		
	}

}