package  
{
	import net.flashpunk.World;
	import net.flashpunk.FP;
	import net.flashpunk.utils.Input;
	import net.flashpunk.Sfx;
	
	/**
	 * ...
	 * @author Benn
	 */
	public class GameWorld extends World 
	{
		
		[Embed(source = '../assets/levels/level1.oel', mimeType = 'application/octet-stream')] private static const LEVEL1:Class;
		[Embed(source = '../assets/levels/level2.oel', mimeType = 'application/octet-stream')] private static const LEVEL2:Class;
		[Embed(source = '../assets/levels/level3.oel', mimeType = 'application/octet-stream')] private static const LEVEL3:Class;
		[Embed(source = '../assets/levels/level4.oel', mimeType = 'application/octet-stream')] private static const LEVEL5:Class;
		[Embed(source = '../assets/levels/level5.oel', mimeType = 'application/octet-stream')] private static const LEVEL4:Class;
		[Embed(source = '../assets/levels/level6.oel', mimeType = 'application/octet-stream')] private static const LEVEL6:Class;
		[Embed(source = '../assets/levels/level7.oel', mimeType = 'application/octet-stream')] private static const LEVEL7:Class;
		[Embed(source = '../assets/levels/level8.oel', mimeType = 'application/octet-stream')] private static const LEVEL8:Class;
		[Embed(source = '../assets/levels/level9.oel', mimeType = 'application/octet-stream')] private static const LEVEL9:Class;
		[Embed(source = '../assets/levels/level10.oel', mimeType = 'application/octet-stream')] private static const LEVEL10:Class;
		
		[Embed(source = '../assets/sound/music.mp3')] private static const MUSIC:Class;
		
		private var Music:Sfx = new Sfx(MUSIC);
		
		private var GamePlayer:Player;
		
		private var Levels:Array = [];
		
		private var CurrentLevel:Level;
		
		private var LevelCounter:int = 0;
		
		private var Spawn:StartPoint;
		
		private var Finish:EndPoint;
		
		private var Blocks:Array = [];
		
		private var Chasers:Array = [];
		
		private var GameText:Array = [];
		
		public var IsOnFrontPage:Boolean = true;
		
		public var _PlayButton:PlayButton;
		
		public var _FrontPage:FrontPage;
		
		public function GameWorld() 
		{	
			
			Music.loop();
			
			_FrontPage = new FrontPage;
			
			add(_FrontPage);
			
			_PlayButton = new PlayButton;
			
			add(_PlayButton);
			
		}
		
		override public function update():void 
		{
			
			if (IsOnFrontPage)
			{
				
				if (Input.mousePressed)
				{
				
					if (collidePoint("playbutton", Input.mouseX, Input.mouseY))
					{
				
						IsOnFrontPage = false;
						
						LoadLevels();
						
						CurrentLevel = Levels[LevelCounter];
						
						add(CurrentLevel);
						
						Spawn = new StartPoint;
						
						add(Spawn);
						
						Finish = new EndPoint;
						
						add(Finish);
						
						LoadObjects();
						
						GamePlayer = new Player;
						
						GamePlayer.SetSpawnPoint(Spawn.x + 12, Spawn.y + 12);
						
						GamePlayer.MoveToSpawnPoint();
						
						add(GamePlayer);
						
						LoadBlocks();
						
						LoadChasers();
						
						LoadLevelText();
						
					}
					
				}
				
				return;
				
			}
			
			
			if (GamePlayer.IsLevelComplete)
			{
				//load next level
				
				++LevelCounter;
				
				CurrentLevel.ResetMask();
				
				CurrentLevel = Levels[LevelCounter];
				
				remove(CurrentLevel);
				
				add(CurrentLevel);
				
				LoadObjects();
				
				ClearBlocks();
				
				LoadBlocks();
				
				ClearChasers();
				
				LoadChasers();
				
				remove(GamePlayer);
				
				GamePlayer.SetSpawnPoint(Spawn.x + 12, Spawn.y + 12);
			
				GamePlayer.MoveToSpawnPoint();
				
				GamePlayer.IsLevelComplete = false;
				
				add(GamePlayer);
				
				LoadLevelText();
				
			}
			
			super.update();
		}
		
		public function ClearChasers():void
		{
			var length:int = Chasers.length;
			
			for (var i:int = 0; i < length; i++) 
			{
				remove(Chasers[i]);
			}
			
			for (var ii:int = 0; ii < length; ii++) 
			{
				Chasers.pop();
			}
		}
		
		public function LoadChasers():void
		{
			
			var dataList:XMLList;
			var dataElement:XML;
			
			dataList = CurrentLevel.LevelData.objects.Chaser;
			for each (dataElement in dataList)
			{
				
				var NewChaser:Chaser = new Chaser();
				
				add(NewChaser);
				
				NewChaser.x = int(dataElement.@x) + 12;
				NewChaser.y = int(dataElement.@y) + 12;
				
				Chasers.push(NewChaser);
			}
			
		}
		
		public function ClearBlocks():void
		{
			
			var length:int = Blocks.length;
			
			for (var i:int = 0; i < length; i++) 
			{
				remove(Blocks[i]);
			}
			
			for (var ii:int = 0; ii < length; ii++) 
			{
				Blocks.pop();
			}
			
		}
		
		public function LoadBlocks():void
		{
			
			var dataList:XMLList;
			var dataElement:XML;
			
			dataList = CurrentLevel.LevelData.objects.Block;
			for each (dataElement in dataList)
			{
				
				var NewBlock:Block = new Block();
				
				add(NewBlock);
				
				NewBlock.x = int(dataElement.@x);
				NewBlock.y = int(dataElement.@y);
				
				Blocks.push(NewBlock);
			}
			
		}
		
		public function LoadObjects():void
		{
			
			remove(Spawn);
			remove(Finish);
			
			var dataList:XMLList;
			var dataElement:XML;
			
			dataList = CurrentLevel.LevelData.objects.Start;
			for each (dataElement in dataList)
			{
				
				Spawn.x = int(dataElement.@x);
				Spawn.y = int(dataElement.@y);	
				
			}
			
			dataList = CurrentLevel.LevelData.objects.Finish;
			for each (dataElement in dataList)
			{
				
				Finish.x = int(dataElement.@x);
				Finish.y = int(dataElement.@y);	
				
			}
			
			add(Spawn);
			
			if (LevelCounter != 9)
			{
			
				add(Finish);
			
			}
			
		}
		
		public function LoadLevels():void
		{
			
			Levels.push(new Level(LEVEL1));
			Levels.push(new Level(LEVEL2));
			Levels.push(new Level(LEVEL3));
			Levels.push(new Level(LEVEL4));
			Levels.push(new Level(LEVEL5));
			Levels.push(new Level(LEVEL6));
			Levels.push(new Level(LEVEL7));
			Levels.push(new Level(LEVEL8));
			Levels.push(new Level(LEVEL9));
			Levels.push(new Level(LEVEL10));
			
		}
		
		public function ClearLevelText():void
		{
			
			var length:int = GameText.length;
			
			for (var i:int = 0; i < length; i++) 
			{
				remove(GameText[i]);
			}
			
			for (var ii:int = 0; ii < length; ii++) 
			{
				GameText.pop();
			}
			
		}
		
		public function LoadLevelText():void
		{
			
			ClearLevelText();
			
			var NewText:InGameText;
			
			switch (LevelCounter)
			{
				
				case 0:
				
				NewText = new InGameText("Welcome.", 0);
				
				NewText.x = 50;
				NewText.y = 350;
				
				add(NewText);
				
				GameText.push(NewText);
				
				NewText = new InGameText("Click and hold the mouse to move the player.", 1);
				
				NewText.x = 50;
				NewText.y = 375;
				
				add(NewText);
				
				GameText.push(NewText);
				
				NewText = new InGameText("Move to the red circle to continue.", 2);
				
				NewText.x = 50;
				NewText.y = 400;
				
				add(NewText);
				
				GameText.push(NewText);
				
				break;
				
				case 1:
				
				NewText = new InGameText("Try to avoid touching the walls.", 0);
				
				NewText.x = 50;
				NewText.y = 400;
				
				add(NewText);
				
				GameText.push(NewText);
				
				break;
				
				case 2:
				
				NewText = new InGameText("Smaller clicks are more precise", 0);
				
				NewText.x = 50;
				NewText.y = 400;
				
				add(NewText);
				
				GameText.push(NewText);
				
				NewText = new InGameText("than holding the mouse.", 0);
				
				NewText.x = 50;
				NewText.y = 425;
				
				add(NewText);
				
				GameText.push(NewText);
				
				break;
				
				case 6:
				
				NewText = new InGameText("Watch out for the chasers!", 0);
				
				NewText.x = 50;
				NewText.y = 400;
				
				add(NewText);
				
				GameText.push(NewText);
				
				break;
				
				case 9:
				
				NewText = new InGameText("Success!", 0);
				
				NewText.x = 50;
				NewText.y = 50;
				
				add(NewText);
				
				GameText.push(NewText);
				
				NewText = new InGameText("You have escaped from the chasers!", 1);
				
				NewText.x = 50;
				NewText.y = 75;
				
				add(NewText);
				
				GameText.push(NewText);
				
				NewText = new InGameText("You are the best! A winner is you! :D", 2);
				
				NewText.x = 50;
				NewText.y = 100;
				
				add(NewText);
				
				GameText.push(NewText);
				
				NewText = new InGameText("http://www.benn-gaming.co.uk", 3);
				
				NewText.x = 50;
				NewText.y = 370;
				
				add(NewText);
				
				GameText.push(NewText);
				
				NewText = new InGameText("Twitter: @bennpowell", 4);
				
				NewText.x = 50;
				NewText.y = 395;
				
				add(NewText);
				
				GameText.push(NewText);
				
				NewText = new InGameText("Made in 48 hours for the 21st Ludum Dare Competition", 5);
				
				NewText.x = 50;
				NewText.y = 420;
				
				add(NewText);
				
				GameText.push(NewText);
				
				break;
				
			}
			
		}
		
	}

}