-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2048.js
245 lines (215 loc) · 6.15 KB
/
2048.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
var indices = 0;
function generateIndex () {
indices += 1;
return indices;
}
/**
* COLOR UTILS
*/
function hexToRGB (hex) {
hex = hex.replace("#", "0x");
value = parseInt(hex);
return [(hex >> 16) % 256, (hex >> 8) % 256, hex % 256];
}
function rgbToHex (rgb) {
str = (rgb[0] * 256 * 256 + rgb[1] * 256 + rgb[2]).toString(16);
while (str.length < 6) {
str = "0" + str;
}
return "#" + str;
}
function interpolateColor (rgb1, rgb2, progress) {
return addVector(scaleVector(rgb1, 1 - progress), scaleVector(rgb2, progress)).map(function (x) {
return Math.min(255, Math.max(0, parseInt(x, 10)));
});
}
/**
* VECTOR UTILS
*/
function scaleVector (arr, x) {
return arr.map(function (z) { return x * z; });
}
function addVector (arr1, arr2) {
return arr1.map(function (a, i) { return a + arr2[i]; });
}
function subtractVector (arr1, arr2) {
return addVector(arr1, scaleVector(arr2, -1));
}
/**
* DISPLAY UTILS
*/
function getCoord (index) {
return index * cellSize + cellMargins;
}
function getCoordString (index) {
return getCoord(index) + "px";
}
function setCellValue (cell, value) {
cell.innerHTML = "<div style=\"display: table-cell; vertical-align: middle;\">" + value + "</div>";
}
function createNewTileHtmlElement (tile) {
var elem = document.createElement("div");
var dims = cellSize - 2 * cellMargins;
elem.style.position = "absolute";
elem.style.width = `${dims}px`;
elem.style.height = `${dims}px`;
elem.style.top = getCoordString(tile.y);
elem.style.left = getCoordString(tile.x);
elem.style.backgroundColor = colorScheme[tile.value][0];
elem.style.color = colorScheme[tile.value][1];
elem.style.opacity = "0";
elem.style.paddingTop = "0";
elem.style.paddingLeft = "0";
elem.style.display = "table";
setCellValue(elem, tile.value);
container.appendChild(elem);
tile.elem = elem;
tile.anim = makeAnimation(tile, tile.x, tile.y, tile.value);
tile.isNew = true;
return tile;
}
/**
* LOGIC UTILS
*/
function generateNewValue () {
var tileProbabilities = [[2, 0.8], [4, 0.16], [8, 0.04]];
var rng = Math.random();
var ans = -1;
tileProbabilities.forEach(function (item) {
if (ans != -1) return;
if (rng < item[1])
ans = item[0];
else
rng -= item[1];
});
if (ans == -1)
ans = 2;
return ans;
}
function generateNewPosition () {
var x = Math.floor(Math.random() * 4);
var y = Math.floor(Math.random() * 4);
var any = false;
state.forEach(function (item, index) {
if (item.anim && item.anim.endVal == 0) {
return;
}
if (item.x == x && item.y == y) {
any = true;
}
});
if (any) {
return generateNewPosition();
}
return [x, y];
}
function generateNewObject () {
let ret = new Object();
let pos = generateNewPosition();
ret.x = pos[0];
ret.y = pos[1];
ret.value = generateNewValue();
ret.index = generateIndex();
return ret;
}
function enumerateCells (dir) {
var ret = [];
if (dir == "Down") {
for (var y = 3; y >= 0; y--) for (var x = 0; x < 4; x++) ret.push([x, y]);
}
if (dir == "Up") {
for (var y = 0; y < 4; y++) for (var x = 0; x < 4; x++) ret.push([x, y]);
}
if (dir == "Right") {
for (var x = 3; x >= 0; x--) for (var y = 0; y < 4; y++) ret.push([x, y]);
}
if (dir == "Left") {
for (var x = 0; x < 4; x++) for (var y = 0; y < 4; y++) ret.push([x, y]);
}
return ret;
}
function dirStringToVector (dir) {
if (dir == "Up") return [0, -1];
if (dir == "Down") return [0, 1];
if (dir == "Left") return [-1, 0];
if (dir == "Right") return [1, 0];
}
function interpolateCurve (x) {
return (x * x) * (3.0 - 2.0 * x);
}
function animateCell (cell) {
if (cell.anim == null) return;
var status = cell.anim.status;
status += cell.anim.inc;
if (status >= 1) {
endAnimation(cell);
}
else {
var x = interpolateCurve(status);
var pos = addVector(
scaleVector(cell.anim.initPos, 1 - x),
scaleVector(cell.anim.finalPos, x));
cell.elem.style.left = pos[0] + "px";
cell.elem.style.top = pos[1] + "px";
cell.elem.style.backgroundColor = rgbToHex(interpolateColor(cell.anim.initCol, cell.anim.finalCol, x));
cell.anim.status = status;
}
}
function deleteCell (cell) {
var elem = cell.elem;
elem.parentNode.removeChild(elem);
state = state.filter(function (value) {
return value.index != cell.index;
});
}
function endAnimation (cell) {
if (cell.anim == null) return;
if (cell.isNew) {
cell.elem.style.opacity = "1";
cell.isNew = false;
}
if (cell.anim.endVal == 0) {
deleteCell(cell);
return;
}
cell.elem.style.left = cell.anim.finalPos[0] + "px";
cell.elem.style.top = cell.anim.finalPos[1] + "px";
cell.elem.style.backgroundColor = rgbToHex(cell.anim.finalCol);
cell.elem.style.color = colorScheme[cell.anim.endVal][1];
cell.elem.style.fontSize = sizeScheme[cell.anim.endVal][0];
cell.value = cell.anim.endVal;
setCellValue(cell.elem, cell.anim.endVal);
cell.anim = null;
}
function getCellAt (pos) {
for (var i = 0; i < state.length; i++) {
if (state[i].x == pos[0] && state[i].y == pos[1]) {
return state[i];
}
}
return null;
}
function inBounds (start) {
if (start[0] < 0 || start[0] >= 4) return false;
if (start[1] < 0 || start[1] >= 4) return false;
return true;
}
function nextFrom (start, dir) {
if (!inBounds(start)) return null;
var tmp = getCellAt(start);
if (tmp != null) return tmp;
return nextFrom(addVector(start, dir), dir);
}
function makeAnimation (cell, newx, newy, value) {
return {
finalX: newx,
finalY: newy,
initPos: [getCoord(cell.x), getCoord(cell.y)],
finalPos: [getCoord(newx), getCoord(newy)],
status: 0,
inc: 0.1,
initCol: hexToRGB(colorScheme[cell.value][0]),
finalCol: hexToRGB(colorScheme[value][0]),
endVal: value
};
}