-
Notifications
You must be signed in to change notification settings - Fork 1
/
game-of-life.ts
316 lines (243 loc) · 8.88 KB
/
game-of-life.ts
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* jshint esversion: 6 */
class Grid {
private grid: Uint8Array;
private width: number;
constructor(width: number, height: number, valuefn: () => number) {
this.width = width;
this.grid = new Uint8Array(width * height);
for (var i = 0; i < this.grid.length; i++) {
this.grid[i] = valuefn();
}
}
get(x: number, y: number): number {
return this.grid[(y * this.width) + x];
}
set(x: number, y: number, value: number) {
this.grid[(y * this.width) + x] = value;
}
increment(x: number, y: number, offset: number) {
this.grid[(y * this.width) + x] += offset;
}
}
class World {
private state: Grid;
private neighbourCounts: Grid;
public width: number;
public height: number;
constructor(x: number, y: number) {
this.width = x;
this.height = y;
this.state = new Grid(x, y, () => {
return (Math.random() <= 0.08) ? 1 : 0;
});
// Extra padding around our array to avoid needing a bunch of conditionals around the edges of the map
this.neighbourCounts = new Grid(this.width + 2, this.height + 2, () => { return 0; });
this.initNeighbourCounts();
}
incrementNeighbours(x: number, y: number, increment: number) {
// adjust for our padding
x += 1;
y += 1;
this.neighbourCounts.increment(x - 1, y - 1, increment);
this.neighbourCounts.increment(x + 1, y - 1, increment);
this.neighbourCounts.increment(x - 1, y + 1, increment);
this.neighbourCounts.increment(x + 1, y + 1, increment);
this.neighbourCounts.increment(x, y - 1, increment);
this.neighbourCounts.increment(x, y + 1, increment);
this.neighbourCounts.increment(x - 1, y, increment);
this.neighbourCounts.increment(x + 1, y, increment);
}
initNeighbourCounts() {
for (let x = 0; x < this.width; x++) {
for (let y = 0; y < this.height; y++) {
if (this.isAlive(x, y)) {
this.incrementNeighbours(x, y, 1);
}
}
}
}
neighbourCount(x: number, y: number): number {
return this.neighbourCounts.get(x + 1, y + 1);
}
giveLife(x: number, y: number) {
this.incrementNeighbours(x, y, 1);
this.state.set(x, y, 1);
}
takeLife(x: number, y: number) {
this.incrementNeighbours(x, y, -1);
this.state.set(x, y, 0);
}
isAlive(x: number, y: number) {
return !!this.state.get(x, y);
}
tick(changes: WorldChanges) {
changes.clear();
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
const neighbourCount = this.neighbourCount(x, y);
if (this.isAlive(x, y)) {
if (neighbourCount !== 2 && neighbourCount !== 3) {
changes.deaths.push(x, y);
}
} else {
if (neighbourCount === 3) {
changes.births.push(x, y);
}
}
}
}
changes.deaths.forEach((x, y) => { this.takeLife(x, y) });
changes.births.forEach((x, y) => { this.giveLife(x, y) });
}
}
class PointSet {
private xs: Uint16Array;
private ys: Uint16Array;
private idx: number;
constructor(size: number) {
this.xs = new Uint16Array(size);
this.ys = new Uint16Array(size);
this.idx = 0;
}
push(x: number, y: number) {
this.xs[this.idx] = x;
this.ys[this.idx] = y;
this.idx += 1;
}
clear() {
this.idx = 0;
}
forEach(fn: (x: number, y: number) => void) {
for (var i = 0; i < this.idx; i++) {
fn(this.xs[i], this.ys[i]);
}
}
}
class WorldChanges {
public births: PointSet;
public deaths: PointSet;
constructor(width: number, height: number) {
this.births = new PointSet(width * height);
this.deaths = new PointSet(width * height);
}
public clear() {
this.births.clear();
this.deaths.clear();
}
}
class Renderer {
private world: World;
private container: HTMLElement;
private pixelSize: number;
private canvas: HTMLCanvasElement;
private ctx: CanvasRenderingContext2D;
private bitmap: ImageData;
private debug: boolean = false;
private fps: number;
static readonly fillRed = 66;
static readonly fillGreen = 31;
static readonly fillBlue = 255;
static readonly fillAlpha = 255;
constructor(world: World, container: HTMLElement, pixelSize: number) {
this.world = world;
this.container = container;
this.pixelSize = pixelSize;
this.container.innerHTML = '<canvas class="game-of-life-canvas"></canvas>';
this.canvas = this.container.querySelector('.game-of-life-canvas') as HTMLCanvasElement;
this.canvas.width = this.world.width * this.pixelSize;
this.canvas.height = this.world.height * this.pixelSize;
this.ctx = this.canvas.getContext('2d') as CanvasRenderingContext2D;
this.bitmap = this.ctx.createImageData(this.canvas.width, this.canvas.height);
// Init all pixels to the right colour but full transparency
for (let y = 0; y < this.world.height; y++) {
for (let x = 0; x < this.world.width; x++) {
for (let offsetY = 0; offsetY < this.pixelSize; offsetY++) {
const bitmapY = ((y * this.pixelSize) + offsetY) * 4;
for (let offsetX = 0; offsetX < this.pixelSize; offsetX++) {
const bitmapX = ((x * this.pixelSize) + offsetX) * 4;
const baseIdx = (bitmapY * this.canvas.width) + bitmapX;
this.bitmap.data[baseIdx + 0] = Renderer.fillRed;
this.bitmap.data[baseIdx + 1] = Renderer.fillGreen;
this.bitmap.data[baseIdx + 2] = Renderer.fillBlue;
this.bitmap.data[baseIdx + 3] = 0;
}
}
}
}
this.ctx.font = '14px serif';
this.ctx.fillStyle = '#ff0000';
this.fps = 0;
}
setDebug(val: boolean) {
this.debug = val;
}
setFPS(fps: number) {
this.fps = Math.round(fps);
}
render(changes: WorldChanges) {
changes.deaths.forEach((x: number, y: number) => {
for (let offsetY = 0; offsetY < this.pixelSize; offsetY++) {
const bitmapY = ((y * this.pixelSize) + offsetY) * 4;
for (let offsetX = 0; offsetX < this.pixelSize; offsetX++) {
const bitmapX = ((x * this.pixelSize) + offsetX) * 4;
this.bitmap.data[(bitmapY * this.canvas.width) + bitmapX + 3] = 0;
}
}
});
/* Fill living pixels */
changes.births.forEach((x: number, y: number) => {
for (let offsetY = 0; offsetY < this.pixelSize; offsetY++) {
const bitmapY = ((y * this.pixelSize) + offsetY) * 4;
for (let offsetX = 0; offsetX < this.pixelSize; offsetX++) {
const bitmapX = ((x * this.pixelSize) + offsetX) * 4;
this.bitmap.data[(bitmapY * this.canvas.width) + bitmapX + 3] = Renderer.fillAlpha;
}
}
});
}
blit() {
this.ctx.putImageData(this.bitmap, 0, 0);
if (this.debug) {
this.ctx.fillText(`FPS: ${this.fps}`, 10, 20);
}
}
}
window.onload = () => {
const debug = false;
const maxFPS = debug ? 99999 : 30;
const pixelSize = 1;
const margin = pixelSize * 8;
const world = new World(Math.floor((document.body.offsetWidth - margin) / pixelSize),
Math.floor((document.body.offsetHeight - margin) / pixelSize));
const renderer = new Renderer(world,
document.getElementById("game-of-life") as HTMLElement,
pixelSize);
renderer.setDebug(debug);
let lastTick = 0;
const msPerFrame = (1000.0 / maxFPS);
let fpsStart = Date.now();
let fpsCount = 0;
let changes = new WorldChanges(world.width, world.height);
let ticker = () => {
setTimeout(ticker);
const now = Date.now();
const delta = now - lastTick;
if (delta >= msPerFrame) {
fpsCount += 1;
lastTick = now - (delta % msPerFrame);
world.tick(changes);
renderer.render(changes);
if (fpsCount === 200) {
renderer.setFPS(fpsCount / ((now - fpsStart) / 1000.0));
fpsCount = 0;
fpsStart = now;
}
}
};
let blitter = () => {
requestAnimationFrame(blitter);
renderer.blit();
};
ticker();
blitter();
};