Path: blob/main/projects/doge2048/js/html_actuator.js
1836 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.info = document.querySelector(".info");6this.dogeSays = document.querySelector(".doge-says");7this.adSpace = document.querySelector(".shout-out");89this.score = 0;10}1112var dogeSayings = ['such good', 'so amaze', 'many points', 'very unstoppable', 'great jorb', 'such playing', 'very good', 'points', 'very gaming', 'such player', 'concern' ,'bewildered', 'pro gamer', 'many game', 'so good', 'very scores', 'so scoring', 'so hot right now', 'such playing', 'such matching', 'so matched', 'very matched', 'very neat' ,'such natural',]1314HTMLActuator.prototype.actuate = function (grid, metadata) {15var self = this;1617window.requestAnimationFrame(function () {18self.clearContainer(self.tileContainer);1920grid.cells.forEach(function (column) {21column.forEach(function (cell) {22if (cell) {23self.addTile(cell);24}25});26});2728self.updateScore(metadata.score);29self.updateBestScore(metadata.bestScore);3031if (metadata.terminated) {32if (metadata.over) {33self.message(false); // You lose34} else if (metadata.won) {35self.message(true); // You win!36}37}3839});40};4142// Continues the game (both restart and keep playing)43HTMLActuator.prototype.continue = function () {44this.clearMessage();45};4647HTMLActuator.prototype.clearContainer = function (container) {48while (container.firstChild) {49container.removeChild(container.firstChild);50}51};5253HTMLActuator.prototype.addTile = function (tile) {54var self = this;5556var wrapper = document.createElement("div");57var inner = document.createElement("div");58var position = tile.previousPosition || { x: tile.x, y: tile.y };59var positionClass = this.positionClass(position);6061// We can't use classlist because it somehow glitches when replacing classes62var classes = ["tile", "tile-" + tile.value, positionClass];6364if (tile.value > 2048) classes.push("tile-super");6566this.applyClasses(wrapper, classes);6768inner.classList.add("tile-inner");69inner.textContent = tile.value;7071if (tile.previousPosition) {72// Make sure that the tile gets rendered in the previous position first73window.requestAnimationFrame(function () {74classes[2] = self.positionClass({ x: tile.x, y: tile.y });75self.applyClasses(wrapper, classes); // Update the position76});77} else if (tile.mergedFrom) {78classes.push("tile-merged");79this.applyClasses(wrapper, classes);8081// Render the tiles that merged82tile.mergedFrom.forEach(function (merged) {83self.addTile(merged);84});85} else {86classes.push("tile-new");87this.applyClasses(wrapper, classes);88}8990// Add the inner part of the tile to the wrapper91wrapper.appendChild(inner);9293// Put the tile on the board94this.tileContainer.appendChild(wrapper);95};9697HTMLActuator.prototype.applyClasses = function (element, classes) {98element.setAttribute("class", classes.join(" "));99};100101HTMLActuator.prototype.normalizePosition = function (position) {102return { x: position.x + 1, y: position.y + 1 };103};104105HTMLActuator.prototype.positionClass = function (position) {106position = this.normalizePosition(position);107return "tile-position-" + position.x + "-" + position.y;108};109110HTMLActuator.prototype.updateScore = function (score) {111this.clearContainer(this.scoreContainer);112this.clearContainer(this.dogeSays)113114var difference = score - this.score;115this.score = score;116117this.scoreContainer.textContent = this.score;118119if (difference > 0) {120var addition = document.createElement("div");121addition.classList.add("score-addition");122addition.textContent = "+" + difference;123this.scoreContainer.appendChild(addition);124125var message = dogeSayings[Math.floor(Math.random() * dogeSayings.length)]126var messageElement = document.createElement("p");127messageElement.textContent = message128var left = 'left:' + Math.round(Math.random() * 80) + '%;'129var top = 'top:' + Math.round(Math.random() * 80) + '%;'130var color = 'color: rgb(' + Math.round(Math.random() * 255) + ', ' + Math.round(Math.random() * 255) + ', ' + Math.round(Math.random() * 255) + ');'131var styleString = left + top + color132messageElement.setAttribute('style', styleString);133this.dogeSays.appendChild(messageElement);134if (difference > 4) {135this.adSpace.innerHTML = ads[Math.floor(Math.random() * ads.length)]136}137138}139};140141HTMLActuator.prototype.updateBestScore = function (bestScore) {142this.bestContainer.textContent = bestScore;143};144145HTMLActuator.prototype.message = function (won) {146var type = won ? "game-won" : "game-over";147var message = won ? "You win!" : "Game over!";148149this.messageContainer.classList.add(type);150this.messageContainer.getElementsByTagName("p")[0].textContent = message;151};152153HTMLActuator.prototype.clearMessage = function () {154// IE only takes one value to remove at a time.155this.messageContainer.classList.remove("game-won");156this.messageContainer.classList.remove("game-over");157};158159160HTMLActuator.prototype.showInfo = function () {161if ( this.info.getAttribute('style') === "display:block;"){162this.info.setAttribute('style','display:none;')163document.querySelector('.show-info').innerHTML = 'INFO';164} else {165this.info.setAttribute('style','display:block;')166document.querySelector('.show-info').innerHTML = 'CLOSE';167}168}169170HTMLActuator.prototype.hideInfo = function () {171this.info.setAttribute('style','display:none;')172}173174175