package  
{
	import adobe.utils.CustomActions;
	import flash.events.KeyboardEvent;
	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/sound/music.mp3')] private static const GAME_MUSIC:Class;
		
		private var music:Sfx = new Sfx(GAME_MUSIC);
		
		private var GameCredits:Credits;
		private var intro:IntroScreen;
		
		//player
		private var gameplayer:Player;
		
		//background
		private var _background:Background;
		
		private var backgroundcolourred:uint = 11;
		private var backgroundcolourgreen:uint = 11;
		private var backgroundcolourblue:uint = 11;
		
		private var backgroundtransitionred:uint = 11;
		private var backgroundtransitiongreen:uint = 11;
		private var backgroundtransitionblue:uint = 11;
		
		//main menu
		private var RedGameQuad:Quad;
		private var BlueGameQuad:Quad;
		private var GreenGameQuad:Quad;
		private var HasWonBlue:Boolean = false;
		private var HasWonRed:Boolean = false;
		private var HasWonGreen:Boolean = false;
		
		//red level
		private var RedLevelTimer:Number = 0;
		
		//blue level
		private var BlueLevelTimer:Number = 0;
		private var BlueLevelBlockCounter:int = 0;
		private var BlueStartDirection:int;
		private var BlueLevelPhase:int = 0;
		
		//green level
		private var GreenLevelTimer:Number = 0;
		private var GreenSpamBlocks:Boolean = false;
		private var GreenBlockTimer:Number = 0.5;
		private var GreenBlocksFaster:Boolean = false;
		
		//game modes
		private var InIntroScreen:Boolean = true;
		private var InMainMenu:Boolean = false;
		private var InMenuToGameModeRed:Boolean = false;
		private var InGameModeRed:Boolean = false;
		private var InMenuToGameModeBlue:Boolean = false;
		private var InGameModeBlue:Boolean = false;
		private var InDeadToMainMenu:Boolean = false;
		private var InMenuToGameModeGreen:Boolean = false;
		private var InGameModeGreen:Boolean = false;
		
		public function GameWorld() 
		{
			
			intro = new IntroScreen;
			add(intro);
			
			music.loop();
			
			//FP.console.enable();
		}
		
		private function ClearQuads():void
		{
		
			var QuadList:Array = [];
					
			FP.world.getClass(Quad, QuadList);
			
			for each(var Q:Quad in QuadList)
			{
				remove(Q);
			}
			
		}
		
		private function ClearEnemies():void
		{
			
			var EnemyList:Array = [];
					
			FP.world.getClass(BaseEnemy, EnemyList);
			
			for each(var E:BaseEnemy in EnemyList)
			{
				remove(E);
			}
			
		}
		
		private function TransitionToGameModeGreenUpdate():void
		{
			
			if (backgroundcolourred > backgroundtransitionred)
			{
				backgroundcolourred -= 1;
			}
			else if (backgroundcolourred < backgroundtransitionred)
			{
				backgroundcolourred += 1;
			}
			
			if (backgroundcolourgreen > backgroundtransitiongreen)
			{
				backgroundcolourgreen -= 1;
			}
			else if (backgroundcolourgreen < backgroundtransitiongreen)
			{
				backgroundcolourgreen += 1;
			}
			
			if (backgroundcolourblue > backgroundtransitionblue)
			{
				backgroundcolourblue -= 1;
			}
			else if (backgroundcolourblue < backgroundtransitionblue)
			{
				backgroundcolourblue += 1;
			}
			
			_background.SetColour(backgroundcolourred, backgroundcolourgreen, backgroundcolourblue);
		
			if (backgroundcolourred == backgroundtransitionred && backgroundcolourblue == backgroundtransitionblue && backgroundcolourgreen == backgroundtransitiongreen)
			{
				ClearQuads();
				remove(GreenGameQuad);
				remove(RedGameQuad);
				remove(BlueGameQuad);
				CreateGameModeGreen();
			}
			
		}
		
		private function TransitionToGameModeBlueUpdate():void
		{
			
			if (backgroundcolourred > backgroundtransitionred)
			{
				backgroundcolourred -= 1;
			}
			else if (backgroundcolourred < backgroundtransitionred)
			{
				backgroundcolourred += 1;
			}
			
			if (backgroundcolourgreen > backgroundtransitiongreen)
			{
				backgroundcolourgreen -= 1;
			}
			else if (backgroundcolourgreen < backgroundtransitiongreen)
			{
				backgroundcolourgreen += 1;
			}
			
			if (backgroundcolourblue > backgroundtransitionblue)
			{
				backgroundcolourblue -= 1;
			}
			else if (backgroundcolourblue < backgroundtransitionblue)
			{
				backgroundcolourblue += 1;
			}
			
			_background.SetColour(backgroundcolourred, backgroundcolourgreen, backgroundcolourblue);
		
			if (backgroundcolourred == backgroundtransitionred && backgroundcolourblue == backgroundtransitionblue && backgroundcolourgreen == backgroundtransitiongreen)
			{
				ClearQuads();
				remove(GreenGameQuad);
				remove(RedGameQuad);
				remove(BlueGameQuad);
				CreateGameModeBlue();
			}
			
		}
		
		private function TransitionToMenuUpdate():void
		{
			
			if (backgroundcolourred > backgroundtransitionred)
			{
				backgroundcolourred -= 1;
			}
			else if (backgroundcolourred < backgroundtransitionred)
			{
				backgroundcolourred += 1;
			}
			
			if (backgroundcolourgreen > backgroundtransitiongreen)
			{
				backgroundcolourgreen -= 1;
			}
			else if (backgroundcolourgreen < backgroundtransitiongreen)
			{
				backgroundcolourgreen += 1;
			}
			
			if (backgroundcolourblue > backgroundtransitionblue)
			{
				backgroundcolourblue -= 1;
			}
			else if (backgroundcolourblue < backgroundtransitionblue)
			{
				backgroundcolourblue += 1;
			}
			
			_background.SetColour(backgroundcolourred, backgroundcolourgreen, backgroundcolourblue);
		
			if (backgroundcolourred == backgroundtransitionred && backgroundcolourblue == backgroundtransitionblue && backgroundcolourgreen == backgroundtransitiongreen)
			{
				CreateMainMenu();
				InDeadToMainMenu = false;
				InMainMenu = true;
			}
			
		}
		
		private function TransitionToGameModeRedUpdate():void
		{
	
			if (backgroundcolourred > backgroundtransitionred)
			{
				backgroundcolourred -= 1;
			}
			else if (backgroundcolourred < backgroundtransitionred)
			{
				backgroundcolourred += 1;
			}
			
			if (backgroundcolourgreen > backgroundtransitiongreen)
			{
				backgroundcolourgreen -= 1;
			}
			else if (backgroundcolourgreen < backgroundtransitiongreen)
			{
				backgroundcolourgreen += 1;
			}
			
			if (backgroundcolourblue > backgroundtransitionblue)
			{
				backgroundcolourblue -= 1;
			}
			else if (backgroundcolourblue < backgroundtransitionblue)
			{
				backgroundcolourblue += 1;
			}
			
			_background.SetColour(backgroundcolourred, backgroundcolourgreen, backgroundcolourblue);
		
			if (backgroundcolourred == backgroundtransitionred && backgroundcolourblue == backgroundtransitionblue && backgroundcolourgreen == backgroundtransitiongreen)
			{
				ClearQuads();
				remove(GreenGameQuad);
				remove(RedGameQuad);
				remove(BlueGameQuad);
				CreateGameModeRed();
			}
			
		}
		
		private function CreateGameModeGreen():void
		{
			
			InMenuToGameModeGreen = false;
			InGameModeGreen = true;
			
			for (var j:int = 0; j < 20; ++j)
			{
				
				var randjsize:int = FP.rand(20) + 150;
				
				var myjquad:Quad = new Quad(randjsize, randjsize, 0, 75, 0);
				
				myjquad.SetSpeed(FP.rand(20) + 50);
				myjquad.SetDirection(4);
				myjquad.SetNoCollide();
				
				myjquad.SetNoFade();
				
				myjquad.SetHighlightColours(0, 75, 0);
				
				myjquad.x = FP.rand(680) - 20;
				
				myjquad.y = FP.rand(555) - 75;	
				
				add(myjquad);
				
			}
			
			for (var i:int = 0; i < 120; ++i)
			{
				
				var randsize:int = FP.rand(10) + 50;
				
				var myquad:Quad = new Quad(randsize, randsize, 0, FP.rand(50) + 175, 0);
				
				myquad.SetSpeed(FP.rand(20) + 50);
				myquad.SetDirection(4);
				
				myquad.SetNoFade();
				
				myquad.SetHighlightColours(0, 255, 0);//FP.rand(50) + 200);
				
				myquad.y = FP.rand(520) - 20;
				
				if (i < 60) //left column
				{
					myquad.x = FP.rand(90) - 40;	
				}
				else //right
				{
					myquad.x = FP.rand(90) + 550;	
				}
				
				add(myquad);
				
			}	
			
			var newEnemy:BaseEnemy = new GreenWorldEnemyOne(160,-60);
			newEnemy.SetSpawnDelay(3);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(260,-60);
			newEnemy.SetSpawnDelay(3);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(360,-60);
			newEnemy.SetSpawnDelay(3);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(300,-60);
			newEnemy.SetSpawnDelay(4);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(400,-60);
			newEnemy.SetSpawnDelay(4);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(500,-60);
			newEnemy.SetSpawnDelay(4);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(160,-60);
			newEnemy.SetSpawnDelay(6);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(160,-60);
			newEnemy.SetSpawnDelay(6.5);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(160,-60);
			newEnemy.SetSpawnDelay(7);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(160,-60);
			newEnemy.SetSpawnDelay(7.5);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(160,-60);
			newEnemy.SetSpawnDelay(8);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(160,-60);
			newEnemy.SetSpawnDelay(8.5);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(500,-60);
			newEnemy.SetSpawnDelay(6);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(500,-60);
			newEnemy.SetSpawnDelay(6.5);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(500,-60);
			newEnemy.SetSpawnDelay(7);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(500,-60);
			newEnemy.SetSpawnDelay(7.5);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(500,-60);
			newEnemy.SetSpawnDelay(8);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(500,-60);
			newEnemy.SetSpawnDelay(8.5);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyTwo(300,-60);
			newEnemy.SetSpawnDelay(7);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(11);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(11.5);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(12);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(12.5);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(13);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(13.5);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(14);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(14.5);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(15);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(15.5);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyTwo(160,-60);
			newEnemy.SetSpawnDelay(18);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyTwo(460,-60);
			newEnemy.SetSpawnDelay(18);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(23.3);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(23.6);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(23.9);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(24.2);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(24.5);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(24.8);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(25.1);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(25.4);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(25.7);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(26);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(26.3);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(26.6);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(26.9);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(27.2);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(27.5);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(27.8);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(28.1);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(28.4);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(28.7);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyOne(FP.rand(400) + 100,-60);
			newEnemy.SetSpawnDelay(29);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyTwo(320,-60);
			newEnemy.SetSpawnDelay(32);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyTwo(160,-60);
			newEnemy.SetSpawnDelay(33);
			add(newEnemy);
			
			newEnemy = new GreenWorldEnemyTwo(460,-60);
			newEnemy.SetSpawnDelay(33);
			add(newEnemy);
			
			newEnemy = new GreenWorldBoss(270,-150);
			newEnemy.SetSpawnDelay(41);
			add(newEnemy);
			
			gameplayer = new Player;
			gameplayer.x = 320;
			gameplayer.y = 555;
			gameplayer.PlayerEnterGreenWorld = true;
			add(gameplayer);
			
		}
		
		private function CreateGameModeBlue():void
		{

			InMenuToGameModeBlue = false;
			InGameModeBlue = true;
			
			BlueStartDirection = 3;//= FP.rand(8);
			
			for (var j:int = 0; j < 40; ++j)
			{
				
				var randjsize:int = FP.rand(20) + 150;
				
				var myjquad:Quad = new Quad(randjsize, randjsize, 0, 0, 75);
				
				myjquad.SetSpeed(FP.rand(20) + 50);
				myjquad.SetDirection(BlueStartDirection);
				myjquad.SetNoCollide();
				
				myjquad.SetNoFade();
				
				myjquad.SetHighlightColours(0, 0, 75);
				
				myjquad.x = FP.rand(680) - 20;
				
				myjquad.y = FP.rand(555) - 75;	
				
				add(myjquad);
				
			}
			
			var newEnemy:BaseEnemy = new BlueWorldEnemyOne(-40,200);
			newEnemy.SetSpawnDelay(7);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyOne(-40,200);
			newEnemy.SetSpawnDelay(7.75);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyOne(-40,200);
			newEnemy.SetSpawnDelay(8.5);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyOne(-40,200);
			newEnemy.SetSpawnDelay(9.25);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyOne(-40,200);
			newEnemy.SetSpawnDelay(10);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyOne(-40,200);
			newEnemy.SetSpawnDelay(10.75);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyOne(-40,200);
			newEnemy.SetSpawnDelay(11.5);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyOne(-40,200);
			newEnemy.SetSpawnDelay(12.25);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyOne(-40,200);
			newEnemy.SetSpawnDelay(13);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyOne(-40,200);
			newEnemy.SetSpawnDelay(13.75);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyTwo(50,-50);
			newEnemy.SetSpawnDelay(19);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyTwo(300,-50);
			newEnemy.SetSpawnDelay(21);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyTwo(500, -50);
			newEnemy.SetSpawnDelay(23);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyOne(690,FP.rand(500) - 20);
			newEnemy.SetSpawnDelay(26);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyOne(690,FP.rand(500) - 20);
			newEnemy.SetSpawnDelay(27);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyOne(690,FP.rand(500) - 20);
			newEnemy.SetSpawnDelay(28);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyOne(690,FP.rand(500) - 20);
			newEnemy.SetSpawnDelay(29);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyOne(690,FP.rand(500) - 20);
			newEnemy.SetSpawnDelay(30);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyOne(-40,FP.rand(500) - 20);
			newEnemy.SetSpawnDelay(26);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyOne(-40,FP.rand(500) - 20);
			newEnemy.SetSpawnDelay(27);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyOne(-40,FP.rand(500) - 20);
			newEnemy.SetSpawnDelay(28);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyOne(-40,FP.rand(500) - 20);
			newEnemy.SetSpawnDelay(29);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyOne(-40,FP.rand(500) - 20);
			newEnemy.SetSpawnDelay(30);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyTwo(50, 530);
			newEnemy.SetSpawnDelay(35);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyTwo(300, 530);
			newEnemy.SetSpawnDelay(36);
			add(newEnemy);
			
			newEnemy = new BlueWorldEnemyTwo(500, 530);
			newEnemy.SetSpawnDelay(37);
			add(newEnemy);
			
			newEnemy = new BlueWorldBoss(660, 190);
			newEnemy.SetSpawnDelay(45);
			add(newEnemy);
			
			gameplayer = new Player;
			gameplayer.x = 320;
			gameplayer.y = -50;
			gameplayer.PlayerEnterBlueWorld = true;
			add(gameplayer);
			
		}
		
		private function CreateGameModeRed():void
		{
			
			for (var j:int = 0; j < 20; ++j)
			{
				
				var randjsize:int = FP.rand(20) + 150;
				
				var myjquad:Quad = new Quad(randjsize, randjsize, 75, 0, 0);
				
				myjquad.SetSpeed(FP.rand(20) + 50);
				myjquad.SetDirection(6);
				myjquad.SetNoCollide();
				
				myjquad.SetNoFade();
				
				myjquad.SetHighlightColours(75, 0, 0);
				
				myjquad.x = FP.rand(680) - 20;
				
				myjquad.y = FP.rand(555) - 75;	
				
				add(myjquad);
				
			}
			
			for (var i:int = 0; i < 120; ++i)
			{
				
				var randsize:int = FP.rand(10) + 50;
				
				var myquad:Quad = new Quad(randsize, randsize, FP.rand(50) + 175, 0, 0);
				
				myquad.SetSpeed(FP.rand(20) + 100);
				myquad.SetDirection(6);
				
				myquad.SetNoFade();
				
				myquad.SetHighlightColours(255, 0, 0);//FP.rand(50) + 200);
				
				myquad.x = FP.rand(680) - 20;
				
				if (i < 60) //top column
				{
					myquad.y = FP.rand(90) - 30;	
				}
				else //bottom
				{
					myquad.y = FP.rand(90) + 390;	
				}
				
				add(myquad);
				
			}		
			
			var newEnemy:BaseEnemy = new RedWorldEnemyOne(660,150);
			newEnemy.SetSpawnDelay(5);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,150);
			newEnemy.SetSpawnDelay(5.5);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,150);
			newEnemy.SetSpawnDelay(6);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,150);
			newEnemy.SetSpawnDelay(6.5);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,150);
			newEnemy.SetSpawnDelay(7);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,150);
			newEnemy.SetSpawnDelay(7.5);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,350);
			newEnemy.SetSpawnDelay(6.5);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,350);
			newEnemy.SetSpawnDelay(7);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,350);
			newEnemy.SetSpawnDelay(7.5);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,150);
			newEnemy.SetSpawnDelay(8);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,150);
			newEnemy.SetSpawnDelay(8.5);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,150);
			newEnemy.SetSpawnDelay(9);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyTwo(660,240);
			newEnemy.SetSpawnDelay(12);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,100);
			newEnemy.SetSpawnDelay(20);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,140);
			newEnemy.SetSpawnDelay(20.15);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,180);
			newEnemy.SetSpawnDelay(20.3);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,220);
			newEnemy.SetSpawnDelay(20.45);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,260);
			newEnemy.SetSpawnDelay(20.6);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,300);
			newEnemy.SetSpawnDelay(20.75);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,340);
			newEnemy.SetSpawnDelay(20.9);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,380);
			newEnemy.SetSpawnDelay(21.05);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyTwo(660,180);
			newEnemy.SetSpawnDelay(22);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyTwo(660,300);
			newEnemy.SetSpawnDelay(22);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,380);
			newEnemy.SetSpawnDelay(21.20);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,340);
			newEnemy.SetSpawnDelay(21.35);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,300);
			newEnemy.SetSpawnDelay(21.50);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,260);
			newEnemy.SetSpawnDelay(21.65);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,220);
			newEnemy.SetSpawnDelay(21.8);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,180);
			newEnemy.SetSpawnDelay(21.95);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,140);
			newEnemy.SetSpawnDelay(22.1);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,100);
			newEnemy.SetSpawnDelay(22.25);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(36);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(36.25);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(36.5);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(36.75);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(37);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(37.25);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(37.5);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(37.75);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(38);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(38.25);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(38.5);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(38.75);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(39);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(39.25);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(39.5);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(39.75);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(40);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(40.25);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(40.5);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(40.75);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(41);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(41.25);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(41.5);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(41.75);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(42);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldEnemyOne(660,FP.rand(200) + 150);
			newEnemy.SetSpawnDelay(42.25);
			newEnemy.SetSpeed(800);
			add(newEnemy);
			
			newEnemy = new RedWorldBoss(660, 150);
			newEnemy.SetSpawnDelay(45);
			add(newEnemy);
			
			gameplayer = new Player;
			gameplayer.x = -50;
			gameplayer.y = 220;
			gameplayer.PlayerEnterRedWorld = true;
			add(gameplayer);
			
			InMenuToGameModeRed = false;
			InGameModeRed = true;
			
		}
		
		private function CreateMainMenu():void
		{
			
			for (var i:int = 0; i < 30; ++i)
			{
				
				var randsize:int = FP.rand(20) + 10;
				
				var myquad:Quad = new Quad(randsize, randsize, 200, 200, 200);
				
				myquad.SetSpeed(FP.rand(20) + 10);
				myquad.SetRotating(true);
				myquad.SetDirection(1);
				
				myquad.SetHighlightColours(255, 255, 255);
				
				myquad.x = FP.rand(640);
				myquad.y = FP.rand(460);
				
				add(myquad);
				
			}
			
			if (HasWonRed && HasWonBlue && HasWonGreen)
			{
				GameCredits = new Credits;
				add(GameCredits);
			}
		
			if (!HasWonRed)
			{
			
				var redquadsize:int = FP.rand(20) + 10;

				RedGameQuad = new Quad(redquadsize, redquadsize, 200, 0, 0);
				RedGameQuad.SetSpeed(FP.rand(20) + 10);
				RedGameQuad.SetRotating(true);
				RedGameQuad.SetDirection(1);
				RedGameQuad.SetButton("mainmenured");
				RedGameQuad.SetHighlightColours(255, 0, 0);
				
				RedGameQuad.x = FP.rand(640);
				RedGameQuad.y = FP.rand(460);
				
				add(RedGameQuad);
			
			}
			
			if (!HasWonBlue)
			{
			
				var bluequadsize:int = FP.rand(20) + 10;
				
				BlueGameQuad = new Quad(bluequadsize, bluequadsize, 0, 0, 200);
				BlueGameQuad.SetSpeed(FP.rand(20) + 10);
				BlueGameQuad.SetRotating(true);
				BlueGameQuad.SetDirection(1);
				BlueGameQuad.SetButton("mainmenublue");
				BlueGameQuad.SetHighlightColours(0, 0, 255);
				
				BlueGameQuad.x = FP.rand(640);
				BlueGameQuad.y = FP.rand(460);
				
				add(BlueGameQuad);
			
			}
			
			if (!HasWonGreen)
			{
			
				var greenquadsize:int = FP.rand(20) + 10;

				GreenGameQuad = new Quad(greenquadsize, greenquadsize, 0, 200, 0);
				GreenGameQuad.SetSpeed(FP.rand(20) + 10);
				GreenGameQuad.SetRotating(true);
				GreenGameQuad.SetDirection(1);
				GreenGameQuad.SetButton("mainmenugreen");
				GreenGameQuad.SetHighlightColours(0, 255, 0);
				
				GreenGameQuad.x = FP.rand(640);
				GreenGameQuad.y = FP.rand(460);
				
				add(GreenGameQuad);
			
			}
			
		}
		
		private function GreenGameModeUpdate():void
		{
			
			//player dead
			if (gameplayer.IsDeadFlag)
			{
				
				remove(gameplayer);
				ClearQuads();
				ClearEnemies();
				InDeadToMainMenu = true;
				InGameModeGreen = false;
				backgroundtransitionblue = 11;
				backgroundtransitiongreen = 11;
				backgroundtransitionred = 11;
				GreenLevelTimer = 0;
				GreenBlocksFaster = false;
				GreenSpamBlocks = false;
				GreenBlockTimer = 0.5;
				
				return;
				
			}
			else if (gameplayer.VictoryFlag)
			{
				
				HasWonGreen = true;
				remove(gameplayer);
				ClearQuads();
				ClearEnemies();
				InDeadToMainMenu = true;
				InGameModeGreen = false;
				backgroundtransitionblue = 11;
				backgroundtransitiongreen = 11;
				backgroundtransitionred = 11;
				GreenLevelTimer = 0;
				GreenBlocksFaster = false;
				GreenSpamBlocks = false;
				GreenBlockTimer = 0.5;
				
				return;
				
			}
			
			var Q:Quad = collidePoint("quad", Input.mouseX, Input.mouseY) as Quad;
			
			if (Q)
			{
				Q.highlight();
			}
			
			GreenLevelTimer += FP.elapsed;
			
			if (GreenLevelTimer > 10 && GreenSpamBlocks != true)
			{
				GreenSpamBlocks = true;
			}
			
			if (GreenSpamBlocks)
			{
				
				GreenBlockTimer -= FP.elapsed;
				
				if (GreenBlockTimer <= 0)
				{
					
					if (GreenBlocksFaster)
					{
						GreenBlockTimer = 0.75;
					}
					else
					{
						GreenBlockTimer = 1;
					}
				
					var randsize:int = FP.rand(10) + 50;
				
					var myquad:Quad = new Quad(randsize, randsize, 0, FP.rand(50) + 175, 0);
					
					myquad.SetSpeed(FP.rand(20) + 250);
					myquad.SetDirection(4);
					
					myquad.SetSpecialQuad();
					
					myquad.SetNoFade();
					
					myquad.SetHighlightColours(0, 255, 0);//FP.rand(50) + 200);
					
					myquad.y = -75;
					
					myquad.x = FP.rand(400) + 100;
					
					add(myquad);
					
				}
				
			}
			
			if (GreenLevelTimer > 20 && GreenLevelTimer < 22)
			{

				var QuadList:Array = [];
				
				FP.world.getClass(Quad, QuadList);
				
				for each(var Qu:Quad in QuadList)
				{
					Qu.SpeedUp();
				}
				
				GreenBlocksFaster = true;
				
			}
			
			if (GreenLevelTimer > 37 && GreenLevelTimer < 40)
			{

				var QuadList2:Array = [];
				
				FP.world.getClass(Quad, QuadList2);
				
				for each(var Qua:Quad in QuadList2)
				{
					Qua.SpeedUp();
				}
				
				GreenBlocksFaster = true;
				
			}
			
		}
		
		private function BlueGameModeUpdate():void
		{
			
			//player dead
			if (gameplayer.IsDeadFlag)
			{
				
				remove(gameplayer);
				ClearQuads();
				ClearEnemies();
				InDeadToMainMenu = true;
				InGameModeBlue = false;
				backgroundtransitionblue = 11;
				backgroundtransitiongreen = 11;
				backgroundtransitionred = 11;
				BlueLevelTimer = 0;
				BlueLevelBlockCounter = 0;
				BlueLevelPhase = 0;
				
				return;
				
			}
			else if (gameplayer.VictoryFlag)
			{
				
				HasWonBlue = true;
				remove(gameplayer);
				ClearQuads();
				ClearEnemies();
				InDeadToMainMenu = true;
				InGameModeBlue = false;
				backgroundtransitionblue = 11;
				backgroundtransitiongreen = 11;
				backgroundtransitionred = 11;
				BlueLevelTimer = 0;
				BlueLevelBlockCounter = 0;
				BlueLevelPhase = 0;	
				
				return;
				
			}
			
			var Q:Quad = collidePoint("quad", Input.mouseX, Input.mouseY) as Quad;
			
			if (Q)
			{
				Q.highlight();
			}
			
			BlueLevelTimer += FP.elapsed;
			
			if (BlueLevelBlockCounter < 10)
			{
			
				if (BlueLevelTimer > 0.5)
				{
					
					BlueLevelTimer = 0;
					
					var randsize:int = FP.rand(10) + 50;
					
					var myquad:Quad = new Quad(randsize, randsize, 0, 0, FP.rand(50) + 175);
					
					myquad.SetSpeed(FP.rand(30) + 40);
					myquad.SetDirection(BlueStartDirection);
					
					myquad.SetNoFade();
					
					myquad.SetHighlightColours(0, 0, 255);
					
					myquad.x = FP.rand(680) - 20
					myquad.y = FP.rand(50) - 150;	
					
					add(myquad);
					
					BlueLevelBlockCounter++;
				}
			
			}
		
			if (BlueLevelTimer > 22 && BlueLevelPhase < 1)
			{
				
				var randomdirection:int = FP.rand(8);
				
				var QuadList:Array = [];
					
				FP.world.getClass(Quad, QuadList);
				
				for each(var Qu:Quad in QuadList)
				{
					Qu.SetDirection(randomdirection);
				}
				
				BlueLevelPhase = 1;
				
			}
			
			if (BlueLevelTimer > 34 && BlueLevelPhase < 2)
			{
				
				var newdirection:int = FP.rand(8);
				
				var QuadList2:Array = [];
					
				FP.world.getClass(Quad, QuadList2);
				
				for each(var Qua:Quad in QuadList2)
				{
					Qua.SetDirection(newdirection);
				}
				
				BlueLevelPhase = 2;
				
			}

		}
		
		private function RedGameModeUpdate():void
		{
			
			//player dead
			if (gameplayer.IsDeadFlag)
			{
				
				remove(gameplayer);
				ClearQuads();
				ClearEnemies();
				InDeadToMainMenu = true;
				InGameModeRed = false;
				backgroundtransitionblue = 11;
				backgroundtransitiongreen = 11;
				backgroundtransitionred = 11;
				RedLevelTimer = 0;
				
			}
			else if (gameplayer.VictoryFlag)
			{
				
				HasWonRed = true;
				remove(gameplayer);
				ClearQuads();
				ClearEnemies();
				InDeadToMainMenu = true;
				InGameModeRed = false;
				backgroundtransitionblue = 11;
				backgroundtransitiongreen = 11;
				backgroundtransitionred = 11;
				RedLevelTimer = 0;
				
				return;
				
			}
			
			var Q:Quad = collidePoint("quad", Input.mouseX, Input.mouseY) as Quad;
			
			if (Q)
			{
				Q.highlight();
			}
			
			RedLevelTimer += FP.elapsed;
			
			if (RedLevelTimer > 30 && RedLevelTimer < 35)
			{

				var QuadList:Array = [];
				
				FP.world.getClass(Quad, QuadList);
				
				for each(var Qu:Quad in QuadList)
				{
					Qu.SpeedUp();
				}
				
			}
	
			
		}
		
		private function MainMenuUpdate():void
		{
			
			var Q:Quad = collidePoint("quad", Input.mouseX, Input.mouseY) as Quad;
			
			if (Q)
			{
				Q.highlight();
			}
			
			var R:Quad = collidePoint("mainmenured", Input.mouseX, Input.mouseY) as Quad;
			
			if (R)
			{
				R.highlight();		
				
				if (Input.mouseReleased)
				{
					
					var QuadList:Array = [];
					
					FP.world.getClass(Quad, QuadList);
					
					for each(var Qu:Quad in QuadList)
					{
						Qu.SetFade(true);
					}
					
					RedGameQuad.SetFade(true);
					BlueGameQuad.SetFade(true);
					GreenGameQuad.SetFade(true);
					
					InMenuToGameModeRed = true;
					InMainMenu = false;
					
					backgroundtransitionblue = 0;
					backgroundtransitiongreen = 0;
					backgroundtransitionred = 100;
					
				}	
			}	
			
			var G:Quad = collidePoint("mainmenugreen", Input.mouseX, Input.mouseY) as Quad;
			
			if (G)
			{
				
				G.highlight();
				
				if (Input.mouseReleased)
				{
					var QuadList3:Array = [];
					
					FP.world.getClass(Quad, QuadList3);
					
					for each(var Quaz:Quad in QuadList3)
					{
						Quaz.SetFade(true);
					}
					
					RedGameQuad.SetFade(true);
					BlueGameQuad.SetFade(true);
					GreenGameQuad.SetFade(true);
					
					InMenuToGameModeGreen = true;
					InMainMenu = false;
					
					backgroundtransitionblue = 0;
					backgroundtransitiongreen = 100;
					backgroundtransitionred = 0;
					
				}
				
			}
			
			var B:Quad = collidePoint("mainmenublue", Input.mouseX, Input.mouseY) as Quad;
			
			if (B)
			{
				B.highlight();
				
				if (Input.mouseReleased)
				{
					var QuadList2:Array = [];
					
					FP.world.getClass(Quad, QuadList2);
					
					for each(var Qua:Quad in QuadList2)
					{
						Qua.SetFade(true);
					}
					
					RedGameQuad.SetFade(true);
					BlueGameQuad.SetFade(true);
					GreenGameQuad.SetFade(true);
					
					InMenuToGameModeBlue = true;
					InMainMenu = false;
					
					backgroundtransitionblue = 100;
					backgroundtransitiongreen = 0;
					backgroundtransitionred = 0;
					
				}
			}
			
		}	
		
		private function IntroScreenUpdate():void
		{
			
			if (Input.mouseReleased)
			{
				
				remove(intro);
			
				_background = new Background;
				
				_background.SetColour(11, 11, 11);
				
				add(_background);
				
				CreateMainMenu();
				
				InIntroScreen = false;
				InMainMenu = true;
			
			}
			
		}
		
		override public function update():void 
		{
			
			if (InIntroScreen)
			{
				IntroScreenUpdate();
			}
			else if (InMainMenu)
			{
				MainMenuUpdate();
			}
			else if (InDeadToMainMenu)
			{
				TransitionToMenuUpdate();
			}
			else if (InMenuToGameModeRed)
			{
				TransitionToGameModeRedUpdate();
			}
			else if (InGameModeRed)
			{
				RedGameModeUpdate();
			}
			else if (InMenuToGameModeBlue)
			{
				TransitionToGameModeBlueUpdate();
			}
			else if (InGameModeBlue)
			{
				BlueGameModeUpdate();
			}
			else if (InMenuToGameModeGreen)
			{
				TransitionToGameModeGreenUpdate();
			}
			else if (InGameModeGreen)
			{
				GreenGameModeUpdate();
			}
			
			super.update();
		}
		
	}

}