diff --git a/.idea/misc.xml b/.idea/misc.xml index 3a93b1f..dece74c 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,5 +8,5 @@ - + \ No newline at end of file diff --git a/core/src/se/cookery/CookeryClient.java b/core/src/se/cookery/CookeryClient.java index 5a30a3a..2416619 100644 --- a/core/src/se/cookery/CookeryClient.java +++ b/core/src/se/cookery/CookeryClient.java @@ -1,28 +1,28 @@ package se.cookery; import com.jme3.app.SimpleApplication; -import com.jme3.collision.CollisionResults; import com.jme3.input.ChaseCamera; import com.jme3.input.KeyInput; import com.jme3.input.MouseInput; -import com.jme3.input.controls.AnalogListener; import com.jme3.input.controls.KeyTrigger; import com.jme3.input.controls.MouseButtonTrigger; import com.jme3.math.ColorRGBA; -import com.jme3.math.Ray; -import com.jme3.math.Vector2f; import com.jme3.math.Vector3f; import com.jme3.post.FilterPostProcessor; -import com.jme3.scene.Geometry; import se.cookery.core.world.World; import se.cookery.core.world.gen.BlockGenerator; import se.cookery.core.world.gen.GrassLandBlockGenerator; import se.cookery.gfx.character.Player; +import se.cookery.gfx.input.MouseClickAction; import se.cookery.gfx.input.WireFrameToggleAction; import se.cookery.gfx.terrain.BlockHeightMap; import se.cookery.gfx.terrain.TerrainManager; import se.cookery.gfx.util.MaterialUtil; import com.jme3.post.filters.CartoonEdgeFilter; +import zutil.log.LogUtil; + +import java.util.logging.Level; + public class CookeryClient extends SimpleApplication { @@ -93,44 +93,10 @@ public class CookeryClient extends SimpleApplication { // Setup the action listeners inputManager.addListener(new WireFrameToggleAction(assetManager, viewPort), "ToggleKeyframe"); - inputManager.addListener(analogListener, "Click"); + inputManager.addListener(new MouseClickAction(inputManager, cam, rootNode), "Click"); } - private AnalogListener analogListener = new AnalogListener() { - public void onAnalog(String name, float value, float tpf) { - // Convert screen click to 3d position - Vector2f screenVector = inputManager.getCursorPosition(); - Vector3f worldVector = cam.getWorldCoordinates(new Vector2f(screenVector.x, screenVector.y), 0f).clone(); - Vector3f direction = cam.getWorldCoordinates(new Vector2f(screenVector.x, screenVector.y), 1f).subtractLocal(worldVector).normalizeLocal(); - - // Aim the ray from the clicked spot forwards. - Ray ray = new Ray(worldVector, direction); - - // Collect intersections between ray and all nodes in results list. - CollisionResults results = new CollisionResults(); - rootNode.collideWith(ray, results); - - System.out.println("Click registered:"); - - // (Print the results so we see what is going on:) - for (int i = 0; i < results.size(); i++) { - System.out.println("Selection #" + i + ": " + - "Geometry: " + results.getCollision(i).getGeometry().getName() + ", " + - "Contact: " + results.getCollision(i).getContactPoint() + ", " + - "Distance: " + results.getCollision(i).getDistance() + " WU"); - } - - // Use the results -- we rotate the selected geometry. - if (results.size() > 0) { - // The closest result is the target that the player picked: - Geometry target = results.getClosestCollision().getGeometry(); - // Here comes the action: -// target.setLocalTranslation(target.getLocalTranslation().add(1, 0, 0)); - } - } - }; - @Override public void simpleUpdate(float tpf) { chaseCam.update(tpf); diff --git a/core/src/se/cookery/gfx/character/Player.java b/core/src/se/cookery/gfx/character/Player.java index 73260f0..4742708 100644 --- a/core/src/se/cookery/gfx/character/Player.java +++ b/core/src/se/cookery/gfx/character/Player.java @@ -12,6 +12,7 @@ public class Player { public Player(AssetManager assetManager) { Geometry box = GeometryUtil.createBox(assetManager, 0.5f,0.7f,0.5f, ColorRGBA.Red); + box.setName("Player Box"); box.setLocalTranslation(0.5f, 0.35f, 0.5f); playerNode.attachChild(box); } diff --git a/core/src/se/cookery/gfx/input/MouseClickAction.java b/core/src/se/cookery/gfx/input/MouseClickAction.java new file mode 100644 index 0000000..37c9b45 --- /dev/null +++ b/core/src/se/cookery/gfx/input/MouseClickAction.java @@ -0,0 +1,63 @@ +package se.cookery.gfx.input; + +import com.jme3.asset.AssetManager; +import com.jme3.collision.CollisionResult; +import com.jme3.collision.CollisionResults; +import com.jme3.input.InputManager; +import com.jme3.input.controls.ActionListener; +import com.jme3.math.Ray; +import com.jme3.math.Vector2f; +import com.jme3.math.Vector3f; +import com.jme3.post.SceneProcessor; +import com.jme3.renderer.Camera; +import com.jme3.renderer.ViewPort; +import com.jme3.scene.Geometry; +import com.jme3.scene.Node; +import se.cookery.gfx.terrain.TerrainMesh; +import se.cookery.gfx.util.WireFrameProcessor; +import zutil.log.LogUtil; + +import java.util.ArrayList; +import java.util.logging.Logger; + +public class MouseClickAction implements ActionListener { + private static final Logger logger = LogUtil.getLogger(); + + private InputManager inputManager; + private Camera cam; + private Node rootNode; + + + public MouseClickAction(InputManager inputManager, Camera cam, Node rootNode) { + this.inputManager = inputManager; + this.cam = cam; + this.rootNode = rootNode; + } + + @Override + public void onAction(String name, boolean isPressed, float tpf) { + if (isPressed) { + // Convert screen click to 3d position + Vector2f screenVector = inputManager.getCursorPosition(); + Vector3f worldVector = cam.getWorldCoordinates(new Vector2f(screenVector.x, screenVector.y), 0f).clone(); + Vector3f direction = cam.getWorldCoordinates(new Vector2f(screenVector.x, screenVector.y), 1f).subtractLocal(worldVector).normalizeLocal(); + + // Aim the ray from the clicked spot forwards. + Ray ray = new Ray(worldVector, direction); + + // Collect intersections between ray and all nodes in results list. + CollisionResults results = new CollisionResults(); + rootNode.collideWith(ray, results); + + if (results.size() > 0) { + // The closest result is the target that the player picked: + Geometry target = results.getClosestCollision().getGeometry(); + System.out.println("Click registered: Geometry: " + target.getName()); + + if (target.getMesh() instanceof TerrainMesh) { + System.out.println("Terrain"); + } + } + } + } +}