HTML5 Canvas [196]
break;
case GAME_STATE_EVALUATE_OUTCOME:
currentGameStateFunction = gameStateEvaluateOutcome;
break;
case GAME_STATE_ANIMATE_EXPLODE:
currentGameStateFunction = gameStateAnimateExplode;
break;
case GAME_STATE_CHECK_FOR_GAME_OVER:
currentGameStateFunction = gameStateCheckForGameOver;
break;
case GAME_STATE_PLAYER_WIN:
currentGameStateFunction = gameStatePlayerWin;
break;
case GAME_STATE_PLAYER_LOSE:
currentGameStateFunction = gameStatePlayerLose;
break;
}
}
function gameStateWaitForLoad(){
//do nothing while loading events occur
//console.log("doing nothing...")
}
function gameStateInit() {
tileSheet = new Image();
tileSheet.src = "tanks_sheet.png";
tileSheet.onload = itemLoaded;
switchGameState(GAME_STATE_WAIT_FOR_LOAD);
}
function itemLoaded(event) {
loadCount++;
////console.log("loading:" + loadCount)
if (loadCount >= itemsToLoad) {
switchGameState(GAME_STATE_TITLE)
}
}
function gameStateTitle() {
if (screenStarted !=true){
fillBackground();
setTextStyleTitle();
context.fillText ("Micro Tank Maze", 160, 70);
context.fillText ("Press Space To Play", 150, 140);
screenStarted = true;
}else{
//wait for space key click
if (keyPressList[32]==true){
//console.log("space pressed");
switchGameState(GAME_STATE_NEW_GAME);
screenStarted = false;
}
}
}
function gameStatePlayerWin(){
if (!screenStarted){
score += goalScore;
fillBackground();
setTextStyleTitle();
context.fillText ("YOU WON THE GAME!", 135, 70);
context.fillText ("Final Score: " + score, 150, 100);
context.fillText ("Number of enemy: " + enemyMax, 150,130);
if (score > highScore){
highScore = score;
context.fillText ("NEW HIGH SCORE!", 150,160);
}
context.fillText ("High Score: " + score, 150, 190);
screenStarted = true;
enemyMax++;
if (enemyMax >50){
enemyMax = 50;
}
context.fillText ("Number of enemy for next game: " +
enemyMax, 100,220);
context.fillText ("Press Space To Play", 150, 300);
}else{
//wait for space key click
if (keyPressList[32]==true){
//console.log("space pressed");
switchGameState(GAME_STATE_NEW_GAME);
screenStarted = false;
}
}
}
function gameStatePlayerLose(){
if (!screenStarted){
fillBackground();
setTextStyleTitle();
context.fillText ("SORRY, YOU LOST THE GAME!", 100, 70);
context.fillText ("Final Score: " + score, 150, 100);
context.fillText ("Number of enemy: " + enemyMax, 150,130);
if (score > highScore){
highScore = score;
context.fillText ("NEW HIGH SCORE!", 150,160);
}
context.fillText ("High Score: " + score, 150, 190);
screenStarted = true;
context.fillText ("Number of enemy for next game: " +
enemyMax, 100,220);
context.fillText ("Press Space To Play", 150, 300);
}else{
//wait for space key click
if (keyPressList[32]==true){
//console.log("space pressed");
switchGameState(GAME_STATE_NEW_GAME);
screenStarted = false;
}
}
}
function gameStateNewGame(){
score = 0;
enemy = [];
explosions = [];
playField = [];
items = [];
resetPlayer();
createPlayField();
renderPlayField();
switchGameState(GAME_STATE_WAIT_FOR_PLAYER_MOVE);
}
function createPlayField(){
var wallCount = 0;
var playerCount = 0;
var enemyCount = 0;
var goalCount = 0;
var roadCount = 0;
//fill with road
for (var rowCtr=0;rowCtr<15;rowCtr++){
var tempRow = [];
for (colCtr=0;colCtr<15;colCtr++) {
tempRow.push(roadTile)
}
playField.push(tempRow);
}
//console.log("playField=" + playField);
//create items array
for (rowCtr=0;rowCtr<15;rowCtr++){
var tempRow = [];
for (colCtr=0;colCtr<15;colCtr++) {
tempRow.push(0);
}
items.push(tempRow);
}
var randRow;
var randCol;
//placewalls
for (var wallCtr=0;wallCtr while(!wallLocationFound){ randRow = Math.floor(Math.random()*15); randCol = Math.floor(Math.random()*15); if (playField[randRow][randCol]==roadTile){ playField[randRow][randCol] = wallTile; wallLocationFound = true; } } } //place enemy for (var enemyCtr=0;enemyCtr