From 275bea8a913af3e56c991637ded5504cd23f6167 Mon Sep 17 00:00:00 2001 From: Dejvino Date: Wed, 25 Jan 2017 22:19:11 +0100 Subject: [PATCH] Map: re-enabled map objects, cleanup. --- src/roadtrip/model/AbstractProceduralBlock.java | 16 +++++++++++++++- src/roadtrip/model/ProceduralMapQuadBlock.java | 2 +- src/roadtrip/view/FineTerrainGrid.java | 8 ++++---- src/roadtrip/view/GameWorldView.java | 5 ++--- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/roadtrip/model/AbstractProceduralBlock.java b/src/roadtrip/model/AbstractProceduralBlock.java index f7d445d..2ecebef 100644 --- a/src/roadtrip/model/AbstractProceduralBlock.java +++ b/src/roadtrip/model/AbstractProceduralBlock.java @@ -1,5 +1,6 @@ package roadtrip.model; +import com.jme3.terrain.geomipmap.LRUCache; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.Random; @@ -11,6 +12,8 @@ public abstract class AbstractProceduralBlock implements ProceduralBlock { private long seed; + private LRUCache subBlocksCache = new LRUCache<>(getSubBlocksCacheSize()); + public AbstractProceduralBlock(long seed) { this.seed = seed; @@ -38,13 +41,24 @@ public abstract class AbstractProceduralBlock implements ProceduralBlock public T getSubBlock(String subBlockKey, Class subBlockClass) { if (subBlockClass == null) throw new NullPointerException("subBlockClass"); + T result = (T) subBlocksCache.get(subBlockKey); + if (result != null) { + return result; + } try { Constructor constructor = subBlockClass.getConstructor(Long.TYPE); - return constructor.newInstance(getSubBlockSeed(subBlockKey)); + result = constructor.newInstance(getSubBlockSeed(subBlockKey)); + subBlocksCache.put(subBlockKey, result); + return result; } catch (NoSuchMethodException e) { throw new IllegalArgumentException("Class " + subBlockClass + " does not have the default constructor with a single 'long' parameter.", e); } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) { throw new IllegalArgumentException("Unable to instantiate sub-block.", e); } } + + public int getSubBlocksCacheSize() + { + return 8; + } } diff --git a/src/roadtrip/model/ProceduralMapQuadBlock.java b/src/roadtrip/model/ProceduralMapQuadBlock.java index 8fd1caa..62360c6 100644 --- a/src/roadtrip/model/ProceduralMapQuadBlock.java +++ b/src/roadtrip/model/ProceduralMapQuadBlock.java @@ -32,7 +32,7 @@ public class ProceduralMapQuadBlock extends AbstractProceduralBlock Random quadRand = getBlockRandom(); Vector2f prevPos = null; Vector2f quadPos = new Vector2f(terrainQuad.getLocalTranslation().x, terrainQuad.getLocalTranslation().z); - for (int i = 0; i < quadRand.nextInt(10000); i++) { + for (int i = 0; i < quadRand.nextInt(100); i++) { Vector2f pos; if (prevPos == null || quadRand.nextFloat() < 0.2f) { pos = new Vector2f((quadRand.nextFloat() - 0.5f) * cellSize, (quadRand.nextFloat() - 0.5f) * cellSize) diff --git a/src/roadtrip/view/FineTerrainGrid.java b/src/roadtrip/view/FineTerrainGrid.java index ec33b5b..860b9ee 100644 --- a/src/roadtrip/view/FineTerrainGrid.java +++ b/src/roadtrip/view/FineTerrainGrid.java @@ -421,15 +421,15 @@ public class FineTerrainGrid extends TerrainQuad { int xMax = getSubdivisionsPerSide(); int yMin = 0; int yMax = getSubdivisionsPerSide(); - if (dx == -1) { // camera moved to -X direction + if (dx < 0) { // camera moved to -X direction xMax = getSubdivisionsPerSide() - 1; - } else if (dx == 1) { // camera moved to +X direction + } else if (dx > 0) { // camera moved to +X direction xMin = 1; } - if (dy == -1) { // camera moved to -Y direction + if (dy < 0) { // camera moved to -Y direction yMax = getSubdivisionsPerSide() - 1; - } else if (dy == 1) { // camera moved to +Y direction + } else if (dy > 0) { // camera moved to +Y direction yMin = 1; } diff --git a/src/roadtrip/view/GameWorldView.java b/src/roadtrip/view/GameWorldView.java index 49f9161..d807bb6 100644 --- a/src/roadtrip/view/GameWorldView.java +++ b/src/roadtrip/view/GameWorldView.java @@ -36,7 +36,7 @@ import roadtrip.view.model.GameWorldState; */ public class GameWorldView { - public static boolean DEBUG = true; + public static boolean DEBUG = false;//true; private final GameWorldState state; @@ -184,8 +184,7 @@ public class GameWorldView { String quadObjectsNodeKey = getQuadObjectsNodeKey(quad); Node objects = new Node(quadObjectsNodeKey); - // TODO: fix - //populateQuadObjectsNode(quad, objects); + populateQuadObjectsNode(quad, objects); rootNode.attachChild(objects); }