remove debug logging
[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]; this.tileContainer = document.getElementsByClassName("tile-container")[0];
} }
   
HTMLActuator.prototype.actuate = function (grid) { HTMLActuator.prototype.actuate = function (grid) {
var self = this; var self = this;
   
window.requestAnimationFrame(function () { window.requestAnimationFrame(function () {
self.clearContainer(); self.clearContainer();
   
grid.cells.forEach(function (column) { grid.cells.forEach(function (column) {
column.forEach(function (cell) { column.forEach(function (cell) {
if (cell) { if (cell) {
self.addTile(cell); self.addTile(cell);
} }
}); });
}); });
}); });
}; };
   
HTMLActuator.prototype.clearContainer = function () { HTMLActuator.prototype.clearContainer = function () {
while (this.tileContainer.firstChild) { while (this.tileContainer.firstChild) {
this.tileContainer.removeChild(this.tileContainer.firstChild); this.tileContainer.removeChild(this.tileContainer.firstChild);
} }
}; };
   
HTMLActuator.prototype.addTile = function (tile) { HTMLActuator.prototype.addTile = function (tile) {
var self = this; var self = this;
   
var element = document.createElement("div"); var element = document.createElement("div");
var position = tile.previousPosition || { x: tile.x, y: tile.y }; var position = tile.previousPosition || { x: tile.x, y: tile.y };
positionClass = this.positionClass(position); positionClass = this.positionClass(position);
   
element.classList.add("tile", "tile-" + tile.value, positionClass); element.classList.add("tile", "tile-" + tile.value, positionClass);
element.textContent = tile.value; element.textContent = tile.value;
   
this.tileContainer.appendChild(element); this.tileContainer.appendChild(element);
   
   
if (tile.previousPosition) { if (tile.previousPosition) {
window.requestAnimationFrame(function () { window.requestAnimationFrame(function () {
// console.log( + " === " + positionClass);  
element.classList.remove(element.classList[2]); element.classList.remove(element.classList[2]);
element.classList.add(self.positionClass({ x: tile.x, y: tile.y })); element.classList.add(self.positionClass({ x: tile.x, y: tile.y }));
}); });
} else { } else {
element.classList.add("tile-new"); element.classList.add("tile-new");
} }
   
}; };
   
HTMLActuator.prototype.normalizePosition = function (position) { HTMLActuator.prototype.normalizePosition = function (position) {
return { x: position.x + 1, y: position.y + 1 }; return { x: position.x + 1, y: position.y + 1 };
}; };
   
HTMLActuator.prototype.positionClass = function (position) { HTMLActuator.prototype.positionClass = function (position) {
position = this.normalizePosition(position); position = this.normalizePosition(position);
return "tile-position-" + position.x + "-" + position.y; return "tile-position-" + position.x + "-" + position.y;
}; };
   
comments