Code refactoring.
[sixth-3d-demos.git] / src / main / java / eu / svjatoslav / sixth / e3d / examples / life_demo / Star.java
diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/life_demo/Star.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/life_demo/Star.java
new file mode 100644 (file)
index 0000000..6f5edaa
--- /dev/null
@@ -0,0 +1,41 @@
+package eu.svjatoslav.sixth.e3d.examples.life_demo;
+
+import eu.svjatoslav.sixth.e3d.geometry.Point3D;
+import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
+import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.GlowingPoint;
+
+import java.util.ArrayList;
+import java.util.List;
+
+class Star extends GlowingPoint {
+    public static final int STAR_SIZE = 10;
+
+    public static final int UNIQUE_STARS_COUNT = 30;
+
+    private static final List<Color> uniqueStarColors = new ArrayList<>();
+
+    /*
+     * A little hack to save RAM. We are going to have potentially lot of stars.
+     * Instead of creating new individual texture for each star, Sixth 3D engine
+     * uses internal optimization and reuses existing star textures, if star with
+     * identical color already exists. To take advantage ot such optimization
+     * we create here limited set of precomputed star colors and later reuse them.
+     */
+    static {
+        for (int i = 0; i < UNIQUE_STARS_COUNT; i++)
+            uniqueStarColors.add(
+                    new Color(
+                            Math.random() + 0.5,
+                            Math.random() + 0.5,
+                            Math.random() + 0.5,
+                            255));
+    }
+
+    public Star(Point3D location) {
+        super(location,
+                STAR_SIZE,
+                uniqueStarColors.get((int) (Math.random() * uniqueStarColors.size())) // pick random pre-generated color
+        );
+    }
+
+}