2014-03-08 12:50:45 +01:00
|
|
|
function HTMLActuator() {
|
2014-03-12 08:59:12 +01:00
|
|
|
this.tileContainer = document.querySelector(".tile-container");
|
|
|
|
this.scoreContainer = document.querySelector(".score-container");
|
2014-03-12 11:23:22 +01:00
|
|
|
this.bestContainer = document.querySelector(".best-container");
|
2014-03-12 08:59:12 +01:00
|
|
|
this.messageContainer = document.querySelector(".game-message");
|
2014-03-09 23:24:17 +01:00
|
|
|
|
|
|
|
this.score = 0;
|
2014-03-08 12:50:45 +01:00
|
|
|
}
|
|
|
|
|
2014-03-09 23:03:13 +01:00
|
|
|
HTMLActuator.prototype.actuate = function (grid, metadata) {
|
2014-03-08 13:32:37 +01:00
|
|
|
var self = this;
|
|
|
|
|
2014-03-09 14:32:30 +01:00
|
|
|
window.requestAnimationFrame(function () {
|
2014-03-09 23:24:17 +01:00
|
|
|
self.clearContainer(self.tileContainer);
|
2014-03-09 14:32:30 +01:00
|
|
|
|
|
|
|
grid.cells.forEach(function (column) {
|
|
|
|
column.forEach(function (cell) {
|
|
|
|
if (cell) {
|
|
|
|
self.addTile(cell);
|
|
|
|
}
|
|
|
|
});
|
2014-03-08 13:32:37 +01:00
|
|
|
});
|
2014-03-09 23:03:13 +01:00
|
|
|
|
|
|
|
self.updateScore(metadata.score);
|
2014-03-10 21:02:16 +01:00
|
|
|
self.updateBestScore(metadata.bestScore);
|
2014-03-09 23:03:13 +01:00
|
|
|
|
2014-03-09 23:43:56 +01:00
|
|
|
if (metadata.over) self.message(false); // You lose
|
|
|
|
if (metadata.won) self.message(true); // You win!
|
2014-03-08 12:50:45 +01:00
|
|
|
});
|
|
|
|
};
|
2014-03-08 13:32:37 +01:00
|
|
|
|
2014-03-10 11:31:10 +01:00
|
|
|
HTMLActuator.prototype.restart = function () {
|
|
|
|
this.clearMessage();
|
|
|
|
};
|
|
|
|
|
2014-03-09 23:24:17 +01:00
|
|
|
HTMLActuator.prototype.clearContainer = function (container) {
|
|
|
|
while (container.firstChild) {
|
|
|
|
container.removeChild(container.firstChild);
|
2014-03-08 13:32:37 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
HTMLActuator.prototype.addTile = function (tile) {
|
2014-03-09 14:32:30 +01:00
|
|
|
var self = this;
|
2014-03-08 13:32:37 +01:00
|
|
|
|
2014-03-09 14:32:30 +01:00
|
|
|
var element = document.createElement("div");
|
|
|
|
var position = tile.previousPosition || { x: tile.x, y: tile.y };
|
|
|
|
positionClass = this.positionClass(position);
|
2014-03-08 13:32:37 +01:00
|
|
|
|
2014-03-10 18:03:43 +01:00
|
|
|
// We can't use classlist because it somehow glitches when replacing classes
|
|
|
|
var classes = ["tile", "tile-" + tile.value, positionClass];
|
|
|
|
this.applyClasses(element, classes);
|
2014-03-08 13:32:37 +01:00
|
|
|
|
2014-03-10 18:03:43 +01:00
|
|
|
element.textContent = tile.value;
|
2014-03-09 14:32:30 +01:00
|
|
|
|
|
|
|
if (tile.previousPosition) {
|
2014-03-10 18:03:43 +01:00
|
|
|
// Make sure that the tile gets rendered in the previous position first
|
2014-03-09 14:32:30 +01:00
|
|
|
window.requestAnimationFrame(function () {
|
2014-03-10 18:03:43 +01:00
|
|
|
classes[2] = self.positionClass({ x: tile.x, y: tile.y });
|
|
|
|
self.applyClasses(element, classes); // Update the position
|
2014-03-09 14:32:30 +01:00
|
|
|
});
|
2014-03-09 20:03:04 +01:00
|
|
|
} else if (tile.mergedFrom) {
|
2014-03-10 18:03:43 +01:00
|
|
|
classes.push("tile-merged");
|
|
|
|
this.applyClasses(element, classes);
|
|
|
|
|
|
|
|
// Render the tiles that merged
|
2014-03-09 20:03:04 +01:00
|
|
|
tile.mergedFrom.forEach(function (merged) {
|
|
|
|
self.addTile(merged);
|
|
|
|
});
|
2014-03-09 14:32:30 +01:00
|
|
|
} else {
|
2014-03-10 18:03:43 +01:00
|
|
|
classes.push("tile-new");
|
|
|
|
this.applyClasses(element, classes);
|
2014-03-09 14:32:30 +01:00
|
|
|
}
|
2014-03-10 18:03:43 +01:00
|
|
|
|
|
|
|
// Put the tile on the board
|
|
|
|
this.tileContainer.appendChild(element);
|
|
|
|
};
|
|
|
|
|
|
|
|
HTMLActuator.prototype.applyClasses = function (element, classes) {
|
|
|
|
element.setAttribute("class", classes.join(" "));
|
2014-03-09 14:32:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
HTMLActuator.prototype.normalizePosition = function (position) {
|
|
|
|
return { x: position.x + 1, y: position.y + 1 };
|
|
|
|
};
|
|
|
|
|
|
|
|
HTMLActuator.prototype.positionClass = function (position) {
|
|
|
|
position = this.normalizePosition(position);
|
|
|
|
return "tile-position-" + position.x + "-" + position.y;
|
2014-03-08 13:32:37 +01:00
|
|
|
};
|
2014-03-09 23:03:13 +01:00
|
|
|
|
|
|
|
HTMLActuator.prototype.updateScore = function (score) {
|
2014-03-09 23:24:17 +01:00
|
|
|
this.clearContainer(this.scoreContainer);
|
|
|
|
|
|
|
|
var difference = score - this.score;
|
|
|
|
this.score = score;
|
|
|
|
|
|
|
|
this.scoreContainer.textContent = this.score;
|
|
|
|
|
2014-03-10 11:31:10 +01:00
|
|
|
if (difference > 0) {
|
2014-03-09 23:24:17 +01:00
|
|
|
var addition = document.createElement("div");
|
|
|
|
addition.classList.add("score-addition");
|
|
|
|
addition.textContent = "+" + difference;
|
|
|
|
|
|
|
|
this.scoreContainer.appendChild(addition);
|
|
|
|
}
|
2014-03-09 23:03:13 +01:00
|
|
|
};
|
|
|
|
|
2014-03-10 21:02:16 +01:00
|
|
|
HTMLActuator.prototype.updateBestScore = function (bestScore) {
|
2014-03-11 16:52:02 +01:00
|
|
|
this.bestContainer.textContent = bestScore;
|
2014-03-10 21:02:16 +01:00
|
|
|
};
|
|
|
|
|
2014-03-09 23:43:56 +01:00
|
|
|
HTMLActuator.prototype.message = function (won) {
|
2014-03-10 11:31:10 +01:00
|
|
|
var type = won ? "game-won" : "game-over";
|
2014-03-10 22:18:54 +01:00
|
|
|
var message = won ? "You win!" : "Game over!";
|
2014-03-10 11:31:10 +01:00
|
|
|
|
2014-03-10 22:18:54 +01:00
|
|
|
if (typeof ga !== "undefined") {
|
|
|
|
ga("send", "event", "game", "end", type, this.score);
|
|
|
|
}
|
2014-03-10 19:38:46 +01:00
|
|
|
|
2014-03-10 11:31:10 +01:00
|
|
|
this.messageContainer.classList.add(type);
|
|
|
|
this.messageContainer.getElementsByTagName("p")[0].textContent = message;
|
|
|
|
};
|
|
|
|
|
|
|
|
HTMLActuator.prototype.clearMessage = function () {
|
|
|
|
this.messageContainer.classList.remove("game-won", "game-over");
|
2014-03-09 23:03:13 +01:00
|
|
|
};
|