/*
	Deamons Of Space
	A JavaScript Side-Scroller
	by Mr Speaker
	http://www.mrspeaker.net/
	mrspeaker@gmail.com
*/

//Game class constants//
	kGameIntro = 0;
	kGameSetup = 1;
	kGameGetReady = 2;
	kGameRun = 3;
	kGameEnd = 4;

	kFrameRate = 50; //the speed the game will run at

	//movement directions
	kUp = 1;
	kLeft = 4;
	kDown = 3;
	kRight = 2;

	//misc
	kImageFolder = "images/";
	debug = true;

//The main game class
function Game(pDivName, pX, pY, pWidth, pHeight){
	//game globals
	this.worldPos;
	this.screenWidth=pWidth;
	this.screenHeight=pHeight;
	this.xOff = pX;
	this.yOff = pY;
	
	this.status;

	this.highScore = 0;
	
	this.timer=null;
	this.frameRate = kFrameRate;
	
	this.screen = new GameScreen(pDivName, pWidth, pHeight, pX, pY);
	this.map; //object of type "map"
	
	//util globals
	this.counter = 0;
	this.counter2 = 0;
	
	//game methods
	this.start=function(){
		this.init();
		this.run();
	}

	this.init = function(){
		this.status=kGameIntro;
		this.counter = 0;
		this.counter2 = 0;
		this.screen = $( "Screen" );
		this.screen.innerHTML = "";
		this.screen.innerHTML+="<div id='game_title'></div>";
		this.screen.innerHTML+="<div id='game_score'></div>";
	}

	this.updateScore = function(){
		DrawString(0,0,"#CCCCCC", "<p style='width=80;text-align:left;font-family:verdana;font-size:7pt;'><img src=images/title1s.gif><br>score:" + this.P.score + "</p>", "game_score");
	}
	
	 /*------ MAIN GAME LOOP ------*/
   this.run = function(){
   /*----------------------------*/
    	clearTimeout(this.timer);
		switch(this.status){
			case kGameIntro:
				this.gameIntro(this.counter++);
				break;
			case kGameSetup:
				this.gameSetup();
				this.counter=0;
				this.status=kGameGetReady;
				break;
			case kGameGetReady:
				this.gameGetReady(this.counter++);
				break;
			case kGameRun:
				this.gameRun();
				break;
			case kGameEnd:
				this.gameLevelOver(this.counter++);
				break;
		}
		this.timer=setTimeout("TheGame.run()",this.frameRate);
	}
	
	/*------ Game Introduction Screen -------*/
	this.gameIntro = function(pFrameCount){
	/*---------------------------------------*/
		if(pFrameCount==0){
			this.Explosion1 = new Explosion(this.screenWidth-Math.floor(Math.random()*50)-25,20,1, this.screen);
			this.Explosion1.init();
		
			Layers = new Array(	new Array(-5, "#FFFFFF"), new Array(-3, "#888888"));
			this.StarController = new StarLayerController(Layers, this.screen);
			textFade1 = new TextFader(
				this.screenWidth,
				this.screenHeight/2-70, 
				30, 
				true, 
				["<img src=images/title1.gif><br><br>The game","<img src=images/title1.gif><br><Br>By Mr Speaker","<img src=images/title1.gif><br><Br>Controls<br><br>A,S,D,W : move<br>L : fire<br>K : brake"], 
				"game_title"
			);
			textFade1.start();
		}
		this.Explosion1.draw();
		if(pFrameCount%19==1){
			this.Explosion1.reset((this.screenWidth/2)+(Math.floor(Math.random()*200))-120,(this.screenHeight/2)-(Math.floor(Math.random()*40))-55, 1);
		}
		if(pFrameCount%180==1){textFade1.start();}
		textFade1.update();
		this.StarController.update(0);
	}
	
	/*---------Game Setup------*/
	this.gameSetup = function(){
	/*-------------------------*/
		this.worldPos = 0;
		this.map1 = new Map(level1, level1length);
				
		this.Explosion1.reset(-40,-40,0); //hide explosion
		
		this.P = new Player((this.screenWidth/2)-16,(this.screenHeight/2)-16,1,this.screen);
		
		//trying a different way to group the baddies...
		//BaddieArray = new Array(
		//	new Array(this.screenWidth+10,50),
		//	new Array(this.screenWidth+40,100));
		//this.BaddieController = new BaddieGroupController(BaddieArray, Screen);
		this.BulletController1 = new BulletController(5, this.screen);
	}
	
	/*-------- Game GET READY ---------*/
	this.gameGetReady = function(pCount){
	/*---------------------------------*/
		if(pCount==0){
			this.levelPos=0;
			this.P.x=(this.screenWidth/2)-16;
			this.P.y=(this.screenHeight/2)-16;
			textFade1 = new TextFader(this.screenWidth,this.screenHeight/2-40, 20, false, ["Round 1"], "game_title")
			textFade1.start();
		}
		else textFade1.update();
		
		if(pCount==25){
			textFade1.stop();
			this.counter=0;
			this.status=kGameRun;
		}
	}
	
	/*--------Game Run--------*/
	this.gameRun = function(){
	/*------------------------*/
		if(this.worldPos==this.map1.length){
			this.status=kGameEnd;
		}
		
		this.P.update();
		this.P.checkCol(this.map1);

		this.BulletController1.checkCol(this.map1);
		this.BulletController1.update();
		
		if(this.Explosion1.status!=0)this.Explosion1.draw();			
		
		this.map1.checkLevel(this.worldPos++); 
		this.map1.update();
		this.updateScore();
		
		if(this.P.xdir==kRight)
			tmpXvel=0-this.P.xvelocity
		else 
			tmpXvel=this.P.xvelocity-10;
		this.StarController.update(tmpXvel);
		
	}
	
	/*---------Game Level over------------*/
	this.gameLevelOver = function(pFrameCount){
	/*------------------------------------*/
		if(pFrameCount==0){
			textFade1 = new TextFader(this.screenWidth,this.screenHeight/2-70, 30, true, ["Level Complete","The only level.","Score: "+this.P.score,"Lives lost: "+this.P.lives,"Well done.", "Game Over"], "game_title")
			textFade1.start();
		}
		this.BulletController1.update();
		this.map1.update();
		if(this.Explosion1.status!=0)this.Explosion1.draw();	
		
		this.StarController.update(0);
		textFade1.update();
		
		if(pFrameCount==200)
		{
			this.init();
		}
	}
	
   
   	//Key handler logic
	this.keyLEFT = function(){
		this.P.xdir=kLeft;
		this.P.facing=kLeft;
	}
	this.keyRIGHT = function(){
		this.P.xdir=kRight;
		this.P.facing=kRight;
	}
	this.keyUP = function(){
		this.P.ydir=kUp;
	}
	this.keyDOWN = function(){
		this.P.ydir=kDown;
	}
	this.keySPACE = function(){
		if(this.P.xvelocity>0)
			this.P.xdir=0;
	}
	this.keyRIGHT_CTRL = function(){
		this.BulletController1.start();
	}
	
}
