Refactoring.
[sixth-3d-demos.git] / src / main / java / eu / svjatoslav / sixth / e3d / examples / life / Main.java
1 package eu.svjatoslav.sixth.e3d.examples.life;
2
3 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
4 import eu.svjatoslav.sixth.e3d.geometry.Rectangle;
5 import eu.svjatoslav.sixth.e3d.gui.Avatar;
6 import eu.svjatoslav.sixth.e3d.gui.ViewFrame;
7 import eu.svjatoslav.sixth.e3d.gui.ViewPanel;
8 import eu.svjatoslav.sixth.e3d.gui.humaninput.WorldNavigationTracker;
9 import eu.svjatoslav.sixth.e3d.math.Transform;
10 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
11 import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection;
12 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.LineAppearance;
13 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.wireframe.Grid2D;
14
15 import java.awt.event.KeyEvent;
16
17
18 public class Main extends WorldNavigationTracker {
19
20     private static final Matrix MATRIX = new Matrix(
21             new Point3D() // position matrix in the center of the scene
22     );
23
24     public static void main(final String[] args) {
25         new Main().run();
26     }
27
28     /**
29      * Handle keyboard input.
30      */
31     @Override
32     public boolean keyPressed(final KeyEvent event, final ViewPanel viewPanel) {
33         switch (event.getKeyChar()) {
34             case ' ': // space key
35                 MATRIX.evolve(false);
36                 break;
37             case 10: // ENTER
38                 MATRIX.evolve(true);
39                 break;
40             case 'c': // reset matrix
41                 MATRIX.clear();
42                 break;
43             default:
44                 return super.keyPressed(event, viewPanel);
45         }
46         return true;
47     }
48
49     private void run() {
50
51         // create application frame visible to the user
52         final ViewFrame viewFrame = new ViewFrame();
53
54         final ShapeCollection shapeCollection = viewFrame.getViewPanel()
55                 .getRootShapeCollection();
56
57         // add matrix
58         shapeCollection.addShape(MATRIX);
59
60         // add wire-frame grid (optional)
61         shapeCollection.addShape(createGrid());
62
63         final ViewPanel viewPanel = viewFrame.getViewPanel();
64
65         setAvatarOrientation(viewPanel.getAvatar());
66
67         // enable receiving of keyboard events
68         viewPanel.getKeyboardFocusTracker().setFocusOwner(this);
69
70         // Done! World is built. So ensure screen is updated too.
71         viewPanel.repaintDuringNextViewUpdate();
72     }
73
74     /**
75      * Create pink wire-frame grid below (for decorative purposes).
76      */
77     private Grid2D createGrid() {
78         return new Grid2D(
79                 new Transform(
80                         new Point3D( // Grid positioning:
81                                 0, // center
82                                 100, // below the main scene
83                                 0), // center
84
85                         // Grid orientation:
86                         0, // no rotation along XZ axis
87                         Math.PI / 2), // face down
88
89                 new Rectangle(800), // large enough, square grid
90
91                 5, 5, // grid will be divided to 5x5 segments
92
93                 new LineAppearance(3, // line thickness
94                         new Color("FF000050") // red and quite transparent
95                 )
96         );
97     }
98
99     /**
100      * Set Avatar/Camera initial position in the world.
101      */
102     private void setAvatarOrientation(final Avatar avatar) {
103         avatar.setLocation(new Point3D(100, -50, -200));
104         avatar.setAngleXZ(0.2f);
105         avatar.setAngleYZ(-0.7f);
106     }
107
108 }