Path: blob/main/projects/flappy-2048/js/html_actuator.js
1834 views
function HTMLActuator() {1this.gridContainer = document.querySelector(".grid-container");2// this.tileContainer = document.querySelector(".tile-container");3this.scoreContainer = document.querySelector(".score-container");4this.bestContainer = document.querySelector(".best-container");5this.messageContainer = document.querySelector(".game-message");6this.birdobj = document.querySelector(".tile-bird");7this.birdinn = document.querySelector(".tile-bird .tile-inner");8this.blockobja = document.querySelector(".tile-block-a");9this.blockobjb = document.querySelector(".tile-block-b");10this.blockobjc = document.querySelector(".tile-block-c");11this.blockobjd = document.querySelector(".tile-block-d");12this.blockinna = document.querySelector(".tile-block-a .tile-inner");13this.blockinnb = document.querySelector(".tile-block-b .tile-inner");14this.blockinnc = document.querySelector(".tile-block-c .tile-inner");15this.blockinnd = document.querySelector(".tile-block-d .tile-inner");16}1718HTMLActuator.prototype.actuate = function (grid, metadata) {19var self = this;2021var classes = ["tile", "tile-bird"];2223var s = Math.floor(metadata.score);2425if (s > 2048) classes.push("tile-super")26else if (s > 1024) classes.push("tile-2048")27else if (s > 512) classes.push("tile-1024")28else if (s > 256) classes.push("tile-512")29else if (s > 128) classes.push("tile-256")30else if (s > 64) classes.push("tile-128")31else if (s > 32) classes.push("tile-64")32else if (s > 16) classes.push("tile-32")33else if (s > 8) classes.push("tile-16")34else if (s > 4) classes.push("tile-8")35else if (s > 2) classes.push("tile-4")36else classes.push("tile-2");3738this.applyClasses(this.birdobj, classes);3940var zonesize = this.gridContainer.clientHeight;41var morepos = 0.75 * (metadata.score - s);4243this.birdobj.style.top = metadata.birdpos * zonesize + "px";4445this.blockobja.style.top = [0.5 , 0 , 0 ][metadata.ab] * zonesize + "px";46this.blockobjb.style.top = [0.75, 0.75, 0.25][metadata.ab] * zonesize + "px";47this.blockobjc.style.top = [0.5 , 0 , 0 ][metadata.cd] * zonesize + "px";48this.blockobjd.style.top = [0.75, 0.75, 0.25][metadata.cd] * zonesize + "px";4950this.blockobja.style.left = (0.5 - morepos) * zonesize + "px";51this.blockobjb.style.left = (0.5 - morepos) * zonesize + "px";52this.blockobjc.style.left = (1.25 - morepos) * zonesize + "px";53this.blockobjd.style.left = (1.25 - morepos) * zonesize + "px";5455this.birdinn.textContent = s;5657window.requestAnimationFrame(function () {58self.updateScore(s);59self.updateBestScore(Math.floor(metadata.bestScore));60});61};6263// Continues the game (both restart and keep playing)64HTMLActuator.prototype.continue = function () {65this.clearMessage();66};6768HTMLActuator.prototype.clearContainer = function (container) {69while (container.firstChild) {70container.removeChild(container.firstChild);71}72};7374HTMLActuator.prototype.addTile = function (tile) {75var self = this;7677var wrapper = document.createElement("div");78var inner = document.createElement("div");79var position = tile.previousPosition || { x: tile.x, y: tile.y };80var positionClass = this.positionClass(position);8182// We can't use classlist because it somehow glitches when replacing classes83var classes = ["tile", "tile-" + tile.value, positionClass];8485if (tile.value > 2048) classes.push("tile-super");8687this.applyClasses(wrapper, classes);8889inner.classList.add("tile-inner");90inner.textContent = tile.value;9192if (tile.previousPosition) {93// Make sure that the tile gets rendered in the previous position first94window.requestAnimationFrame(function () {95classes[2] = self.positionClass({ x: tile.x, y: tile.y });96self.applyClasses(wrapper, classes); // Update the position97});98} else if (tile.mergedFrom) {99classes.push("tile-merged");100this.applyClasses(wrapper, classes);101102// Render the tiles that merged103tile.mergedFrom.forEach(function (merged) {104self.addTile(merged);105});106} else {107classes.push("tile-new");108this.applyClasses(wrapper, classes);109}110111// Add the inner part of the tile to the wrapper112wrapper.appendChild(inner);113114// Put the tile on the board115// this.tileContainer.appendChild(wrapper);116};117118HTMLActuator.prototype.applyClasses = function (element, classes) {119element.setAttribute("class", classes.join(" "));120};121122HTMLActuator.prototype.normalizePosition = function (position) {123return { x: position.x + 1, y: position.y + 1 };124};125126HTMLActuator.prototype.positionClass = function (position) {127position = this.normalizePosition(position);128return "tile-position-" + position.x + "-" + position.y;129};130131HTMLActuator.prototype.updateScore = function (score) {132//this.clearContainer(this.scoreContainer);133134var difference = score - this.score;135this.score = score;136137if (difference > 0) {138this.scoreContainer.textContent = this.score;139140var addition = document.createElement("div");141addition.classList.add("score-addition");142addition.textContent = "+" + difference;143144this.scoreContainer.appendChild(addition);145}146};147148HTMLActuator.prototype.updateBestScore = function (bestScore) {149this.bestContainer.textContent = bestScore;150};151152HTMLActuator.prototype.message = function (won) {153var type = won ? "game-won" : "game-over";154var message = won ? "You win!" : "Game over!";155156this.messageContainer.classList.add(type);157this.messageContainer.getElementsByTagName("p")[0].textContent = message;158};159160HTMLActuator.prototype.clearMessage = function () {161// IE only takes one value to remove at a time.162this.messageContainer.classList.remove("game-won");163this.messageContainer.classList.remove("game-over");164};165166167