Code refactoring.
[sixth-3d-demos.git] / src / main / java / eu / svjatoslav / sixth / e3d / examples / life_demo / Main.java
diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/life_demo/Main.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/life_demo/Main.java
new file mode 100644 (file)
index 0000000..cf47e7f
--- /dev/null
@@ -0,0 +1,108 @@
+package eu.svjatoslav.sixth.e3d.examples.life_demo;
+
+import eu.svjatoslav.sixth.e3d.geometry.Point3D;
+import eu.svjatoslav.sixth.e3d.geometry.Rectangle;
+import eu.svjatoslav.sixth.e3d.gui.Avatar;
+import eu.svjatoslav.sixth.e3d.gui.ViewFrame;
+import eu.svjatoslav.sixth.e3d.gui.ViewPanel;
+import eu.svjatoslav.sixth.e3d.gui.humaninput.WorldNavigationUserInputTracker;
+import eu.svjatoslav.sixth.e3d.math.Transform;
+import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
+import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection;
+import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.LineAppearance;
+import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.wireframe.Grid2D;
+
+import java.awt.event.KeyEvent;
+
+
+public class Main extends WorldNavigationUserInputTracker {
+
+    private static final Matrix MATRIX = new Matrix(
+            new Point3D() // position matrix in the center of the scene
+    );
+
+    public static void main(final String[] args) {
+        new Main().run();
+    }
+
+    /**
+     * Handle keyboard input.
+     */
+    @Override
+    public boolean keyPressed(final KeyEvent event, final ViewPanel viewPanel) {
+        switch (event.getKeyChar()) {
+            case ' ': // space key
+                MATRIX.evolve(false);
+                break;
+            case 10: // ENTER
+                MATRIX.evolve(true);
+                break;
+            case 'c': // reset matrix
+                MATRIX.clear();
+                break;
+            default:
+                return super.keyPressed(event, viewPanel);
+        }
+        return true;
+    }
+
+    private void run() {
+
+        // create application frame visible to the user
+        final ViewFrame viewFrame = new ViewFrame();
+
+        final ShapeCollection shapeCollection = viewFrame.getViewPanel()
+                .getRootShapeCollection();
+
+        // add matrix
+        shapeCollection.addShape(MATRIX);
+
+        // add wire-frame grid (optional)
+        shapeCollection.addShape(createGrid());
+
+        final ViewPanel viewPanel = viewFrame.getViewPanel();
+
+        setAvatarOrientation(viewPanel.getAvatar());
+
+        // enable receiving of keyboard events
+        viewPanel.getKeyboardFocusStack().pushFocusOwner(this);
+
+        // Done! World is built. So ensure screen is updated too.
+        viewPanel.repaintDuringNextViewUpdate();
+    }
+
+    /**
+     * Create pink wire-frame grid below (for decorative purposes).
+     */
+    private Grid2D createGrid() {
+        return new Grid2D(
+                new Transform(
+                        new Point3D( // Grid positioning:
+                                0, // center
+                                100, // below the main scene
+                                0), // center
+
+                        // Grid orientation:
+                        0, // no rotation along XZ axis
+                        Math.PI / 2), // face down
+
+                new Rectangle(800), // large enough, square grid
+
+                5, 5, // grid will be divided to 5x5 segments
+
+                new LineAppearance(3, // line thickness
+                        new Color("FF000050") // red and quite transparent
+                )
+        );
+    }
+
+    /**
+     * Set Avatar/Camera initial position in the world.
+     */
+    private void setAvatarOrientation(final Avatar avatar) {
+        avatar.setLocation(new Point3D(100, -50, -200));
+        avatar.setAngleXZ(0.2f);
+        avatar.setAngleYZ(-0.7f);
+    }
+
+}