-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
453 lines (402 loc) · 13.4 KB
/
Main.java
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package conway3d;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.ToolBar;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.util.Random;
/**
* @author Connor Denman
* <p>
* This is the entry point class for Conway's Game of Life in 3D.
*/
public class Main extends Application
{
private int w, h, depth;
private static final int GRID_SIZE = 30;
private static final float RANDOM_DENSITY = 0.05f;
private static final int BOX_SIZE = 1;
private int[][][][] world;
private Cell[][][] cells;
private static final int[] INIT_RULES = {4, 10, 10, 4};
private static final String[] PRESET_NAMES = {
"Random", "Two Walls", "Weird Corners", "Halfsies", "Triangular", "Single Beam"
};
private Slider[] rSliders;
private Group root;
private SubScene subScene;
final Xform cubeGroup = new Xform();
final Xform worldForm = new Xform();
final PerspectiveCamera camera = new PerspectiveCamera(true);
final Xform cameraXform = new Xform();
final Xform cameraXform2 = new Xform();
final Xform cameraXform3 = new Xform();
private static final double CAMERA_INITIAL_DISTANCE = -450;
private static final double CAMERA_INITIAL_X_ANGLE = 70.0;
private static final double CAMERA_INITIAL_Y_ANGLE = 320.0;
private static final double CAMERA_NEAR_CLIP = 0.1;
private static final double CAMERA_FAR_CLIP = 10000.0;
private static final double CONTROL_MULTIPLIER = 0.1;
private static final double SHIFT_MULTIPLIER = 10.0;
private static final double MOUSE_SPEED = 0.1;
private static final double ROTATION_SPEED = 2.0;
private static final double TRACK_SPEED = 0.3;
double mousePosX;
double mousePosY;
double mouseOldX;
double mouseOldY;
double mouseDeltaX;
double mouseDeltaY;
/**
* Prepare the JavaFX camera.
*
* @return Nothing
*/
private void buildCamera()
{
root.getChildren().add(cameraXform);
cameraXform.getChildren().add(cameraXform2);
cameraXform2.getChildren().add(cameraXform3);
cameraXform3.getChildren().add(camera);
cameraXform3.setRotateZ(180.0);
camera.setNearClip(CAMERA_NEAR_CLIP);
camera.setFarClip(CAMERA_FAR_CLIP);
camera.setTranslateZ(CAMERA_INITIAL_DISTANCE);
cameraXform.ry.setAngle(CAMERA_INITIAL_Y_ANGLE);
cameraXform.rx.setAngle(CAMERA_INITIAL_X_ANGLE);
}
/**
* Handle for when the user uses the mouse to rotate the molecule being displayed.
*
* @param scene - the scene where the action applies.
* @param root - root node.
*/
private void handleMouse(Scene scene, final Node root)
{
scene.setOnMousePressed(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me)
{
mousePosX = me.getSceneX();
mousePosY = me.getSceneY();
mouseOldX = me.getSceneX();
mouseOldY = me.getSceneY();
}
});
scene.setOnMouseDragged(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me)
{
mouseOldX = mousePosX;
mouseOldY = mousePosY;
mousePosX = me.getSceneX();
mousePosY = me.getSceneY();
mouseDeltaX = (mousePosX - mouseOldX);
mouseDeltaY = (mousePosY - mouseOldY);
double modifier = 1.0;
if (me.isControlDown())
{
modifier = CONTROL_MULTIPLIER;
}
if (me.isShiftDown())
{
modifier = SHIFT_MULTIPLIER;
}
if (me.isPrimaryButtonDown())
{
cameraXform.ry.setAngle(cameraXform.ry.getAngle() - mouseDeltaX * MOUSE_SPEED * modifier * ROTATION_SPEED);
cameraXform.rx.setAngle(cameraXform.rx.getAngle() + mouseDeltaY * MOUSE_SPEED * modifier * ROTATION_SPEED);
} else if (me.isSecondaryButtonDown())
{
double z = camera.getTranslateZ();
double newZ = z + mouseDeltaX * MOUSE_SPEED * modifier;
camera.setTranslateZ(newZ);
} else if (me.isMiddleButtonDown())
{
cameraXform2.t.setX(cameraXform2.t.getX() + mouseDeltaX * MOUSE_SPEED * modifier * TRACK_SPEED);
cameraXform2.t.setY(cameraXform2.t.getY() + mouseDeltaY * MOUSE_SPEED * modifier * TRACK_SPEED);
}
}
});
}
/**
* Prepare the game of life 3D grid and data structure.
*
* @param gridSize - e.g. "30" for a 30x30x30 grid.
* @param preset - the integer representation of the desired grid preset.
*/
private void setupGame(int preset)
{
cubeGroup.getChildren().clear();
w = GRID_SIZE;
h = GRID_SIZE;
depth = GRID_SIZE;
world = new int[w][h][depth][2];
cells = new Cell[w][h][depth];
Random rand = new Random();
// different grid presets
switch (preset)
{
case 0:
// random preset
for (int i = 0; i < RANDOM_DENSITY * w * h * depth; i += BOX_SIZE)
{
world[rand.nextInt(w)][rand.nextInt(h)][rand.nextInt(depth)][1] = 1;
}
break;
case 1:
// 2 walls of cells on opposite sides of the cube
for (int j = 0; j < w; j += BOX_SIZE)
{
for (int k = 0; k < h; k += BOX_SIZE)
{
world[0][j][k][1] = 1;
world[depth - 1][j][k][1] = 1;
}
}
break;
case 2:
// weird corners
for (int i = 0; i < rand.nextInt(w); i += BOX_SIZE)
{
for (int j = 0; j < rand.nextInt(h); j += BOX_SIZE)
{
for (int k = 0; k < rand.nextInt(depth); k += BOX_SIZE)
{
world[i][j][k][1] = 1;
}
}
}
break;
case 3:
// half of all axes
for (int i = 0; i < w/2; i += BOX_SIZE)
{
for (int j = 0; j < h/2; j += BOX_SIZE)
{
for (int k = 0; k < depth/2; k += BOX_SIZE)
{
world[i][j][k][1] = 1;
}
}
}
break;
case 4:
for (int i = 0; i < w; i += BOX_SIZE)
{
for (int j = 0; j < i; j += BOX_SIZE)
{
for (int k = 0; k < j; k += BOX_SIZE)
{
world[i][j][k][1] = 1;
}
}
}
break;
case 5:
for (int i = 0; i < w; i += BOX_SIZE)
{
world[i][i][i][1] = 1;
}
break;
}
}
@Override
/**
* Overridden start method, takes a Stage element
*
* Sets up the JavaFX scene for drawing and calls functions
* for building the camera, axes, and molecule.
*
* @param primaryStage - the main stage.
*/
public void start(Stage primaryStage)
{
root = new Group();
buildCamera();
subScene = new SubScene(root, 1024, 768, false, SceneAntialiasing.DISABLED);
subScene.setFill(Color.LIGHTGREY);
subScene.setCamera(camera);
// 2D
BorderPane pane = new BorderPane();
pane.setCenter(subScene);
// UI controls
ToolBar toolBar = new ToolBar(new Label("Presets"));
rSliders = new Slider[4];
// setup preset buttons
for (int i = 0; i < PRESET_NAMES.length; i++)
{
Button currentButton = new Button(PRESET_NAMES[i]);
final int currentIndex = i;
currentButton.setOnAction((event) ->
{
setupGame(currentIndex);
});
toolBar.getItems().add(currentButton);
}
toolBar.getItems().add(new Label("Rules"));
// setup rule input sliders
for (int i = 0; i < 4; i++)
{
rSliders[i] = new Slider();
rSliders[i].setMin(0);
rSliders[i].setMax(26);
rSliders[i].setValue(INIT_RULES[i]);
rSliders[i].setShowTickLabels(true);
rSliders[i].setShowTickMarks(true);
toolBar.getItems().addAll(new Label("r" + Integer.toString(i + 1)), rSliders[i]);
}
toolBar.setOrientation(Orientation.VERTICAL);
pane.setRight(toolBar);
pane.setPrefSize(300, 300);
Scene scene = new Scene(pane);
worldForm.getChildren().add(cubeGroup);
root.getChildren().add(worldForm);
handleMouse(scene, worldForm);
scene.setOnScroll(new EventHandler<ScrollEvent>()
{
@Override
public void handle(ScrollEvent event)
{
double zoomFactor = 1.05;
double deltaY = event.getDeltaY();
if (deltaY < 0)
{
zoomFactor = 2.0 - zoomFactor;
}
System.out.println(zoomFactor);
worldForm.setScaleX(worldForm.getScaleX() * zoomFactor);
worldForm.setScaleY(worldForm.getScaleY() * zoomFactor);
worldForm.setScaleZ(worldForm.getScaleZ() * zoomFactor);
event.consume();
}
});
primaryStage.setScene(scene);
primaryStage.setTitle("Conway's GOL 3D - Connor Denman");
primaryStage.show();
setupGame(0);
new GameLoop().start();
}
/**
* @author Connor Denman
* <p>
* GameLoop handles the primary game animation frame timing.
*/
class GameLoop extends AnimationTimer
{
private long lastUpdate = 0;
@Override
public void handle(long now)
{
// wait 1 second to update the cells
if (now - lastUpdate < 1_000_000_000)
{
cubeGroup.setRotate(cubeGroup.getRotate() + 0.5f);
return;
}
lastUpdate = now;
for (int i = 0; i < w; i += BOX_SIZE)
{
for (int j = 0; j < h; j += BOX_SIZE)
{
for (int k = 0; k < depth; k += BOX_SIZE)
{
if ((world[i][j][k][1] == 1))
{
world[i][j][k][0] = 1;
Cell newBox = new Cell();
newBox.setTranslateX(i);
newBox.setTranslateY(j);
newBox.setTranslateZ(k);
cubeGroup.getChildren().add(newBox);
newBox.animateSpawn();
cells[i][j][k] = newBox;
}
if (world[i][j][k][1] == -1)
{
world[i][j][k][0] = 0;
cells[i][j][k].animateDeath();
}
world[i][j][k][1] = 0;
}
}
}
for (int i = 0; i < w; i += BOX_SIZE)
{
for (int j = 0; j < h; j += BOX_SIZE)
{
for (int k = 0; k < depth; k += BOX_SIZE)
{
int count = neighbors(i, j, k);
if ((count >= (int) rSliders[0].getValue()) && (count <= (int) rSliders[1].getValue()) && world[i][j][k][0] == 0)
{
world[i][j][k][1] = 1;
}
if ((count > (int) rSliders[2].getValue() || count < (int) rSliders[3].getValue()) && world[i][j][k][0] == 1)
{
world[i][j][k][1] = -1;
}
}
}
}
}
/**
* Count all neighbors of cell specified at x, y, z.
*
* @param x - x value of currently selected cell.
* @param y - y value of currently selected cell.
* @param z - z value of currently selected cell.
* @return Total number of neighbors of selected cell.
*/
private int neighbors(int x, int y, int z)
{
return world[(x + BOX_SIZE) % w][y][z][0] +
world[x][(y + BOX_SIZE) % h][z][0] +
world[(x + w - BOX_SIZE) % w][y][z][0] +
world[x][(y + h - BOX_SIZE) % h][z][0] +
world[(x + BOX_SIZE) % w][(y + BOX_SIZE) % h][z][0] +
world[(x + w - BOX_SIZE) % w][(y + BOX_SIZE) % h][z][0] +
world[(x + BOX_SIZE) % w][(y + h - BOX_SIZE) % h][z][0] +
world[(x + w - BOX_SIZE) % w][(y + h - BOX_SIZE) % h][z][0] +
world[(x + BOX_SIZE) % w][y][(z + BOX_SIZE) % depth][0] +
world[x][(y + BOX_SIZE) % h][(z + BOX_SIZE) % depth][0] +
world[(x + w - BOX_SIZE) % w][y][(z + BOX_SIZE) % depth][0] +
world[x][(y + h - BOX_SIZE) % h][(z + BOX_SIZE) % depth][0] +
world[(x + BOX_SIZE) % w][(y + BOX_SIZE) % h][(z + BOX_SIZE) % depth][0] +
world[(x + w - BOX_SIZE) % w][(y + BOX_SIZE) % h][(z + BOX_SIZE) % depth][0] +
world[(x + BOX_SIZE) % w][(y + h - BOX_SIZE) % h][(z + BOX_SIZE) % depth][0] +
world[(x + w - BOX_SIZE) % w][(y + h - BOX_SIZE) % h][(z + BOX_SIZE) % depth][0] +
world[(x + BOX_SIZE) % w][y][(z + depth - BOX_SIZE) % depth][0] +
world[x][(y + BOX_SIZE) % h][(z + depth - BOX_SIZE) % depth][0] +
world[(x + w - BOX_SIZE) % w][y][(z + depth - BOX_SIZE) % depth][0] +
world[x][(y + h - BOX_SIZE) % h][(z + depth - BOX_SIZE) % depth][0] +
world[(x + BOX_SIZE) % w][(y + BOX_SIZE) % h][(z + depth - BOX_SIZE) % depth][0] +
world[(x + w - BOX_SIZE) % w][(y + BOX_SIZE) % h][(z + depth - BOX_SIZE) % depth][0] +
world[(x + BOX_SIZE) % w][(y + h - BOX_SIZE) % h][(z + depth - BOX_SIZE) % depth][0] +
world[(x + w - BOX_SIZE) % w][(y + h - BOX_SIZE) % h][(z + depth - BOX_SIZE) % depth][0];
}
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}