make actuator properly create html elements

This commit is contained in:
Gabriele Cirulli
2014-03-08 13:32:37 +01:00
parent dca5207fa4
commit b7476f1f62
3 changed files with 34 additions and 20 deletions
+1 -12
View File
@@ -46,18 +46,7 @@
</div>
<div class="tile-container">
<div class="tile tile-2 tile-position-1-1">
2
</div>
<div class="tile tile-4 tile-position-2-3">
4
</div>
<div class="tile tile-512 tile-position-3-2">
512
</div>
<div class="tile tile-2048 tile-position-4-4">
2048
</div>
</div>
</div>
+3 -1
View File
@@ -25,7 +25,9 @@ GameManager.prototype.addStartTiles = function () {
// Adds a tile in a random position
GameManager.prototype.addRandomTile = function () {
var tile = new Tile(this.grid.randomAvailableCell());
var value = Math.random() < 0.9 ? 2 : 4;
var tile = new Tile(this.grid.randomAvailableCell(), value);
this.grid.insertTile(tile);
};
+30 -7
View File
@@ -1,13 +1,36 @@
function HTMLActuator() {
this.tileContainer = document.getElementsByClassName("tile-container")[0];
}
HTMLActuator.prototype.actuate = function (grid) {
// Temporary debug visualizer
grid.cells.forEach(function (row) {
var mapped = row.map(function (tile) {
return tile ? tile.value : " ";
}).join(" | ");
console.log(mapped);
var self = this;
this.clearContainer();
grid.cells.forEach(function (column) {
column.forEach(function (cell) {
if (cell) {
self.addTile(cell);
}
});
});
};
HTMLActuator.prototype.clearContainer = function () {
while (this.tileContainer.firstChild) {
this.tileContainer.removeChild(this.tileContainer.firstChild);
}
};
HTMLActuator.prototype.addTile = function (tile) {
var element = document.createElement("div");
var x = tile.x + 1;
var y = tile.y + 1;
var position = "tile-position-" + x + "-" + y;
element.classList.add("tile", "tile-" + tile.value, position);
element.textContent = tile.value;
this.tileContainer.appendChild(element);
};