Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AroriaNetwork
GitHub Repository: AroriaNetwork/3kho-backup
Path: blob/main/projects/flappy-2048/js/html_actuator.js
1834 views
1
function HTMLActuator() {
2
this.gridContainer = document.querySelector(".grid-container");
3
// this.tileContainer = document.querySelector(".tile-container");
4
this.scoreContainer = document.querySelector(".score-container");
5
this.bestContainer = document.querySelector(".best-container");
6
this.messageContainer = document.querySelector(".game-message");
7
this.birdobj = document.querySelector(".tile-bird");
8
this.birdinn = document.querySelector(".tile-bird .tile-inner");
9
this.blockobja = document.querySelector(".tile-block-a");
10
this.blockobjb = document.querySelector(".tile-block-b");
11
this.blockobjc = document.querySelector(".tile-block-c");
12
this.blockobjd = document.querySelector(".tile-block-d");
13
this.blockinna = document.querySelector(".tile-block-a .tile-inner");
14
this.blockinnb = document.querySelector(".tile-block-b .tile-inner");
15
this.blockinnc = document.querySelector(".tile-block-c .tile-inner");
16
this.blockinnd = document.querySelector(".tile-block-d .tile-inner");
17
}
18
19
HTMLActuator.prototype.actuate = function (grid, metadata) {
20
var self = this;
21
22
var classes = ["tile", "tile-bird"];
23
24
var s = Math.floor(metadata.score);
25
26
if (s > 2048) classes.push("tile-super")
27
else if (s > 1024) classes.push("tile-2048")
28
else if (s > 512) classes.push("tile-1024")
29
else if (s > 256) classes.push("tile-512")
30
else if (s > 128) classes.push("tile-256")
31
else if (s > 64) classes.push("tile-128")
32
else if (s > 32) classes.push("tile-64")
33
else if (s > 16) classes.push("tile-32")
34
else if (s > 8) classes.push("tile-16")
35
else if (s > 4) classes.push("tile-8")
36
else if (s > 2) classes.push("tile-4")
37
else classes.push("tile-2");
38
39
this.applyClasses(this.birdobj, classes);
40
41
var zonesize = this.gridContainer.clientHeight;
42
var morepos = 0.75 * (metadata.score - s);
43
44
this.birdobj.style.top = metadata.birdpos * zonesize + "px";
45
46
this.blockobja.style.top = [0.5 , 0 , 0 ][metadata.ab] * zonesize + "px";
47
this.blockobjb.style.top = [0.75, 0.75, 0.25][metadata.ab] * zonesize + "px";
48
this.blockobjc.style.top = [0.5 , 0 , 0 ][metadata.cd] * zonesize + "px";
49
this.blockobjd.style.top = [0.75, 0.75, 0.25][metadata.cd] * zonesize + "px";
50
51
this.blockobja.style.left = (0.5 - morepos) * zonesize + "px";
52
this.blockobjb.style.left = (0.5 - morepos) * zonesize + "px";
53
this.blockobjc.style.left = (1.25 - morepos) * zonesize + "px";
54
this.blockobjd.style.left = (1.25 - morepos) * zonesize + "px";
55
56
this.birdinn.textContent = s;
57
58
window.requestAnimationFrame(function () {
59
self.updateScore(s);
60
self.updateBestScore(Math.floor(metadata.bestScore));
61
});
62
};
63
64
// Continues the game (both restart and keep playing)
65
HTMLActuator.prototype.continue = function () {
66
this.clearMessage();
67
};
68
69
HTMLActuator.prototype.clearContainer = function (container) {
70
while (container.firstChild) {
71
container.removeChild(container.firstChild);
72
}
73
};
74
75
HTMLActuator.prototype.addTile = function (tile) {
76
var self = this;
77
78
var wrapper = document.createElement("div");
79
var inner = document.createElement("div");
80
var position = tile.previousPosition || { x: tile.x, y: tile.y };
81
var positionClass = this.positionClass(position);
82
83
// We can't use classlist because it somehow glitches when replacing classes
84
var classes = ["tile", "tile-" + tile.value, positionClass];
85
86
if (tile.value > 2048) classes.push("tile-super");
87
88
this.applyClasses(wrapper, classes);
89
90
inner.classList.add("tile-inner");
91
inner.textContent = tile.value;
92
93
if (tile.previousPosition) {
94
// Make sure that the tile gets rendered in the previous position first
95
window.requestAnimationFrame(function () {
96
classes[2] = self.positionClass({ x: tile.x, y: tile.y });
97
self.applyClasses(wrapper, classes); // Update the position
98
});
99
} else if (tile.mergedFrom) {
100
classes.push("tile-merged");
101
this.applyClasses(wrapper, classes);
102
103
// Render the tiles that merged
104
tile.mergedFrom.forEach(function (merged) {
105
self.addTile(merged);
106
});
107
} else {
108
classes.push("tile-new");
109
this.applyClasses(wrapper, classes);
110
}
111
112
// Add the inner part of the tile to the wrapper
113
wrapper.appendChild(inner);
114
115
// Put the tile on the board
116
// this.tileContainer.appendChild(wrapper);
117
};
118
119
HTMLActuator.prototype.applyClasses = function (element, classes) {
120
element.setAttribute("class", classes.join(" "));
121
};
122
123
HTMLActuator.prototype.normalizePosition = function (position) {
124
return { x: position.x + 1, y: position.y + 1 };
125
};
126
127
HTMLActuator.prototype.positionClass = function (position) {
128
position = this.normalizePosition(position);
129
return "tile-position-" + position.x + "-" + position.y;
130
};
131
132
HTMLActuator.prototype.updateScore = function (score) {
133
//this.clearContainer(this.scoreContainer);
134
135
var difference = score - this.score;
136
this.score = score;
137
138
if (difference > 0) {
139
this.scoreContainer.textContent = this.score;
140
141
var addition = document.createElement("div");
142
addition.classList.add("score-addition");
143
addition.textContent = "+" + difference;
144
145
this.scoreContainer.appendChild(addition);
146
}
147
};
148
149
HTMLActuator.prototype.updateBestScore = function (bestScore) {
150
this.bestContainer.textContent = bestScore;
151
};
152
153
HTMLActuator.prototype.message = function (won) {
154
var type = won ? "game-won" : "game-over";
155
var message = won ? "You win!" : "Game over!";
156
157
this.messageContainer.classList.add(type);
158
this.messageContainer.getElementsByTagName("p")[0].textContent = message;
159
};
160
161
HTMLActuator.prototype.clearMessage = function () {
162
// IE only takes one value to remove at a time.
163
this.messageContainer.classList.remove("game-won");
164
this.messageContainer.classList.remove("game-over");
165
};
166
167