Fixed click input
This commit is contained in:
parent
6bdada9846
commit
9518118651
4 changed files with 71 additions and 41 deletions
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
|
|
@ -8,5 +8,5 @@
|
|||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" project-jdk-name="11" project-jdk-type="JavaSDK" />
|
||||
</project>
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
63
core/src/se/cookery/gfx/input/MouseClickAction.java
Normal file
63
core/src/se/cookery/gfx/input/MouseClickAction.java
Normal file
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue