HTML5 Canvas [159]
}
function switchGameState(newState) {
currentGameState = newState;
switch (currentGameState) {
case GAME_STATE_TITLE:
currentGameStateFunction = gameStateTitle;
break;
case GAME_STATE_NEW_GAME:
currentGameStateFunction = gameStateNewGame;
break;
case GAME_STATE_NEW_LEVEL:
currentGameStateFunction = gameStateNewLevel;
break;
case GAME_STATE_PLAYER_START:
currentGameStateFunction = gameStatePlayerStart;
break;
case GAME_STATE_PLAY_LEVEL:
currentGameStateFunction = gameStatePlayLevel;
break;
case GAME_STATE_PLAYER_DIE:
currentGameStateFunction = gameStatePlayerDie;
break;
case GAME_STATE_GAME_OVER:
currentGameStateFunction = gameStateGameOver;
break;
}
}
function gameStateTitle() {
if (titleStarted !=true){
fillBackground();
setTextStyle();
context.fillText ("Geo Blaster Basic", 130, 70);
context.fillText ("Press Space To Play", 120, 140);
titleStarted = true;
}else{
//wait for space key click
if (keyPressList[32]==true){
ConsoleLog.log("space pressed");
switchGameState(GAME_STATE_NEW_GAME);
titleStarted = false;
}
}
}
function gameStateNewGame(){
ConsoleLog.log("gameStateNewGame")
//set up new game
level = 0;
score = 0;
playerShips = 3;
player.maxVelocity = 5;
player.width = 20;
player.height = 20;
player.halfWidth = 10;
player.halfHeight = 10;
player.rotationalVelocity = 5; //how many degrees to turn the ship
player.thrustAcceleration = .05;
player.missileFrameDelay = 5;
player.thrust = false;
fillBackground();
renderScoreBoard();
switchGameState(GAME_STATE_NEW_LEVEL)
}
function gameStateNewLevel(){
rocks = [];
saucers = [];
playerMissiles = [];
particles = [];
saucerMissiles = [];
level++;
levelRockMaxSpeedAdjust = level*.25;
if (levelRockMaxSpeedAdjust > 3){
levelRockMaxSpeed = 3;
}
levelSaucerMax = 1+Math.floor(level/10);
if (levelSaucerMax > 5){
levelSaucerMax = 5;
}
levelSaucerOccurrenceRate = 10+3*level;
if (levelSaucerOccurrenceRate > 35){
levelSaucerOccurrenceRate = 35;
}
levelSaucerSpeed = 1+.5*level;
if (levelSaucerSpeed>5){
levelSaucerSpeed = 5;
}
levelSaucerFireDelay = 120-10*level;
if (levelSaucerFireDelay<20) {
levelSaucerFireDelay = 20;
}
levelSaucerFireRate = 20 + 3*level;
if (levelSaucerFireRate<50) {
levelSaucerFireRate = 50;
}
levelSaucerMissileSpeed = 1+.2*level;
if (levelSaucerMissileSpeed > 4){
levelSaucerMissileSpeed = 4;
}
//create level rocks
for (var newRockctr=0;newRockctr newRock.scale = 1; //scale //1 = large //2 = medium //3 = small //these will be used as the divisor for the new size //50/1 = 50 //50/2 = 25 //50/3 = 16 newRock.width = 50; newRock.height = 50; newRock.halfWidth = 25; newRock.halfHeight = 25; //start all new rocks in upper left for ship safety newRock.x = Math.floor(Math.random()*50); //ConsoleLog.log("newRock.x=" + newRock.x); newRock.y = Math.floor(Math.random()*50); //ConsoleLog.log("newRock.y=" + newRock.y); newRock.dx = (Math.random()*2)+levelRockMaxSpeedAdjust; if (Math.random()<.5){ newRock.dx*=-1; } newRock.dy = (Math.random()*2)+levelRockMaxSpeedAdjust; if (Math.random()<.5){ newRock.dy*=-1; } //rotation speed and direction newRock.rotationInc = (Math.random()*5)+1; if (Math.random()<.5){ newRock.rotationInc*=-1; } newRock.scoreValue = bigRockScore; newRock.rotation = 0; rocks.push(newRock); //ConsoleLog.log("rock created rotationInc=" + newRock.rotationInc); } resetPlayer(); switchGameState(GAME_STATE_PLAYER_START); } function gameStatePlayerStart(){ fillBackground(); renderScoreBoard(); if (player.alpha < 1){ player.alpha += .02; context.globalAlpha = player.alpha; }else{ switchGameState(GAME_STATE_PLAY_LEVEL); } renderPlayerShip(player.x, player.y,270,1); context.globalAlpha = 1; updateRocks(); renderRocks(); } function gameStatePlayLevel(){ checkKeys(); update(); render(); checkCollisions(); checkForExtraShip(); checkForEndOfLevel(); frameRateCounter.countFrames(); } function resetPlayer() { player.rotation