package Game
{
	import flash.geom.Matrix;
	import Engine.GameSprite;
	import Engine.ScrollingSprite;
	import Engine.SpriteGroup;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.setInterval;
	import Game.Objects.UFOEnemy;
	import Game.Scenarios.*;
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	
	/**
	 * ...
	 * @author ...
	 */
	public class SceneGame extends SpriteGroup
	{
		public var Player:Game.UserPlayer;
		public var Ground:ScrollingSprite;
		
		public var Enemys:Array = new Array();
		
		public var CurrentScenario:Game.Scenarios.Scenario;
		
		public var Timeline:uint = 0;
		
		private var _timeAtStart:uint;
		
		public var sndMainTheme:Sound;
		public var sndBossThee:Sound;
		public var chnMainTheme:SoundChannel;
		
		override public function Move():void
		{
			var _dat:Date = new Date();
			
			Timeline = _dat.getTime() - _timeAtStart;
			
			CurrentScenario.Run(this, Timeline);
			
			Ground.speedX = -Player.Speed;
			y = (200 - Player.y) / 2;
			super.Move();
		}
		
		public function Win():void {
			trace('WIN');
			
			var _win:GameSprite = new GameSprite(new Game.LibGraphics.Win(), new Point(0, 0));
			_win.x = 208;
			_win.y = 190;
			
			_win.zIndex = 10;
			AddSprite(_win);
		}
		
		public function Fail():void {
			Sprites = new Array();
			trace('FAIL');
			
			if (chnMainTheme != null) {
				chnMainTheme.removeEventListener(Event.SOUND_COMPLETE, loopSound);
				chnMainTheme.removeEventListener(Event.SOUND_COMPLETE, loopBossTheme);
				chnMainTheme.stop();
			}
			
			Player.parentGame.NewGame();
		}
		
		private function loopSound(e:Event):void {
			chnMainTheme = sndMainTheme.play();
			chnMainTheme.addEventListener(Event.SOUND_COMPLETE, loopSound);
		}
		
		public function loopBossTheme(e:Event):void {
			chnMainTheme = sndBossThee.play();
			chnMainTheme.addEventListener(Event.SOUND_COMPLETE, loopBossTheme);
		}
		
		
		public function SceneGame()
		{
			Player = new Game.UserPlayer();
			Player.zIndex = 0;
			AddSprite(Player);
			
			Ground = new ScrollingSprite(new Game.LibGraphics.Ground());
			Ground.y = 400;
			Ground.zIndex = 1;
			
			AddSprite(Ground);
			
			CurrentScenario = new Game.Scenarios.Mission1Scenario();
			
			CollisionCheck = true;
			
			var _dat:Date = new Date();
			_timeAtStart = _dat.getTime();
			
			sndMainTheme = new Game.LibSounds.GameLoop() as Sound;
			sndBossThee = new Game.LibSounds.BossTheme() as Sound;
			chnMainTheme = sndMainTheme.play();
			chnMainTheme.addEventListener(Event.SOUND_COMPLETE, loopSound);
		}
		
	}

}