-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameOfLife3D.pde
90 lines (81 loc) · 2.26 KB
/
GameOfLife3D.pde
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
private static final float RADIAN = PI / 180.0;
private static final int ANIMATION_DELAY = 100;
private static final int GRID_WIDTH = 32;
private static final int GRID_HEIGHT = 32;
private static final int GRID_DEPTH = 32;
private static final int CELL_CHANCE_TO_LIVE = 5;
private static final int CELL_SIZE = 5;
private static final int NEIGHBOUR_MAXIMUM = 17;
private static final int NEIGHBOUR_MINIMUM = 7;
private static final int NEIGHBOUR_REPRODUCTION = 8;
private Grid mGrid;
private boolean mRunning;
private float mAngle;
private float mZoom;
private int mCentreZ;
@Override
void setup() {
size(displayWidth, displayHeight, P3D);
noStroke();
final int centreX = GRID_WIDTH / 2 * CELL_SIZE;
final int centreY = GRID_HEIGHT / 2 * CELL_SIZE;
mCentreZ = GRID_DEPTH / 2 * CELL_SIZE;
mZoom = RADIAN * 180.0;
zoomCamera();
mGrid = new GridBuilder().setWidth(GRID_WIDTH).setHeight(GRID_HEIGHT).setDepth(GRID_DEPTH)
.setCellSize(CELL_SIZE).setCentreX(centreX).setCentreY(centreY).setCentreZ(mCentreZ)
.build();
mGrid.randomise();
lights();
mGrid.draw();
}
@Override
void draw() {
if (mRunning) {
lights();
mGrid.update();
mGrid.draw();
}
try {
Thread.sleep(ANIMATION_DELAY);
} catch (InterruptedException e) {}
}
@Override
void keyPressed() {
switch (key) {
case 'p': // Resume/pause
mRunning = !mRunning;
break;
case 'c': // Clear mGrid
mGrid.clear();
mGrid.draw();
break;
case 'r': // Randomise mGrid
lights();
mGrid.randomise();
mGrid.draw();
break;
case 'j': // Rotate left
mAngle -= RADIAN;
mGrid.setAngle(mAngle);
break;
case 'k': // Rotate right
mAngle += RADIAN;
mGrid.setAngle(mAngle);
break;
case 'h': // Zoom in
mZoom -= RADIAN;
zoomCamera();
break;
case 'l': // Zoom out
mZoom += RADIAN;
zoomCamera();
break;
case 'q': // Quit
exit();
break;
}
}
private void zoomCamera() {
camera(0, 0, mCentreZ * mZoom, 0, 0, 0, 0, 1, 0);
}