-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameOfLifeObelisk.js
94 lines (94 loc) · 4.22 KB
/
GameOfLifeObelisk.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
/// <reference path="obelisk.d.ts"/>
var GameOfLifeObelisk = (function () {
function GameOfLifeObelisk() {
this.gridSize = 12;
this.boardWidth = 240;
this.boardHeight = 200;
this.boardState = new Uint8Array(this.boardWidth * this.boardHeight);
this.cubeSide = this.gridSize;
this.cubeHeight = this.gridSize;
this.colorSeed = 20;
this.brick = new obelisk.Brick(new obelisk.BrickDimension(this.gridSize, this.gridSize), undefined, false);
this.cubeDimension = new obelisk.CubeDimension(this.cubeSide, this.cubeSide, this.cubeHeight);
this.pixelView = new obelisk.PixelView(document.getElementById('board'), new obelisk.Point(0, -1000));
this.pixelView.context.canvas.width = window.innerWidth - 20;
this.pixelView.context.canvas.height = window.innerHeight - 20;
}
GameOfLifeObelisk.prototype.start = function () {
this.seedBoard();
this.nextFrame();
};
GameOfLifeObelisk.prototype.nextFrame = function () {
this.render();
this.updateBoard();
requestAnimationFrame(this.nextFrame.bind(this));
};
GameOfLifeObelisk.prototype.seedBoard = function () {
for (var i = 0; i < this.boardState.length; i++) {
if (Math.random() > 0.78) {
this.boardState[i] = 1;
}
}
};
GameOfLifeObelisk.prototype.generateColor = function () {
var red = Math.sin(0.05 * this.colorSeed) * 127 + 128;
var grn = Math.sin(0.10 * this.colorSeed) * 127 + 128;
var blu = Math.sin(0.15 * this.colorSeed) * 127 + 128;
return (Math.floor(red) << 16) + (Math.floor(grn) << 8) + (Math.floor(blu));
};
GameOfLifeObelisk.prototype.generateCubeWithColor = function () {
var newColor = this.generateColor();
var newCubeColor = new obelisk.CubeColor().getByHorizontalColor(newColor);
return new obelisk.Cube(this.cubeDimension, newCubeColor, false);
};
GameOfLifeObelisk.prototype.cellAliveAt = function (x, y) {
if (x >= 0 && x < this.boardWidth && y >= 0 && y < this.boardHeight) {
return this.boardState[(y * this.boardWidth) + x];
}
else {
return 0;
}
};
GameOfLifeObelisk.prototype.getCountOfAliveNeighbors = function (x, y) {
var count = this.cellAliveAt(x - 1, y - 1) + this.cellAliveAt(x, y - 1) + this.cellAliveAt(x + 1, y - 1) + this.cellAliveAt(x - 1, y) + this.cellAliveAt(x + 1, y) + this.cellAliveAt(x - 1, y + 1) + this.cellAliveAt(x, y + 1) + this.cellAliveAt(x + 1, y + 1);
return count;
};
GameOfLifeObelisk.prototype.updateBoard = function () {
var nextBoard = new Uint8Array(this.boardWidth * this.boardHeight);
for (var x = 0; x < this.boardWidth; x++) {
for (var y = 0; y < this.boardHeight; y++) {
var prevState = this.boardState[(y * this.boardWidth) + x];
var nextState = 0;
var aliveNeighbors = this.getCountOfAliveNeighbors(x, y);
if (prevState === 1 && (aliveNeighbors === 2 || aliveNeighbors === 3)) {
nextState = 1;
}
else if (prevState === 0 && aliveNeighbors === 3) {
nextState = 1;
}
nextBoard[(y * this.boardWidth) + x] = nextState;
}
}
this.boardState = nextBoard;
};
GameOfLifeObelisk.prototype.render = function () {
this.colorSeed++;
var cube = this.generateCubeWithColor();
this.pixelView.clear();
for (var x = 0; x < this.boardWidth; x++) {
for (var y = 0; y < this.boardHeight; y++) {
var point3d = new obelisk.Point3D(x * this.gridSize, y * this.gridSize, 0);
if (this.boardState[(y * this.boardWidth) + x] === 1) {
this.pixelView.renderObject(cube, new obelisk.Point3D(x * this.gridSize, y * this.gridSize, 0));
}
else {
this.pixelView.renderObject(this.brick, point3d);
}
}
}
};
return GameOfLifeObelisk;
})();
var game = new GameOfLifeObelisk();
game.start();
//# sourceMappingURL=GameOfLifeObelisk.js.map