/*
teleport in a tas?
*/

//Héritage
DrWho.prototype  = new Player ( );
Dalek.prototype = new Player ( );



//Variables pour le jeu
var nbDaleks;
var nbDaleksInit = 5;
var nbDrWhos = 1;
var nbScrew;
var daleksKilledThisLevel;
var playerArray; //Array qui contient tous les joueurs
var level;
var isGameOver = true;
var idAnim_array;
var highscore = 25;
var score = 0;

//Grid
var theGrid;
var blocSize = 32; //size in pixels
var gridX = 20;
var gridY = 16;
var GRID_TOP 	= 0;
var GRID_LEFT 	= 0;
var GRID_BOTTOM = gridY;
var GRID_RIGHT 	= gridX;

//Sounds
var flashObj = "soundController";
var soundCount = 0;

function newGame(){

	level = 1;
	nbScrew = 2;
	nbDaleks = nbDaleksInit;
	isGameOver = false;
	updateLevel();
	updateHighScore();
	score = 0;
	updateScore();
	updateScrew();
	daleksKilledThisLevel = 0;
	
	if ( playerArray != undefined ) {
	
		stopAnimation();
		
	} else {
	
		document.getElementById("aireDeJeu" ).className = "gamebg";	
		
	}
	
	init();	 //init the game
}

//Verifie si tous les daleks sont morts
function checkWin(){

	if (daleksKilledThisLevel == nbDaleks){
	
		level++;
		nbDaleks +=5;
		daleksKilledThisLevel = 0;
		nbScrew++;
		updateLevel();
		updateScore();
		updateScrew();
		stopAnimation();		
		init();
		
		return true;
		
	} else {
	
		return false;
	
	}
	
}


//initialise un nouveau niveau
function init( ) {

	theGrid = new Grid ( ); //create new grid
	playerArray = new Array ( nbDaleks + nbDrWhos ); //new player array
	theGrid.initGrid();		//init the grid with drwho and daleks ( creates our divs automatically )
	theGrid.displayGame();  //display the players on the grid
	idAnim_array= new Array(nbDaleks + nbDrWhos);	
	
}

//deplacement avec la souris
function move ( e ) {
	
	if ( !isGameOver ) {		

		if ( !checkWin( ) ) {

			mouseX = Math.floor ( e.x / blocSize );
			mouseY = Math.floor ( e.y / blocSize );

			//verifier si le deplacement est valide
			if ( theGrid.isMoveValid( mouseX, mouseY ) ) {

				playerArray[0].move( mouseX , mouseY );		
				moveDaleks ( );		
				stopAnimation( );
				theGrid.animateGame();		
				updateScore ( );

			}
			
		}
		
	} 
		
}

//deplacement avec le clavvier
function keyboardMove ( ) {

	evt = window.event;
	keypressed = evt.keyCode;

	if ( !isGameOver ) {
	
		if ( !checkWin( ) ) {

			if ( 	keypressed == 97  || keypressed == 98  || keypressed == 99  ||
					keypressed == 100 || keypressed == 101 || keypressed == 102 ||
					keypressed == 103 || keypressed == 104 || keypressed == 105 ) {		
				//top
				if ( keypressed == 104 ) {
					newX = playerArray[0]._x;
					newY = playerArray[0]._y - 1;
				//left
				} else if ( keypressed == 100 ) {
					newX = playerArray[0]._x - 1;
					newY = playerArray[0]._y;
				//right
				} else if ( keypressed == 102 ) {
					newX = playerArray[0]._x + 1;
					newY = playerArray[0]._y;			
				//down
				} else if ( keypressed == 98 ) {
					newX = playerArray[0]._x;
					newY = playerArray[0]._y + 1;			
				//up-left
				} else if ( keypressed == 103 ) {
					newX = playerArray[0]._x - 1;
					newY = playerArray[0]._y - 1;			
				//up-right
				} else if ( keypressed == 105 ) {
					newX = playerArray[0]._x + 1;
					newY = playerArray[0]._y - 1;
				//down-left
				} else if ( keypressed == 97 ) {
					newX = playerArray[0]._x - 1;
					newY = playerArray[0]._y + 1;			
				//down-right
				} else if ( keypressed == 99 ) {
					newX = playerArray[0]._x + 1;
					newY = playerArray[0]._y + 1;			
				//dont move
				} else {
					newX = playerArray[0]._x;
					newY = playerArray[0]._y;				
				}

				//verifier si le deplacement est valid
				if ( theGrid.isMoveValid( newX, newY ) ) {			

					playerArray[0].move( newX , newY );			
					moveDaleks ( );	
					stopAnimation( );
					theGrid.animateGame();
					updateScore ( );

				} 

			// la touche 'T'
			} else if ( keypressed == 84 ) { 

				teleport ( );

			// la touche 'S'			
			} else if ( keypressed == 83 ) { 

				if ( screwdriver ( ) ) {
					moveDaleks ( );	
					theGrid.animateGame();
				}

			} else if ( keypressed == 78 ) { 

				if ( confirm ( "Are you sure you want to start a new game?" ) ) {
					newGame ( );
				}	

			}
			
		}
		
	} else if ( keypressed == 78 ) { 
	
		newGame ( );
	
	}
	
}

function moveDaleks ( ) {

	for ( i = nbDrWhos; i < playerArray.length; i++ ){	
	
		if ( playerArray[i].isAlive ) {
						
			//move dalek towards drwho
			playerArray[i].move ( playerArray[0]._x , playerArray[0]._y );
			playerArray[i].hasMoved = true;
									
		} 
		
	}

	theGrid.newGrid( );
	theGrid.reDraw( );
	
}

