PageSourceSearch

https://voxogenic.com/static/js_2048/html_actuator.js

js voxogenic.com collected 2026-10-03 21:17:43 UTC 4,384 bytes, 156 lines download raw bytes

1function HTMLActuator() {
2  this.tileContainer    = document.querySelector(".tile-container");
3  this.scoreContainer   = document.querySelector(".score-container");
4  this.bestContainer    = document.querySelector(".best-container");
5  this.messageContainer = document.querySelector(".game-message");
6
7  this.score = 0;
8}
9
10HTMLActuator.prototype.actuate = function (grid, metadata) {
11  var self = this;
12
13  window.requestAnimationFrame(function () {
14    self.clearContainer(self.tileContainer);
15
16    grid.cells.forEach(function (column) {
17      column.forEach(function (cell) {
18        if (cell) {
19          self.addTile(cell);
20        }
21      });
22    });
23
24    self.updateScore(metadata.score);
25    self.updateBestScore(metadata.bestScore);
26    
27    if (metadata.pause) {
28		self.message1(true);
29 	}    
30
31
32    if (metadata.terminated) {
33      if (metadata.over) {
34        self.message(false); // You lose
35      } else if (metadata.won) {
36        self.message(true); // You win!
37      }
38    }
39
40  });
41};
42
43// Continues the game (both restart and keep playing)
44HTMLActuator.prototype.continueGame = function () {
45  this.clearMessage();
46};
47
48HTMLActuator.prototype.clearContainer = function (container) {
49  while (container.firstChild) {
50    container.removeChild(container.firstChild);
51  }
52};
53
54HTMLActuator.prototype.addTile = function (tile) {
55  var self = this;
56
57  var wrapper   = document.createElement("div");
58  var inner     = document.createElement("div");
59  var position  = tile.previousPosition || { x: tile.x, y: tile.y };
60  var positionClass = this.positionClass(position);
61
62  // We can't use classlist because it somehow glitches when replacing classes
63  var classes = ["tile", "tile-" + tile.value, positionClass];
64
65  if (tile.value > 2048) classes.push("tile-super");
66
67  this.applyClasses(wrapper, classes);
68
69  inner.classList.add("tile-inner");
70  inner.textContent = tile.value;
71
72  if (tile.previousPosition) {
73    // Make sure that the tile gets rendered in the previous position first
74    window.requestAnimationFrame(function () {
75      classes[2] = self.positionClass({ x: tile.x, y: tile.y });
76      self.applyClasses(wrapper, classes); // Update the position
77    });
78  } else if (tile.mergedFrom) {
79    classes.push("tile-merged");
80    this.applyClasses(wrapper, classes);
81
82    // Render the tiles that merged
83    tile.mergedFrom.forEach(function (merged) {
84      self.addTile(merged);
85    });
86  } else {
87    classes.push("tile-new");
88    this.applyClasses(wrapper, classes);
89  }
90
91  // Add the inner part of the tile to the wrapper
92  wrapper.appendChild(inner);
93
94  // Put the tile on the board
95  this.tileContainer.appendChild(wrapper);
96};
97
98HTMLActuator.prototype.applyClasses = function (element, classes) {
99  element.setAttribute("class", classes.join(" "));
100};
101
102HTMLActuator.prototype.normalizePosition = function (position) {
103  return { x: position.x + 1, y: position.y + 1 };
104};
105
106HTMLActuator.prototype.positionClass = function (position) {
107  position = this.normalizePosition(position);
108  return "tile-position-" + position.x + "-" + position.y;
109};
110
111HTMLActuator.prototype.updateScore = function (score) {
112  this.clearContainer(this.scoreContainer);
113
114  var difference = score - this.score;
115  this.score = score;
116
117  this.scoreContainer.textContent = this.score;
118
119  if (difference > 0) {
120    var addition = document.createElement("div");
121    addition.classList.add("score-addition");
122    addition.textContent = "+" + difference;
123
124    this.scoreContainer.appendChild(addition);
125  }
126};
127
128HTMLActuator.prototype.updateBestScore = function (bestScore) {
129  this.bestContainer.textContent = bestScore;
130};
131
132HTMLActuator.prototype.message = function (won) {
133  var type    = won ? "game-won" : "game-over";
134  var message = won ? "You win!" : "Game over!";
135
136  this.messageContainer.classList.add(type);
137  this.messageContainer.getElementsByTagName("p")[0].textContent = message;
138};
139
140
141HTMLActuator.prototype.message1 = function (won) {
142  var type    = won ? "game-won" : "game-over";
143  var message = won ? "Game Paused" : "Game over!";
144
145  this.messageContainer.classList.add(type);
146  this.messageContainer.getElementsByTagName("p")[0].textContent = message;
147};
148
149
150
151
152HTMLActuator.prototype.clearMessage = function () {
153  // IE only takes one value to remove at a time.
154  this.messageContainer.classList.remove("game-won");
155  this.messageContainer.classList.remove("game-over");
156};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.