2014-03-08 12:50:45 +01:00
|
|
|
function HTMLActuator() {
|
2014-03-09 23:03:13 +01:00
|
|
|
this.tileContainer = document.getElementsByClassName("tile-container")[0];
|
|
|
|
this.gameContainer = document.getElementsByClassName("game-container")[0];
|
|
|
|
this.scoreContainer = document.getElementsByClassName("score-container")[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 () {
|
|
|
|
self.clearContainer();
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (metadata.over) {
|
|
|
|
self.gameOver();
|
|
|
|
}
|
2014-03-08 12:50:45 +01:00
|
|
|
});
|
|
|
|
};
|
2014-03-08 13:32:37 +01:00
|
|
|
|
|
|
|
HTMLActuator.prototype.clearContainer = function () {
|
|
|
|
while (this.tileContainer.firstChild) {
|
|
|
|
this.tileContainer.removeChild(this.tileContainer.firstChild);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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-09 14:32:30 +01:00
|
|
|
element.classList.add("tile", "tile-" + tile.value, positionClass);
|
2014-03-08 13:32:37 +01:00
|
|
|
element.textContent = tile.value;
|
|
|
|
|
|
|
|
this.tileContainer.appendChild(element);
|
2014-03-09 14:32:30 +01:00
|
|
|
|
|
|
|
if (tile.previousPosition) {
|
|
|
|
window.requestAnimationFrame(function () {
|
|
|
|
element.classList.remove(element.classList[2]);
|
|
|
|
element.classList.add(self.positionClass({ x: tile.x, y: tile.y }));
|
|
|
|
});
|
2014-03-09 20:03:04 +01:00
|
|
|
} else if (tile.mergedFrom) {
|
|
|
|
element.classList.add("tile-merged");
|
|
|
|
tile.mergedFrom.forEach(function (merged) {
|
|
|
|
self.addTile(merged);
|
|
|
|
});
|
2014-03-09 14:32:30 +01:00
|
|
|
} else {
|
|
|
|
element.classList.add("tile-new");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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) {
|
|
|
|
this.scoreContainer.textContent = score;
|
|
|
|
};
|
|
|
|
|
|
|
|
HTMLActuator.prototype.gameOver = function () {
|
|
|
|
this.gameContainer.classList.add("game-over");
|
|
|
|
};
|