Improved code readability
[sixth-3d-demos.git] / src / main / java / eu / svjatoslav / sixth / e3d / examples / RainingNumbersDemo.java
1 /*
2  * Sixth 3D engine demos. Author: Svjatoslav Agejenko. 
3  * This project is released under Creative Commons Zero (CC0) license.
4  *
5 */
6
7 package eu.svjatoslav.sixth.e3d.examples;
8
9 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
10 import eu.svjatoslav.sixth.e3d.gui.ViewFrame;
11 import eu.svjatoslav.sixth.e3d.gui.ViewPanel;
12 import eu.svjatoslav.sixth.e3d.gui.ViewRenderListener;
13 import eu.svjatoslav.sixth.e3d.math.Transform;
14 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
15 import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection;
16 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractShape;
17 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas.TextCanvas;
18
19 import java.util.Collection;
20 import java.util.Random;
21
22 public class RainingNumbersDemo implements ViewRenderListener {
23
24     private static final int NUMBERS_COUNT = 1000;
25     private final static int AREA = 600;
26     private final static int AREA_HALF = AREA / 2;
27
28     public static void main(final String[] args) {
29         new RainingNumbersDemo().run();
30     }
31
32     @Override
33     public boolean beforeRender(final ViewPanel viewPanel,
34                                 final int millisecondsSinceLastFrame) {
35
36         final Collection<AbstractShape> shapes = viewPanel
37                 .getRootShapeCollection().getShapes();
38
39         final double translateAmount = millisecondsSinceLastFrame / 50d;
40
41         shapes.stream().filter(shape -> shape instanceof TextCanvas).forEach(shape -> {
42             final TextCanvas block = (TextCanvas) shape;
43             final Point3D location = block.getLocation();
44             location.translateY(translateAmount);
45
46             if (location.y > AREA_HALF)
47                 location.y -= AREA;
48         });
49
50         return true;
51     }
52
53     private void run() {
54         final ViewFrame viewFrame = new ViewFrame();
55
56         final ShapeCollection geometryCollection = viewFrame.getViewPanel()
57                 .getRootShapeCollection();
58
59         Random random = new Random();
60
61         for (int i = 0; i < NUMBERS_COUNT; i++) {
62             final Point3D location = new Point3D((Math.random() * AREA)
63                     - AREA_HALF, (Math.random() * AREA) - AREA_HALF,
64                     (Math.random() * AREA) - AREA_HALF);
65
66             final Color color = new Color(Math.random(), Math.random(),
67                     Math.random(), Math.random());
68
69             final TextCanvas textCanvas = new TextCanvas(
70                     new Transform(location), String.valueOf(random.nextInt(10)), color,
71                     Color.TRANSPARENT);
72
73             geometryCollection.addShape(textCanvas);
74         }
75
76         viewFrame.getViewPanel().addViewUpdateListener(this);
77     }
78 }