-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
179 lines (160 loc) · 4.24 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
document.oncontextmenu = function (e) {
var evt = new Object({ keyCode: 93 });
stopEvent(e);
}
function stopEvent(event) {
if (event.preventDefault != undefined)
event.preventDefault();
if (event.stopPropagation != undefined)
event.stopPropagation();
}
const cellSize = 20;
var numSizeX = 20;
var numSizeY = 20;
const deltaT = 25;
var CellType = { "Empty": 1, "Head": 2, "Tail": 3, "Wire": 4 }
Object.freeze(CellType);
var cells = [];
var time = 0;
var paused = false;
function setup() {
var canvas = createCanvas(500, 500);
numSizeX = Math.floor(width / cellSize);
numSizeY = Math.floor(height / cellSize);
cells = initialCells();
time = millis();
}
function initialCells() {
var tmp = emptyCells();
return tmp;
}
function draw() {
background(0);
var newTime = millis();
if (!paused && newTime - time >= deltaT) {
time = newTime;
cells = getNewCells(cells);
}
drawCells(cells);
if (paused) {
textSize(32);
fill(255);
text("Paused", 10, 32);
}
}
function getHeadNeighbors(i, j, currentCells) {
var numHead = 0;
for (var di = 0; di < 3; ++di) {
var ni = i + di - 1;
if (ni >= 0 && ni < numSizeX) {
for (var dj = 0; dj < 3; ++dj) {
var nj = j + dj - 1;
if (nj >= 0 && nj < numSizeY) {
if (currentCells[ni][nj] == CellType.Head) {
++numHead;
}
}
}
}
}
return numHead;
}
function getNewCells(currentCells) {
var out = emptyCells();
for (var i = 0; i < numSizeX; ++i) {
for (var j = 0; j < numSizeY; ++j) {
var current = currentCells[i][j];
switch (current) {
case CellType.Empty:
out[i][j] = CellType.Empty;
break;
case CellType.Head:
out[i][j] = CellType.Tail;
break;
case CellType.Tail:
out[i][j] = CellType.Wire;
break;
case CellType.Wire:
var numHead = getHeadNeighbors(i, j, currentCells);
if (numHead == 1 || numHead == 2) {
out[i][j] = CellType.Head;
}
else {
out[i][j] = CellType.Wire;
}
break;
default:
out[i][j] = CellType.Empty;
break;
}
}
}
return out;
}
function mousePressed() {
if (mouseX < 0 || mouseX >= width)
return;
if (mouseY < 0 || mouseY >= height)
return;
var x = Math.floor(mouseX / cellSize);
var y = Math.floor(mouseY / cellSize);
try {
var current = cells[x][y];
} catch{
return;
}
if (mouseButton === LEFT) {
if (current == CellType.Wire)
cells[x][y] = CellType.Empty;
else
cells[x][y] = CellType.Wire;
}
else if (mouseButton === RIGHT) {
if (current == CellType.Head)
cells[x][y] = CellType.Empty;
else
cells[x][y] = CellType.Head;
}
}
function keyPressed() {
if (key === ' ')
paused = !paused;
}
function getCellColor(type) {
switch (type) {
case CellType.Empty:
return color(0);
case CellType.Head:
return color(0, 0, 255);
case CellType.Tail:
return color(255, 0, 0);
case CellType.Wire:
return color(255, 255, 0);
default:
return color(0);
}
}
function drawCell(x, y, type) {
var c = getCellColor(type);
fill(c);
stroke(c);
rect(x * cellSize, y * cellSize, cellSize, cellSize);
}
function drawCells(currentCells) {
for (var i = 0; i < numSizeX; ++i) {
for (var j = 0; j < numSizeY; ++j) {
var current = currentCells[i][j];
drawCell(i, j, current);
}
}
}
function emptyCells() {
var tmp = [];
for (var i = 0; i < numSizeX; ++i) {
tmp[i] = [];
for (var j = 0; j < numSizeY; ++j) {
tmp[i][j] = CellType.Empty;
}
}
return tmp;
}