Skip to content

Commit

Permalink
Handle 'GameOver' sequence.
Browse files Browse the repository at this point in the history
  • Loading branch information
RandolphBurt committed Sep 15, 2013
1 parent 3d6d984 commit 332df9e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
7 changes: 7 additions & 0 deletions App/js/GameStateEnum.js
@@ -0,0 +1,7 @@
"use string";

var GameStateEnum = {
GameActive: 0,
LevelTransition: 1,
GameOver: 2
};
19 changes: 16 additions & 3 deletions App/js/services/GameEngine.js
Expand Up @@ -7,7 +7,6 @@ gameApp.factory('GameEngine', function(GraphicsEngine, GameBoard, GlobalSettings
this.canvas = canvas;

this.livesAnimationCount = 0;

this.centipedeUpperBoundary = (GlobalSettings.gameBoardHeight - GlobalSettings.playerAreaHeight) + 1;

this.resetBoard();
Expand Down Expand Up @@ -53,6 +52,9 @@ gameApp.factory('GameEngine', function(GraphicsEngine, GameBoard, GlobalSettings

update: function(animation) {
if (this.shouldPlayerRegenerate()) {
if (GameState.isGameOver()) {
GameState.reset();
}
this.resetBoard();
this.initialiseLevel();
return;
Expand Down Expand Up @@ -84,6 +86,7 @@ gameApp.factory('GameEngine', function(GraphicsEngine, GameBoard, GlobalSettings
this.drawCentipedes(animation);
this.drawBullets();
this.drawScoreMarkers();
this.drawGameState();
},

checkPlayerCollision: function() {
Expand Down Expand Up @@ -180,6 +183,17 @@ gameApp.factory('GameEngine', function(GraphicsEngine, GameBoard, GlobalSettings
GraphicsEngine.blankScreen();
},

drawGameState: function() {
if (GameState.isGameOver()) {
GraphicsEngine.drawText(
GlobalSettings.gameOverXPosition,
GlobalSettings.gameOverYPosition,
"Game Over",
GlobalSettings.gameOverFontColour,
GlobalSettings.gameOverFont);
}
},

drawScoreBoard: function(animation) {
if (animation == 0) {
this.livesAnimationCount++;
Expand All @@ -194,7 +208,6 @@ gameApp.factory('GameEngine', function(GraphicsEngine, GameBoard, GlobalSettings
animationOffset = 9 - this.livesAnimationCount;
}


GraphicsEngine.drawText(
GlobalSettings.scoreBoardLivesXPositionText,
GlobalSettings.scoreBoardTitleYPosition,
Expand Down Expand Up @@ -244,7 +257,7 @@ gameApp.factory('GameEngine', function(GraphicsEngine, GameBoard, GlobalSettings
GlobalSettings.scoreBoardContentFontColour,
GlobalSettings.scoreBoardFont);

for (var i = 0; i < GameState.lives - 1; i++) {
for (var i = 0; i < GameState.lives; i++) {
GraphicsEngine.drawImage(
GlobalSettings.scoreBoardLivesXPositionImage + (GlobalSettings.scoreBoardLivesOffset * i) + (animationOffset * 4),
GlobalSettings.scoreBoardLivesYPosition,
Expand Down
10 changes: 10 additions & 0 deletions App/js/services/GameState.js
Expand Up @@ -4,11 +4,16 @@ gameApp.factory("GameState", function(GlobalSettings) {
highScore: 0,
level: 1,
lives: GlobalSettings.lives,
gameState: GameStateEnum.GameActive,

isPlayerAlive: function() {
return this.lives > 0;
},

isGameOver: function() {
return this.gameState === GameStateEnum.GameOver;
},

isCurrentLevelHighSpeed: function() {
return this.level % 2 == 0;
},
Expand All @@ -17,6 +22,7 @@ gameApp.factory("GameState", function(GlobalSettings) {
this.lives = GlobalSettings.lives;
this.level = 1;
this.score = 0;
this.gameState = GameStateEnum.GameActive;
},

incrementScore: function(increment) {
Expand All @@ -31,6 +37,10 @@ gameApp.factory("GameState", function(GlobalSettings) {
if (this.lives > 0) {
this.lives--;
}

if (this.lives === 0) {
this.gameState = GameStateEnum.GameOver;
}
}
}
});
7 changes: 6 additions & 1 deletion App/js/services/GlobalSettings.js
Expand Up @@ -19,7 +19,12 @@ gameApp.constant("GlobalSettings", {
scoreBoardTitleYPosition: 15,
scoreBoardContentYPosition: 35,
scoreBoardLivesYPosition: 20,
scoreBoardLivesOffset: 20,
scoreBoardLivesOffset: 15,

gameOverXPosition: 210,
gameOverYPosition: 300,
gameOverFontColour: "yellow",
gameOverFont: "32px Arial bold",

gameBoardWidth: 30,
gameBoardHeight: 30,
Expand Down

0 comments on commit 332df9e

Please sign in to comment.