X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fsixth%2Fe3d%2Fexamples%2Flife%2FMain.java;fp=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fsixth%2Fe3d%2Fexamples%2Flife%2FMain.java;h=527a6a21a2b10a17b20654bbadce8b7d734061f0;hb=151ac55eaaa0f3a2d7a589a0a3ff877b6db09a6f;hp=0000000000000000000000000000000000000000;hpb=3faa9ebcc7e643181ec70a26a69ba1dbd40e6ab6;p=sixth-3d-demos.git diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/life/Main.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/life/Main.java new file mode 100644 index 0000000..527a6a2 --- /dev/null +++ b/src/main/java/eu/svjatoslav/sixth/e3d/examples/life/Main.java @@ -0,0 +1,107 @@ +package eu.svjatoslav.sixth.e3d.examples.life; + +import eu.svjatoslav.sixth.e3d.geometry.Point3D; +import eu.svjatoslav.sixth.e3d.geometry.Rectangle; +import eu.svjatoslav.sixth.e3d.geometry.Transform; +import eu.svjatoslav.sixth.e3d.gui.Avatar; +import eu.svjatoslav.sixth.e3d.gui.ViewContext; +import eu.svjatoslav.sixth.e3d.gui.ViewFrame; +import eu.svjatoslav.sixth.e3d.gui.humaninput.WorldNavigationTracker; +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; + + +class Main extends WorldNavigationTracker { + + 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 void keyPressed(final KeyEvent event, final ViewContext viewContext) { + 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: + super.keyPressed(event, viewContext); + } + } + + private void run() { + + // create application frame visible to the user + final ViewFrame viewFrame = new ViewFrame(); + + final ShapeCollection shapeCollection = viewFrame.getView() + .getContext().getRootShapeCollection(); + + // add matrix + shapeCollection.addShape(MATRIX); + + // add wireframe grid (optional) + shapeCollection.addShape(createGrid()); + + final ViewContext context = viewFrame.getView().getContext(); + + setAvatarOrientation(context.getAvatar()); + + // enable receiving of keyboard events + context.getKeyboardFocusTracker().setFocusOwner(this); + + // Done! World is built. So ensure screen is updated too. + context.getView().repaintDuringNextViewUpdate(); + } + + /** + * Create pink wireframe 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); + } + +}