fix merge conflict

This commit is contained in:
Gabriele Cirulli
2014-03-12 11:23:22 +01:00
7 changed files with 88 additions and 17 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
// Wait till the browser is ready to render the game (avoids glitches)
window.requestAnimationFrame(function () {
var manager = new GameManager(4, KeyboardInputManager, HTMLActuator);
new GameManager(4, KeyboardInputManager, HTMLActuator, LocalScoreManager);
});
+11 -4
View File
@@ -1,6 +1,7 @@
function GameManager(size, InputManager, Actuator) {
function GameManager(size, InputManager, Actuator, ScoreManager) {
this.size = size; // Size of the grid
this.inputManager = new InputManager;
this.scoreManager = new ScoreManager;
this.actuator = new Actuator;
this.startTiles = 2;
@@ -51,11 +52,17 @@ GameManager.prototype.addRandomTile = function () {
// Sends the updated grid to the actuator
GameManager.prototype.actuate = function () {
if (this.scoreManager.get() < this.score) {
this.scoreManager.set(this.score);
}
this.actuator.actuate(this.grid, {
score: this.score,
over: this.over,
won: this.won
score: this.score,
over: this.over,
won: this.won,
bestScore: this.scoreManager.get()
});
};
// Save all tile positions and remove merger info
+10 -2
View File
@@ -1,6 +1,7 @@
function HTMLActuator() {
this.tileContainer = document.querySelector(".tile-container");
this.scoreContainer = document.querySelector(".score-container");
this.bestContainer = document.querySelector(".best-container");
this.messageContainer = document.querySelector(".game-message");
this.score = 0;
@@ -21,6 +22,7 @@ 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!
@@ -103,11 +105,17 @@ HTMLActuator.prototype.updateScore = function (score) {
}
};
HTMLActuator.prototype.updateBestScore = function (bestScore) {
this.bestContainer.textContent = bestScore;
};
HTMLActuator.prototype.message = function (won) {
var type = won ? "game-won" : "game-over";
var message = won ? "You win!" : "Game over!"
var message = won ? "You win!" : "Game over!";
// if (ga) ga("send", "event", "game", "end", type, this.score);
if (typeof ga !== "undefined") {
ga("send", "event", "game", "end", type, this.score);
}
this.messageContainer.classList.add(type);
this.messageContainer.getElementsByTagName("p")[0].textContent = message;
+31
View File
@@ -0,0 +1,31 @@
window.fakeStorage = {
_data : {},
setItem : function (id, val) {
console.log('set');
return this._data[id] = String(val);
},
getItem : function (id) {
return this._data.hasOwnProperty(id) ? this._data[id] : undefined;
},
removeItem : function (id) { return delete this._data[id]; },
clear : function () { return this._data = {}; }
};
function LocalScoreManager() {
var localSupported = !!window.localStorage;
this.key = 'bestScore';
this.storage = localSupported ? window.localStorage : window.fakeStorage;
}
LocalScoreManager.prototype.get = function () {
var score = this.storage.getItem(this.key);
if (typeof score === "undefined" || score === null) {
score = 0;
}
return score;
};
LocalScoreManager.prototype.set = function (score) {
this.storage.setItem(this.key, score);
};