add retry button

This commit is contained in:
Gabriele Cirulli
2014-03-10 11:31:10 +01:00
parent 2c066a55fc
commit 02b66ccb9b
6 changed files with 114 additions and 51 deletions
+16 -7
View File
@@ -4,19 +4,28 @@ function GameManager(size, InputManager, Actuator) {
this.actuator = new Actuator;
this.startTiles = 2;
this.inputManager.on("move", this.move.bind(this));
this.inputManager.on("restart", this.restart.bind(this));
this.setup();
}
// Restart the game
GameManager.prototype.restart = function () {
this.actuator.restart();
this.setup();
};
// Set up the game
GameManager.prototype.setup = function () {
this.grid = new Grid(this.size);
this.score = 0;
this.over = false;
this.won = false;
this.inputManager.on("move", this.move.bind(this));
this.setup();
}
// Set up the game
GameManager.prototype.setup = function () {
// Add the initial tiles
this.addStartTiles();
// Update the actuator
+19 -6
View File
@@ -1,7 +1,7 @@
function HTMLActuator() {
this.tileContainer = document.getElementsByClassName("tile-container")[0];
this.gameContainer = document.getElementsByClassName("game-container")[0];
this.scoreContainer = document.getElementsByClassName("score-container")[0];
this.tileContainer = document.getElementsByClassName("tile-container")[0];
this.scoreContainer = document.getElementsByClassName("score-container")[0];
this.messageContainer = document.getElementsByClassName("game-message")[0];
this.score = 0;
}
@@ -27,6 +27,10 @@ HTMLActuator.prototype.actuate = function (grid, metadata) {
});
};
HTMLActuator.prototype.restart = function () {
this.clearMessage();
};
HTMLActuator.prototype.clearContainer = function (container) {
while (container.firstChild) {
container.removeChild(container.firstChild);
@@ -77,7 +81,7 @@ HTMLActuator.prototype.updateScore = function (score) {
this.scoreContainer.textContent = this.score;
if (difference) {
if (difference > 0) {
var addition = document.createElement("div");
addition.classList.add("score-addition");
addition.textContent = "+" + difference;
@@ -87,6 +91,15 @@ HTMLActuator.prototype.updateScore = function (score) {
};
HTMLActuator.prototype.message = function (won) {
var type = won ? "game-won" : "game-over";
this.gameContainer.classList.add(type);
if (ga) ga("send", "event", "game", "end", type, this.score);
var type = won ? "game-won" : "game-over";
var message = won ? "You win!" : "Game over!"
this.messageContainer.classList.add(type);
this.messageContainer.getElementsByTagName("p")[0].textContent = message;
};
HTMLActuator.prototype.clearMessage = function () {
this.messageContainer.classList.remove("game-won", "game-over");
};
+6
View File
@@ -40,4 +40,10 @@ KeyboardInputManager.prototype.listen = function () {
self.emit("move", mapped);
}
});
var retry = document.getElementsByClassName("retry-button")[0];
retry.addEventListener("click", function (event) {
event.preventDefault();
self.emit("restart");
});
};