-
Notifications
You must be signed in to change notification settings - Fork 1
/
sketch.js
112 lines (96 loc) · 2.17 KB
/
sketch.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
let canvas;
let container;
let grid;
let gridSize;
let gridResolution;
function setup() {
container = select(".container");
canvas = createCanvas(container.width, container.width);
canvas.parent(container);
gridSize = 50;
gridResolution = width / gridSize;
grid = Create2DArray(gridSize, gridSize);
//grid[gridSize / 2][gridSize / 2] = 3000;
background(0);
}
function draw() {
let nextGrid = Create2DArray(gridSize, gridSize);
if (mouseButton === LEFT) {
let x = Math.floor(mouseX / gridResolution);
let y = Math.floor(mouseY / gridResolution);
if (x >= 0 && x < gridSize && y >= 0 && y < gridSize)
grid[x][y] += 10;
}
//Display current grid and calc next grid
for (let x = 0; x < gridSize; x++) {
for (let y = 0; y < gridSize; y++) {
//calc
nextGrid[x][y] += grid[x][y];
if (grid[x][y] >= 4) {
nextGrid[x][y] -= 4;
if (x - 1 >= 0)
nextGrid[x - 1][y] += 1;
if (x + 1 < gridSize)
nextGrid[x + 1][y] += 1;
if (y - 1 >= 0)
nextGrid[x][y - 1] += 1;
if (y + 1 < gridSize)
nextGrid[x][y + 1] += 1;
}
}
}
for (let x = 0; x < gridSize; x++) {
for (let y = 0; y < gridSize; y++) {
//Only redraw when needed - Big Performance boost !important
if (nextGrid[x][y] != grid[x][y]) {
fill(GetGridColor(nextGrid[x][y]));
noStroke();
rect(x * gridResolution, y * gridResolution, gridResolution, gridResolution);
}
}
}
grid = nextGrid;
}
function mouseReleased() {
mouseButton = null;
}
function Create2DArray(cols, rows) {
let result = [];
for (let x = 0; x < cols; x++) {
result.push([]);
for (let y = 0; y < rows; y++) {
result[x].push(0);
}
}
return result;
}
function GetGridColor(val) {
let c;
switch (val) {
case 0:
c = color(0, 0, 0);
break;
case 1:
c = color(100, 100, 0);
break;
case 2:
c = color(150, 150, 0);
break;
case 3:
c = color(200, 200, 0);
break;
default:
c = color(255, 255, 0);
// >= 4
break;
}
return c;
}
function windowResized() {
resizeCanvas(container.width, container.width);
gridSize = 50;
gridResolution = width / gridSize;
grid = Create2DArray(gridSize, gridSize);
//grid[gridSize / 2][gridSize / 2] = 3000;
background(0);
}