Path: blob/main/projects/1/js/html_actuator.js
1834 views
function HTMLActuator() {1this.tileContainer = document.querySelector(".tile-container");2this.scoreContainer = document.querySelector(".score-container");3this.bestContainer = document.querySelector(".best-container");4this.messageContainer = document.querySelector(".game-message");5this.sharingContainer = document.querySelector(".score-sharing");67this.score = 0;8}910HTMLActuator.prototype.actuate = function (grid, metadata) {11var self = this;1213window.requestAnimationFrame(function () {14self.clearContainer(self.tileContainer);1516grid.cells.forEach(function (column) {17column.forEach(function (cell) {18if (cell) {19self.addTile(cell);20}21});22});2324self.updateScore(metadata.score);25self.updateBestScore(metadata.bestScore);2627if (metadata.terminated) {28if (metadata.over) {29self.message(false); // You lose30} else if (metadata.won) {31self.message(true); // You win!32}33}3435});36};3738// Continues the game (both restart and keep playing)39HTMLActuator.prototype.continue = function () {40this.clearMessage();41};4243HTMLActuator.prototype.clearContainer = function (container) {44while (container.firstChild) {45container.removeChild(container.firstChild);46}47};4849HTMLActuator.prototype.addTile = function (tile) {50var self = this;5152var wrapper = document.createElement("div");53var inner = document.createElement("div");54var position = tile.previousPosition || { x: tile.x, y: tile.y };55var positionClass = this.positionClass(position);5657// We can't use classlist because it somehow glitches when replacing classes58var classes = ["tile", "tile-" + tile.value, positionClass];5960if (tile.value > 2048) classes.push("tile-super");6162this.applyClasses(wrapper, classes);6364inner.classList.add("tile-inner");65inner.textContent = tile.value;6667if (tile.previousPosition) {68// Make sure that the tile gets rendered in the previous position first69window.requestAnimationFrame(function () {70classes[2] = self.positionClass({ x: tile.x, y: tile.y });71self.applyClasses(wrapper, classes); // Update the position72});73} else if (tile.mergedFrom) {74classes.push("tile-merged");75this.applyClasses(wrapper, classes);7677// Render the tiles that merged78tile.mergedFrom.forEach(function (merged) {79self.addTile(merged);80});81} else {82classes.push("tile-new");83this.applyClasses(wrapper, classes);84}8586// Add the inner part of the tile to the wrapper87wrapper.appendChild(inner);8889// Put the tile on the board90this.tileContainer.appendChild(wrapper);91};9293HTMLActuator.prototype.applyClasses = function (element, classes) {94element.setAttribute("class", classes.join(" "));95};9697HTMLActuator.prototype.normalizePosition = function (position) {98return { x: position.x + 1, y: position.y + 1 };99};100101HTMLActuator.prototype.positionClass = function (position) {102position = this.normalizePosition(position);103return "tile-position-" + position.x + "-" + position.y;104};105106HTMLActuator.prototype.updateScore = function (score) {107this.clearContainer(this.scoreContainer);108109var difference = score - this.score;110this.score = score;111112this.scoreContainer.textContent = this.score;113114if (difference > 0) {115var addition = document.createElement("div");116addition.classList.add("score-addition");117addition.textContent = "+" + difference;118119this.scoreContainer.appendChild(addition);120}121};122123HTMLActuator.prototype.updateBestScore = function (bestScore) {124this.bestContainer.textContent = bestScore;125};126127HTMLActuator.prototype.message = function (won) {128var type = won ? "game-won" : "game-over";129var message = won ? "You win!" : "Game over!";130131this.messageContainer.classList.add(type);132this.messageContainer.getElementsByTagName("p")[0].textContent = message;133134this.clearContainer(this.sharingContainer);135this.sharingContainer.appendChild(this.scoreTweetButton());136twttr.widgets.load();137};138139HTMLActuator.prototype.clearMessage = function () {140// IE only takes one value to remove at a time.141this.messageContainer.classList.remove("game-won");142this.messageContainer.classList.remove("game-over");143};144145HTMLActuator.prototype.scoreTweetButton = function () {146var tweet = document.createElement("a");147tweet.classList.add("twitter-share-button");148tweet.setAttribute("href", "https://twitter.com/share");149tweet.setAttribute("data-via", "3kh0");150tweet.setAttribute("data-url", "http://git.io/lrmEpA");151tweet.setAttribute("data-counturl", "http://3kh0.github.io/");152tweet.textContent = "Tweet";153154var text = "I scored " + this.score + " points at 1, a game where you " +155"join numbers to score high! #1game";156tweet.setAttribute("data-text", text);157158return tweet;159};160161162