make actuator properly create html elements
[2048.git] / js / html_actuator.js
blob:a/js/html_actuator.js -> blob:b/js/html_actuator.js
--- a/js/html_actuator.js
+++ b/js/html_actuator.js
@@ -1,14 +1,37 @@
 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);
+};
+

comments