From 6214ff34e82513cd439f102065077cb23207bf37 Mon Sep 17 00:00:00 2001 From: Dejvino Date: Sat, 28 Jan 2017 01:29:45 +0100 Subject: [PATCH] Added HideControl to hide eye-candy objects in the distance. --- src/roadtrip/RoadTripPlanner.java | 2 +- src/roadtrip/view/GameWorldView.java | 1 + src/roadtrip/view/HideControl.java | 77 ++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/roadtrip/view/HideControl.java diff --git a/src/roadtrip/RoadTripPlanner.java b/src/roadtrip/RoadTripPlanner.java index 90e8d01..6ecb781 100644 --- a/src/roadtrip/RoadTripPlanner.java +++ b/src/roadtrip/RoadTripPlanner.java @@ -18,7 +18,7 @@ public class RoadTripPlanner extends SimpleApplication { app.start(); } - public static boolean DEBUG = /*false;/*/true;/**/ + public static boolean DEBUG = !true;/**/ protected BulletAppState bulletAppState; private GameWorldState gameWorldState; diff --git a/src/roadtrip/view/GameWorldView.java b/src/roadtrip/view/GameWorldView.java index 0f8e758..d52fe80 100644 --- a/src/roadtrip/view/GameWorldView.java +++ b/src/roadtrip/view/GameWorldView.java @@ -264,6 +264,7 @@ public class GameWorldView { } modelInstance.setLocalTranslation(pos); modelInstance.setLocalScale(scale); + modelInstance.addControl(new HideControl()); objects.attachChild(modelInstance); } } diff --git a/src/roadtrip/view/HideControl.java b/src/roadtrip/view/HideControl.java new file mode 100644 index 0000000..492734e --- /dev/null +++ b/src/roadtrip/view/HideControl.java @@ -0,0 +1,77 @@ +package roadtrip.view; + +import java.util.ArrayList; +import com.jme3.bounding.BoundingVolume; +import com.jme3.renderer.Camera; +import com.jme3.renderer.RenderManager; +import com.jme3.renderer.ViewPort; +import com.jme3.scene.Node; +import com.jme3.scene.Spatial; +import com.jme3.scene.control.AbstractControl; +import com.jme3.scene.control.Control; + +public class HideControl extends AbstractControl +{ + private static final float DISTANCE_HIDE = 200; + + private ArrayList children; + private boolean hidden; + + private BoundingVolume prevBv; + + @Override + public Control cloneForSpatial(Spatial spatial) + { + // TODO + return null; + } + + @Override + protected void controlRender(RenderManager rm, ViewPort vp) + { + Camera cam = vp.getCamera(); + BoundingVolume bv = spatial.getWorldBound(); + + if (bv == null) { + bv = prevBv; + } else { + prevBv = bv; + } + + float distance = bv.distanceTo(cam.getLocation()); + + if (distance > HideControl.DISTANCE_HIDE) { + if (!hidden) { + for (int i = 0; i < children.size(); i++) { + children.get(i).removeFromParent(); + } + spatial.updateGeometricState(); + hidden = true; + } + } else { + if (hidden) { + for (int i = 0; i < children.size(); i++) { + ((Node) spatial).attachChild(children.get(i)); + } + } + spatial.updateGeometricState(); + hidden = false; + } + } + + @Override + protected void controlUpdate(float tpf) + { + } + + @Override + public void setSpatial(Spatial spatial) + { + if (!(spatial instanceof Node)) { + throw new IllegalArgumentException("only Node type is supported"); + } + super.setSpatial(spatial); + + children = new ArrayList<>(((Node) spatial).getChildren()); + } +}