Skip to content

Commit

Permalink
merge infinite game
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielecirulli committed Mar 14, 2014
2 parents 2bf48ef + c329f35 commit a831bae
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 6 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ <h1 class="title">2048</h1>
<div class="game-message">
<p></p>
<div class="lower">
<a class="keep-playing-button">Keep playing</a>
<a class="retry-button">Try again</a>
</div>
</div>
Expand Down
23 changes: 21 additions & 2 deletions js/game_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function GameManager(size, InputManager, Actuator, ScoreManager) {

this.inputManager.on("move", this.move.bind(this));
this.inputManager.on("restart", this.restart.bind(this));
this.inputManager.on("keepPlaying", this.keepPlaying.bind(this));

this.setup();
}
Expand All @@ -18,13 +19,30 @@ GameManager.prototype.restart = function () {
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
GameManager.prototype.setup = function () {
this.grid = new Grid(this.size);

this.score = 0;
this.over = false;
this.won = false;
this.keepPlaying = false;

// Add the initial tiles
this.addStartTiles();
Expand Down Expand Up @@ -60,7 +78,8 @@ GameManager.prototype.actuate = function () {
score: this.score,
over: this.over,
won: this.won,
bestScore: this.scoreManager.get()
bestScore: this.scoreManager.get(),
gameOver: this.isGameOver()
});

};
Expand All @@ -87,7 +106,7 @@ GameManager.prototype.move = function (direction) {
// 0: up, 1: right, 2:down, 3: left
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;

Expand Down
16 changes: 13 additions & 3 deletions js/html_actuator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,22 @@ HTMLActuator.prototype.actuate = function (grid, metadata) {
self.updateScore(metadata.score);
self.updateBestScore(metadata.bestScore);

if (metadata.over) self.message(false); // You lose
if (metadata.won) self.message(true); // You win!
if (metadata.gameOver) {
if (metadata.over) self.message(false); // You lose
else if (metadata.won) self.message(true); // You win!
}

});
};

HTMLActuator.prototype.restart = function () {
this.clearMessage();
};

HTMLActuator.prototype.keepPlaying = function () {
this.clearMessage();
};

HTMLActuator.prototype.clearContainer = function (container) {
while (container.firstChild) {
container.removeChild(container.firstChild);
Expand All @@ -45,10 +52,13 @@ HTMLActuator.prototype.addTile = function (tile) {
var wrapper = document.createElement("div");
var inner = document.createElement("div");
var position = tile.previousPosition || { x: tile.x, y: tile.y };
positionClass = this.positionClass(position);
var positionClass = this.positionClass(position);

// We can't use classlist because it somehow glitches when replacing classes
var classes = ["tile", "tile-" + tile.value, positionClass];

if (tile.value > 2048) classes.push("tile-gold");

this.applyClasses(wrapper, classes);

inner.classList.add("tile-inner");
Expand Down
11 changes: 10 additions & 1 deletion js/keyboard_input_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ 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("touchend", this.restart.bind(this));

var keepPlaying = document.querySelector(".keep-playing-button");
keepPlaying.addEventListener("click", this.keepPlaying.bind(this));
keepPlaying.addEventListener("touchend", this.keepPlaying.bind(this));

// Listen to swipe events
var touchStartClientX, touchStartClientY;
var gameContainer = document.getElementsByClassName("game-container")[0];
Expand Down Expand Up @@ -93,3 +97,8 @@ KeyboardInputManager.prototype.restart = function (event) {
event.preventDefault();
this.emit("restart");
};

KeyboardInputManager.prototype.keepPlaying = function (event) {
event.preventDefault();
this.emit("keepPlaying");
};
4 changes: 4 additions & 0 deletions style/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,13 @@ hr {
height: 40px;
line-height: 42px;
margin-left: 9px; }
.game-container .game-message a.keep-playing-button {
display: none; }
.game-container .game-message.game-won {
background: rgba(237, 194, 46, 0.5);
color: #f9f6f2; }
.game-container .game-message.game-won a.keep-playing-button {
display: inline-block; }
.game-container .game-message.game-won, .game-container .game-message.game-over {
display: block; }

Expand Down
8 changes: 8 additions & 0 deletions style/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ hr {
@include button;
margin-left: 9px;
// margin-top: 59px;

&.keep-playing-button {
display: none;
}
}

@include animation(fade-in 800ms ease $transition-speed * 12);
Expand All @@ -226,6 +230,10 @@ hr {
&.game-won {
background: rgba($tile-gold-color, .5);
color: $bright-text-color;

a.keep-playing-button {
display: inline-block;
}
}

&.game-won, &.game-over {
Expand Down

0 comments on commit a831bae

Please sign in to comment.