I have this game that is very simple due to time and tools that I have, it’s for a university project. Well, I have houses on the terrain ( I did the worst job EVER putting them on the terrain) and inside one of them, there’s a chest (cube)…what I want to do to is: Once the player finds the house in which the chest is inside and sees the chest or is in the same position, I click with the left button of the mouse and the chest disappears (I collect it), but I really didn’t find much information regarding to mouse usage and I’m 100% noob on JMonkey, I honestly use it only because the professor required me to. So…what can I do? My code (horrible, and it’s working, so please, just stick to my question)
[java]package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
public class Main extends SimpleApplication {
private BulletAppState bulletAppState;
private RigidBodyControl landscape;
private CharacterControl player;
private Vector3f walkDirection = new Vector3f();
private boolean left = false, right = false, up = false, down = false;
private Vector3f camDir = new Vector3f();
private Vector3f camLeft = new Vector3f();
private Geometry cube;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
setDisplayStatView(false); //Desliga as estatisticas que aparecem na tela do jogo.
setDisplayFps(false);//Não permite que FPS seja mostrada na tela
setUpKeys();
// Configurações da câmera.
flyCam.setMoveSpeed(10);
flyCam.setRotationSpeed(10);
// Inicializar physics.
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
// Carregar objetos da cena.
createCube();
Spatial ferrari = assetManager.loadModel("Models/Ferrari.j3o");
ferrari.setLocalTranslation(12,0,0);
rootNode.attachChild(ferrari);
Spatial house = assetManager.loadModel("Models/cg_house_A.j3o");
house.setLocalTranslation(4, 0, 0);
rootNode.attachChild(house);
Spatial house2 =assetManager.loadModel("Models/cg_house_A.j3o");
house2.setLocalTranslation(30,0,0);
rootNode.attachChild(house2);
Spatial house3 =assetManager.loadModel("Models/cg_house_A.j3o");
house3.setLocalTranslation(45,0,0);
rootNode.attachChild(house3);
Spatial house4 =assetManager.loadModel("Models/cg_house_A.j3o");
house4.setLocalTranslation(35,0,-80);
rootNode.attachChild(house4);
Spatial house5 =assetManager.loadModel("Models/cg_house_A.j3o");
house5.setLocalTranslation(-35,0,80);
rootNode.attachChild(house5);
Spatial house6 =assetManager.loadModel("Models/cg_house_A.j3o");
house6.setLocalTranslation(80,0,-80);
rootNode.attachChild(house6);
Spatial house7 =assetManager.loadModel("Models/cg_house_A.j3o");
house7.setLocalTranslation(60,0,-50);
rootNode.attachChild(house7);
Spatial house8 =assetManager.loadModel("Models/cg_house_A.j3o");
house8.setLocalTranslation(78,0,-60);
rootNode.attachChild(house8);
Spatial house9 =assetManager.loadModel("Models/cg_house_A.j3o");
house9.setLocalTranslation(-80,0,-29);
rootNode.attachChild(house9);
Spatial house10 =assetManager.loadModel("Models/cg_house_A.j3o");
house10.setLocalTranslation(-90,0,-46);
rootNode.attachChild(house10);
Spatial house11 =assetManager.loadModel("Models/cg_house_A.j3o");
house11.setLocalTranslation(100,0,-90);
rootNode.attachChild(house11);
Spatial house12 =assetManager.loadModel("Models/cg_house_A.j3o");
house12.setLocalTranslation(-84,0,0);
rootNode.attachChild(house12);
Spatial house13 =assetManager.loadModel("Models/cg_house_A.j3o");
house13.setLocalTranslation(52,0,-54);
rootNode.attachChild(house13);
Spatial house14 =assetManager.loadModel("Models/cg_house_A.j3o");
house14.setLocalTranslation(-94,0,10);
rootNode.attachChild(house14);
Spatial house15 =assetManager.loadModel("Models/cg_house_A.j3o");
house15.setLocalTranslation(50,0,50);
rootNode.attachChild(house15);
Spatial house16 =assetManager.loadModel("Models/cg_house_A.j3o");
house16.setLocalTranslation(-10,0,-70);
rootNode.attachChild(house16);
Spatial house17 =assetManager.loadModel("Models/cg_house_A.j3o");
house17.setLocalTranslation(63,0,-40);
rootNode.attachChild(house17);
Spatial terrain = assetManager.loadModel("Scenes/TerrenoJogo.j3o");
// terrain.setLocalTranslation(0, 0, 0);
// rootNode.attachChild(terrain);
AmbientLight light = new AmbientLight();
light.setColor(ColorRGBA.White.mult(6f));
rootNode.addLight(light);
viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
CollisionShape sceneShape =
CollisionShapeFactory.createMeshShape(terrain);
landscape = new RigidBodyControl(sceneShape, 0);
terrain.addControl(landscape);
CapsuleCollisionShape capsuleShape =
new CapsuleCollisionShape(1.5f, 3f, 1);
player = new CharacterControl(capsuleShape, 0.05f);
player.setJumpSpeed(20);
player.setFallSpeed(30);
player.setGravity(30);
player.setPhysicsLocation(new Vector3f(0, 5, 0));
rootNode.attachChild(terrain);
bulletAppState.getPhysicsSpace().add(landscape);
bulletAppState.getPhysicsSpace().add(player);
}
private void createCube(){
Box b = new Box(0.5f, 0.6f, 0.5f);
cube = new Geometry("Cube", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setTexture("ColorMap",assetManager.loadTexture("Textures/treasure-fish.jpg"));
mat.setColor("Color", ColorRGBA.Gray);
cube.setMaterial(mat);
cube.setLocalTranslation(37,0,-80); //Localização do cubo.
rootNode.attachChild(cube);
}
private void setUpKeys() {
MovimentoListener listener = new MovimentoListener();
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("Collect", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(listener, "Left");
inputManager.addListener(listener, "Right");
inputManager.addListener(listener, "Up");
inputManager.addListener(listener, "Down");
inputManager.addListener(listener, "Jump");
inputManager.addListener(listener, "Collect");
}
@Override
public void simpleUpdate(float tpf) {
camDir.set(cam.getDirection()).multLocal(0.6f);
camLeft.set(cam.getLeft()).multLocal(0.4f);
walkDirection.set(0, 0, 0);
if (left) {
walkDirection.addLocal(camLeft);
}
if (right) {
walkDirection.addLocal(camLeft.negate());
}
if (up) {
walkDirection.addLocal(camDir);
}
if (down) {
walkDirection.addLocal(camDir.negate());
}
player.setWalkDirection(walkDirection);
cam.setLocation(player.getPhysicsLocation());
}
private class MovimentoListener implements ActionListener {
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals("Left")) {
left = isPressed;
} else if (name.equals("Right")) {
right = isPressed;
} else if (name.equals("Up")) {
up = isPressed;
} else if (name.equals("Down")) {
down = isPressed;
} else if (name.equals("Jump")) {
if (isPressed) {
player.jump();
}
}
}
}
}
[/java]