-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
291 lines (245 loc) · 7.26 KB
/
main.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"use strict";
if (!window.Worker) {
throw new Error("WorkerAPI is not supported");
}
const STAGE_WIDTH = 800;
const STAGE_HEIGHT = 800;
const GRID_SIZE_ROW = 80;
const GRID_SIZE_COL = 80;
const TILE_SIZE_WIDTH = STAGE_WIDTH / GRID_SIZE_ROW;
const TILE_SIZE_HEIGHT = STAGE_HEIGHT / GRID_SIZE_COL;
const TILES = [];
const STATE = [];
const PADDING = 2;
const GAME_STATE = {
isRunning: true,
changedTiles: [
[5, 1, 1],
[5, 2, 1],
[6, 1, 1],
[6, 2, 1],
[5, 11, 1],
[6, 11, 1],
[7, 11, 1],
[4, 12, 1],
[8, 12, 1],
[3, 13, 1],
[9, 13, 1],
[3, 14, 1],
[9, 14, 1],
[6, 15, 1],
[4, 16, 1],
[8, 16, 1],
[5, 17, 1],
[6, 17, 1],
[7, 17, 1],
[6, 18, 1],
[3, 21, 1],
[4, 21, 1],
[5, 21, 1],
[3, 22, 1],
[4, 22, 1],
[5, 22, 1],
[2, 23, 1],
[6, 23, 1],
[1, 25, 1],
[2, 25, 1],
[6, 25, 1],
[7, 25, 1],
[3, 35, 1],
[4, 35, 1],
[3, 36, 1],
[4, 36, 1],
]
}
setup(GAME_STATE);
// Animation loop
let start = null;
function step(timestamp) {
if (!start) start = timestamp;
let progress = timestamp - start;
if (progress > 10) {
if(GAME_STATE.isRunning) {
GAME_STATE.changedTiles = stepSimulation(GAME_STATE.changedTiles);
}
start = timestamp;
}
window.requestAnimationFrame(step);
}
window.requestAnimationFrame(step);
function setup() {
const stage = document.getElementById("stage");
stage.innerHTML = "";
const grid = createGrid();
update(GAME_STATE.changedTiles);
stage.appendChild(grid);
const toggleButton = document.getElementById("toggle");
toggleButton.addEventListener("mousedown", simulationControlHandler);
}
function toggleSimulationState() {
GAME_STATE.isRunning = !GAME_STATE.isRunning;
}
function simulationControlHandler(event) {
toggleSimulationState();
const toggleButton = document.getElementById("toggle");
if(GAME_STATE.isRunning) {
toggleButton.innerText = "STOP";
} else {
toggleButton.innerText = "PLAY";
}
}
function stepSimulation(oldDiff) {
let newDiff = calculateDiff(oldDiff, STATE)
update(newDiff);
return newDiff;
}
function update(diff) {
switchTiles(diff);
updateState(diff);
}
function createGrid() {
let grid = document.createElementNS("http://www.w3.org/2000/svg", "svg");
grid.setAttribute("width", STAGE_WIDTH);
grid.setAttribute("height", STAGE_HEIGHT);
// State
for (let i = 0; i < GRID_SIZE_ROW + PADDING * 2; ++i) {
let stateRow = [];
for (let j = 0; j < GRID_SIZE_COL + PADDING * 2; ++j) {
stateRow.push(0);
}
STATE.push(stateRow);
}
// Tiles
for (let i = 0; i < GRID_SIZE_ROW; ++i) {
let tileRow = [];
for (let j = 0; j < GRID_SIZE_COL; ++j) {
let tileY = i * TILE_SIZE_HEIGHT;
let tileX = j * TILE_SIZE_WIDTH;
let tile = document.createElementNS("http://www.w3.org/2000/svg", "rect");
tile.setAttribute("x", tileX);
tile.setAttribute("y", tileY);
tile.setAttribute("width", TILE_SIZE_WIDTH);
tile.setAttribute("height", TILE_SIZE_HEIGHT);
tile.setAttribute("fill", "white");
tile.addEventListener("mousedown", decorateTileClickEvent(i, j));
grid.appendChild(tile);
tileRow.push(tile);
}
TILES.push(tileRow);
}
// Horizontal grid lines
for (let i = 0; i < GRID_SIZE_ROW; ++i) {
let gridLine = document.createElementNS("http://www.w3.org/2000/svg", "line");
gridLine.setAttribute("x1", 0);
gridLine.setAttribute("y1", i * TILE_SIZE_HEIGHT);
gridLine.setAttribute("x2", STAGE_WIDTH);
gridLine.setAttribute("y2", i * TILE_SIZE_HEIGHT);
gridLine.setAttribute("stroke", "#eee");
grid.appendChild(gridLine);
}
// Vertical grid lines
for (let i = 0; i < GRID_SIZE_COL; ++i) {
let gridLine = document.createElementNS("http://www.w3.org/2000/svg", "line");
gridLine.setAttribute("x1", i * TILE_SIZE_WIDTH);
gridLine.setAttribute("y1", 0);
gridLine.setAttribute("x2", i * TILE_SIZE_WIDTH);
gridLine.setAttribute("y2", STAGE_HEIGHT);
gridLine.setAttribute("stroke", "#eee");
grid.appendChild(gridLine);
}
return grid;
}
function decorateTileClickEvent(row, col) {
function tileClick(event) {
if(GAME_STATE.isRunning) return;
if(STATE[row + PADDING][col + PADDING] === 1) {
STATE[row + PADDING][col + PADDING] = 0;
TILES[row][col].setAttribute("fill", "white");
GAME_STATE.changedTiles.push([row, col, 0]);
} else {
STATE[row + PADDING][col + PADDING] = 1;
TILES[row][col].setAttribute("fill", "black");
GAME_STATE.changedTiles.push([row, col, 0]);
}
}
return tileClick;
}
function switchTiles(diff) {
for(let i = 0; i < diff.length; ++i) {
let row = diff[i][0];
let col = diff[i][1];
if (row >= 0 && col > 0 && row < TILES.length && col < TILES[0].length){
if(diff[i][2] === 1) {
TILES[row][col].setAttribute("fill", "black");
} else {
TILES[row][col].setAttribute("fill", "white");
}
}
}
}
function updateState(diff) {
for(let i = 0; i < diff.length; ++i) {
let row = diff[i][0] + PADDING;
let col = diff[i][1] + PADDING;
if (row >= 0 && col > 0 && row < STATE.length && col < STATE[0].length){
if(diff[i][2] === 1) {
STATE[row][col] = 1;
} else {
STATE[row][col] = 0;
}
}
}
}
function calculateDiff(paintedTiles, state) {
let changes = {};
for(let i = 0; i < paintedTiles.length; ++i) {
let row = paintedTiles[i][0] + PADDING;
let col = paintedTiles[i][1] + PADDING;
// top left
applyRules(row - 1, col - 1, state, changes);
// top center
applyRules(row - 1, col, state, changes);
// top right
applyRules(row - 1, col + 1, state, changes);
// mid left
applyRules(row, col - 1, state, changes);
// mid center
applyRules(row, col, state, changes);
// mid right
applyRules(row, col + 1, state, changes);
// bottom left
applyRules(row + 1, col - 1, state, changes);
// bottom center
applyRules(row + 1, col, state, changes);
// bottom right
applyRules(row + 1, col + 1, state, changes);
}
return Object.values(changes);
}
function applyRules(i, j, state, changes) {
if (i < 0 || j < 0 || i >= state.length || j >= state[0].length) return;
let numPainted = 0;
// top left
if (i - 1 >= 0 && j - 1 >= 0 && state[i - 1][j - 1] == 1) numPainted++;
// top center
if (i - 1 >= 0 && state[i - 1][j] == 1) numPainted++;
// top right
if (i - 1 >= 0 && j + 1 < state[0].length && state[i - 1][j + 1] == 1) numPainted++;
// mid left
if (j - 1 >= 0 && state[i][j - 1] == 1) numPainted++;
// mid right
if (j + 1 < state[0].length && state[i][j + 1] == 1) numPainted++;
// bottom left
if (i + 1 < state.length && j - 1 >= 0 && state[i + 1][j - 1] == 1) numPainted++;
// bottom center
if (i + 1 < state.length && state[i + 1][j] == 1) numPainted++;
// bottom right
if (i + 1 < state.length && j + 1 < state[0].length && state[i + 1][j + 1] == 1) numPainted++;
if (state[i][j] == 1) {
if (numPainted < 2) changes[[i - PADDING, j - PADDING]] = [i - PADDING, j - PADDING, 0]
else if (numPainted > 3) changes[[i - PADDING, j - PADDING]]= [i - PADDING, j - PADDING, 0];
}
else {
if (numPainted === 3) changes[[i - PADDING, j - PADDING]] = [i - PADDING, j - PADDING, 1];
}
}