function teleport ( ) {

	if ( !isGameOver ) {

		do {
		
			playerArray[0].teleport ( );	
			
		} while ( theGrid.checkCollsion ( theGrid.grid,playerArray[0]._x,playerArray[0]._y ) );

		moveDaleks ( );	
		theGrid.animateGame();
		
	}

}	

function screwdriver ( ) { 

	if ( nbScrew > 0 && !isGameOver ) { 


		//Position xy de DrWho
		x = playerArray[0]._x;
		y = playerArray[0]._y;

		nbScrew--;
		updateScrew();
		
		//top left
		if ( x == GRID_LEFT && y == GRID_TOP ) { 
			theGrid.screwDalek ( x + 1 , y );
			theGrid.screwDalek ( x + 1 , y + 1 );
			theGrid.screwDalek ( x , y + 1 );
		//top right
		} else if ( x == GRID_RIGHT - 1 && y == GRID_TOP ) { 

			theGrid.screwDalek ( x - 1 , y );
			theGrid.screwDalek ( x - 1 , y + 1 );
			theGrid.screwDalek ( x , y + 1 );

		
		//bottom right
		} else if ( x == GRID_RIGHT - 1 && y == GRID_BOTTOM - 1 ) { 

			theGrid.screwDalek ( x  , y - 1 );
			theGrid.screwDalek ( x - 1 , y - 1 );
			theGrid.screwDalek ( x - 1, y );

		//bottom left
		} else if ( x == GRID_LEFT && y == GRID_BOTTOM - 1 ) { 

			theGrid.screwDalek ( x  , y - 1 );
			theGrid.screwDalek ( x + 1 , y - 1 );
			theGrid.screwDalek ( x + 1, y );

				
		//top
		} else if ( y == GRID_TOP ) { 

			theGrid.screwDalek ( x - 1 , y );
			theGrid.screwDalek ( x + 1 , y );
			theGrid.screwDalek ( x - 1, y + 1 );
			theGrid.screwDalek ( x + 1, y + 1 );
			theGrid.screwDalek ( x, y + 1 );

		//left 
		} else if ( x == GRID_LEFT ) { 

			theGrid.screwDalek ( x, y - 1 );
			theGrid.screwDalek ( x + 1 , y - 1);
			theGrid.screwDalek ( x + 1, y );
			theGrid.screwDalek ( x + 1, y + 1 );
			theGrid.screwDalek ( x, y + 1 );

		//right
		} else if ( x == GRID_RIGHT - 1 ) { 

			theGrid.screwDalek ( x, y - 1 );
			theGrid.screwDalek ( x, y + 1 );			
			theGrid.screwDalek ( x - 1 , y - 1);
			theGrid.screwDalek ( x - 1, y + 1 );
			theGrid.screwDalek ( x - 1, y );
			

		//bottom
		} else if ( y == GRID_BOTTOM - 1 ) { 

			theGrid.screwDalek ( x, y - 1 );
			theGrid.screwDalek ( x - 1 , y );
			theGrid.screwDalek ( x - 1, y - 1 );
			theGrid.screwDalek ( x + 1, y - 1 );
			theGrid.screwDalek ( x + 1 , y );

		
		//elsewhere...
		} else { 
		
			theGrid.screwDalek ( x - 1 , y );
			theGrid.screwDalek ( x - 1 , y - 1);
			theGrid.screwDalek ( x - 1 , y + 1);
			theGrid.screwDalek ( x + 1 , y - 1);
			theGrid.screwDalek ( x + 1 , y )
			theGrid.screwDalek ( x + 1 , y + 1);
			theGrid.screwDalek ( x , y - 1);
			theGrid.screwDalek ( x , y + 1);
					
		}		
		
		//animation of the screwdriver
		doc = document.getElementById("sonic").style;
		doc.visibility = "visible";
		doc.backgroundImage = "url('images/sonic.gif')";
		doc.top  = y * blocSize - blocSize;
		doc.left = x * blocSize - blocSize;
		
		//screwdriver 
		return true;
		
	} else {
		
		//il ne reste plus de screwdriver ou la partie est terminée
		return false;
		
	}

}

//mettre le deplacement des joueurs à faux
function resetMove ( ) { 
		
	for ( i = 1 ; i < playerArray.length; i++ ) {
	
		playerArray[i].hasMoved = false;
	
	}
	
}


//forcer l'arret de l'animation 
function stopAnimation ( ) {
	
	for ( i = 0; i < idAnim_array.length; i++ ){
	
		clearInterval ( idAnim_array[i] );
		
	}
	
}

//mise a jour des pts
function updateScore ( ) { 

	var layersObj = document.all("score");
	layersObj.innerHTML = "Score:  " + score;

}

//mise a jour du niveau
function updateLevel ( ) { 

	var layersObj = document.all("level");
	layersObj.innerHTML = "Level:  " + level;

}

//mise a jour des screwdrivers
function updateScrew ( ) { 

	var layersObj = document.all("screw");
	layersObj.innerHTML = "Screwdriver:  " + nbScrew;

}

//mise a jour du highscore
function updateHighScore ( ) { 
	

	if ( score > highscore ) {
	
		highscore = score;
		
	}
	
	var layersObj = document.all("highscore");
	layersObj.innerHTML = "Highscore :  " + highscore;


}

//jouer un son
function playSound ( soundName ) {
	var flashplayer = document.getElementById ("soundController");
	flashplayer.TGotoLabel("_level0/", soundName );	
}

