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