make actuator properly create html elements
[2048.git] / js / html_actuator.js
blob:a/js/html_actuator.js -> blob:b/js/html_actuator.js
function HTMLActuator() { function HTMLActuator() {
  this.tileContainer = document.getElementsByClassName("tile-container")[0];
} }
   
HTMLActuator.prototype.actuate = function (grid) { HTMLActuator.prototype.actuate = function (grid) {
// Temporary debug visualizer var self = this;
grid.cells.forEach(function (row) {  
var mapped = row.map(function (tile) { this.clearContainer();
return tile ? tile.value : " ";  
}).join(" | "); grid.cells.forEach(function (column) {
console.log(mapped); 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);
  };
   
comments