add win condition
This commit is contained in:
+6
-3
@@ -8,6 +8,7 @@ function GameManager(size, InputManager, Actuator) {
|
||||
|
||||
this.score = 0;
|
||||
this.over = false;
|
||||
this.won = false;
|
||||
|
||||
this.inputManager.on("move", this.move.bind(this));
|
||||
|
||||
@@ -43,7 +44,8 @@ GameManager.prototype.addRandomTile = function () {
|
||||
GameManager.prototype.actuate = function () {
|
||||
this.actuator.actuate(this.grid, {
|
||||
score: this.score,
|
||||
over: this.over
|
||||
over: this.over,
|
||||
won: this.won
|
||||
});
|
||||
};
|
||||
|
||||
@@ -69,7 +71,7 @@ GameManager.prototype.move = function (direction) {
|
||||
// 0: up, 1: right, 2:down, 3: left
|
||||
var self = this;
|
||||
|
||||
if (this.over) return; // Don't do anything if the game's over
|
||||
if (this.over || this.won) return; // Don't do anything if the game's over
|
||||
|
||||
var cell, tile;
|
||||
|
||||
@@ -104,7 +106,8 @@ GameManager.prototype.move = function (direction) {
|
||||
// Update the score
|
||||
self.score += merged.value;
|
||||
|
||||
// Something's moved for sure
|
||||
// The mighty 2048 tile
|
||||
if (merged.value === 2048) self.won = true;
|
||||
} else {
|
||||
self.moveTile(tile, positions.farthest);
|
||||
}
|
||||
|
||||
+5
-5
@@ -22,9 +22,8 @@ HTMLActuator.prototype.actuate = function (grid, metadata) {
|
||||
|
||||
self.updateScore(metadata.score);
|
||||
|
||||
if (metadata.over) {
|
||||
self.gameOver();
|
||||
}
|
||||
if (metadata.over) self.message(false); // You lose
|
||||
if (metadata.won) self.message(true); // You win!
|
||||
});
|
||||
};
|
||||
|
||||
@@ -87,6 +86,7 @@ HTMLActuator.prototype.updateScore = function (score) {
|
||||
}
|
||||
};
|
||||
|
||||
HTMLActuator.prototype.gameOver = function () {
|
||||
this.gameContainer.classList.add("game-over");
|
||||
HTMLActuator.prototype.message = function (won) {
|
||||
var type = won ? "game-won" : "game-over";
|
||||
this.gameContainer.classList.add(type);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user