Endless Game. Allow user to continue the game.
- main.css isn't compiled - a special class `.tile-gold` is added to tiles above 2048
This commit is contained in:
@@ -26,6 +26,7 @@
|
|||||||
<div class="game-message">
|
<div class="game-message">
|
||||||
<p></p>
|
<p></p>
|
||||||
<div class="lower">
|
<div class="lower">
|
||||||
|
<a class="keep-playing-button">Keep playing</a>
|
||||||
<a class="retry-button">Try again</a>
|
<a class="retry-button">Try again</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
+21
-2
@@ -8,6 +8,7 @@ function GameManager(size, InputManager, Actuator, ScoreManager) {
|
|||||||
|
|
||||||
this.inputManager.on("move", this.move.bind(this));
|
this.inputManager.on("move", this.move.bind(this));
|
||||||
this.inputManager.on("restart", this.restart.bind(this));
|
this.inputManager.on("restart", this.restart.bind(this));
|
||||||
|
this.inputManager.on("keepPlaying", this.keepPlaying.bind(this));
|
||||||
|
|
||||||
this.setup();
|
this.setup();
|
||||||
}
|
}
|
||||||
@@ -18,6 +19,22 @@ GameManager.prototype.restart = function () {
|
|||||||
this.setup();
|
this.setup();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Keep playing after winning
|
||||||
|
GameManager.prototype.keepPlaying = function () {
|
||||||
|
this.keepPlaying = true;
|
||||||
|
this.actuator.keepPlaying();
|
||||||
|
};
|
||||||
|
|
||||||
|
GameManager.prototype.isGameOver = function() {
|
||||||
|
if (this.over || (this.won && !this.keepPlaying) ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Set up the game
|
// Set up the game
|
||||||
GameManager.prototype.setup = function () {
|
GameManager.prototype.setup = function () {
|
||||||
this.grid = new Grid(this.size);
|
this.grid = new Grid(this.size);
|
||||||
@@ -25,6 +42,7 @@ GameManager.prototype.setup = function () {
|
|||||||
this.score = 0;
|
this.score = 0;
|
||||||
this.over = false;
|
this.over = false;
|
||||||
this.won = false;
|
this.won = false;
|
||||||
|
this.keepPlaying = false;
|
||||||
|
|
||||||
// Add the initial tiles
|
// Add the initial tiles
|
||||||
this.addStartTiles();
|
this.addStartTiles();
|
||||||
@@ -60,7 +78,8 @@ GameManager.prototype.actuate = function () {
|
|||||||
score: this.score,
|
score: this.score,
|
||||||
over: this.over,
|
over: this.over,
|
||||||
won: this.won,
|
won: this.won,
|
||||||
bestScore: this.scoreManager.get()
|
bestScore: this.scoreManager.get(),
|
||||||
|
gameOver: this.isGameOver()
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -87,7 +106,7 @@ GameManager.prototype.move = function (direction) {
|
|||||||
// 0: up, 1: right, 2:down, 3: left
|
// 0: up, 1: right, 2:down, 3: left
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
if (this.over || this.won) return; // Don't do anything if the game's over
|
if (this.isGameOver()) return; // Don't do anything if the game's over
|
||||||
|
|
||||||
var cell, tile;
|
var cell, tile;
|
||||||
|
|
||||||
|
|||||||
+23
-6
@@ -24,8 +24,11 @@ HTMLActuator.prototype.actuate = function (grid, metadata) {
|
|||||||
self.updateScore(metadata.score);
|
self.updateScore(metadata.score);
|
||||||
self.updateBestScore(metadata.bestScore);
|
self.updateBestScore(metadata.bestScore);
|
||||||
|
|
||||||
if (metadata.over) self.message(false); // You lose
|
if (metadata.gameOver) {
|
||||||
if (metadata.won) self.message(true); // You win!
|
if (metadata.over) self.message(false); // You lose
|
||||||
|
else if (metadata.won) self.message(true); // You win!
|
||||||
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -33,6 +36,10 @@ HTMLActuator.prototype.restart = function () {
|
|||||||
this.clearMessage();
|
this.clearMessage();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
HTMLActuator.prototype.keepPlaying = function () {
|
||||||
|
this.clearMessage();
|
||||||
|
}
|
||||||
|
|
||||||
HTMLActuator.prototype.clearContainer = function (container) {
|
HTMLActuator.prototype.clearContainer = function (container) {
|
||||||
while (container.firstChild) {
|
while (container.firstChild) {
|
||||||
container.removeChild(container.firstChild);
|
container.removeChild(container.firstChild);
|
||||||
@@ -42,12 +49,13 @@ HTMLActuator.prototype.clearContainer = function (container) {
|
|||||||
HTMLActuator.prototype.addTile = function (tile) {
|
HTMLActuator.prototype.addTile = function (tile) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var element = document.createElement("div");
|
var element = document.createElement("div");
|
||||||
var position = tile.previousPosition || { x: tile.x, y: tile.y };
|
var position = tile.previousPosition || { x: tile.x, y: tile.y };
|
||||||
positionClass = this.positionClass(position);
|
var positionClass = this.positionClass(position);
|
||||||
|
var styleClass = this.styleClass(tile);
|
||||||
|
|
||||||
// We can't use classlist because it somehow glitches when replacing classes
|
// We can't use classlist because it somehow glitches when replacing classes
|
||||||
var classes = ["tile", "tile-" + tile.value, positionClass];
|
var classes = ["tile", styleClass, positionClass];
|
||||||
this.applyClasses(element, classes);
|
this.applyClasses(element, classes);
|
||||||
|
|
||||||
element.textContent = tile.value;
|
element.textContent = tile.value;
|
||||||
@@ -88,6 +96,15 @@ HTMLActuator.prototype.positionClass = function (position) {
|
|||||||
return "tile-position-" + position.x + "-" + position.y;
|
return "tile-position-" + position.x + "-" + position.y;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
HTMLActuator.prototype.styleClass = function (tile) {
|
||||||
|
var className = 'tile-'+tile.value;
|
||||||
|
|
||||||
|
if (tile.value > 2048 ) {
|
||||||
|
className = className+' tile-gold';
|
||||||
|
}
|
||||||
|
return className;
|
||||||
|
};
|
||||||
|
|
||||||
HTMLActuator.prototype.updateScore = function (score) {
|
HTMLActuator.prototype.updateScore = function (score) {
|
||||||
this.clearContainer(this.scoreContainer);
|
this.clearContainer(this.scoreContainer);
|
||||||
|
|
||||||
|
|||||||
@@ -53,9 +53,12 @@ KeyboardInputManager.prototype.listen = function () {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var retry = document.getElementsByClassName("retry-button")[0];
|
var retry = document.querySelector(".retry-button");
|
||||||
retry.addEventListener("click", this.restart.bind(this));
|
retry.addEventListener("click", this.restart.bind(this));
|
||||||
|
|
||||||
|
var keepPlaying = document.querySelector(".keep-playing-button");
|
||||||
|
keepPlaying.addEventListener("click", this.keepPlaying.bind(this));
|
||||||
|
|
||||||
// Listen to swipe events
|
// Listen to swipe events
|
||||||
var touchStartClientX, touchStartClientY;
|
var touchStartClientX, touchStartClientY;
|
||||||
var gameContainer = document.getElementsByClassName("game-container")[0];
|
var gameContainer = document.getElementsByClassName("game-container")[0];
|
||||||
@@ -92,3 +95,8 @@ KeyboardInputManager.prototype.restart = function (event) {
|
|||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
this.emit("restart");
|
this.emit("restart");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
KeyboardInputManager.prototype.keepPlaying = function (event) {
|
||||||
|
event.preventDefault();
|
||||||
|
this.emit("keepPlaying");
|
||||||
|
};
|
||||||
|
|||||||
@@ -218,6 +218,10 @@ hr {
|
|||||||
@include button;
|
@include button;
|
||||||
margin-left: 9px;
|
margin-left: 9px;
|
||||||
// margin-top: 59px;
|
// margin-top: 59px;
|
||||||
|
|
||||||
|
&.keep-playing-button {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@include animation(fade-in 800ms ease $transition-speed * 12);
|
@include animation(fade-in 800ms ease $transition-speed * 12);
|
||||||
@@ -226,6 +230,10 @@ hr {
|
|||||||
&.game-won {
|
&.game-won {
|
||||||
background: rgba($tile-gold-color, .5);
|
background: rgba($tile-gold-color, .5);
|
||||||
color: $bright-text-color;
|
color: $bright-text-color;
|
||||||
|
|
||||||
|
a.keep-playing-button {
|
||||||
|
display: inline-block;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
&.game-won, &.game-over {
|
&.game-won, &.game-over {
|
||||||
|
|||||||
Reference in New Issue
Block a user