private boolean left = false, right = false, up = false, down = false,
mouseLeft = false,
key_e = false, key_q = false,
key_shift = false;
private void setUpKeys() {
inputManager.clearMappings();
inputManager.addMapping("MouseLeft", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
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("E", new KeyTrigger(KeyInput.KEY_E));
inputManager.addMapping("Q", new KeyTrigger(KeyInput.KEY_F)); // KEY_F because KEY_Q make Problems. It works perfectly with KEY_F but wrong with KEY_Q
inputManager.addMapping("Shift", new KeyTrigger(KeyInput.KEY_LSHIFT));
inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("Esc", new KeyTrigger(KeyInput.KEY_ESCAPE));
inputManager.addListener(this, "Left");
inputManager.addListener(this, "Right");
inputManager.addListener(this, "Up");
inputManager.addListener(this, "Down");
inputManager.addListener(this, "Jump");
inputManager.addListener(this, "Shift");
inputManager.addListener(actionListener, "E");
inputManager.addListener(actionListener, "Q");
inputManager.addListener(actionListener, "MouseLeft");
inputManager.addListener(actionListener, "Esc");
}
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals("Esc") && !keyPressed) { stop(); }
if (name.equals("MouseLeft") && !keyPressed) { mouseLeft = true; } else { mouseLeft = false; }
if (name.equals("E") && !keyPressed) { key_e = true; } else { key_e = false; }
if (name.equals("Q") && !keyPressed) { key_q = true; } else { key_q = false; }
}
};
public void key_qAction() { // Take something in left Hand
CollisionResults results = new CollisionResults();
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
game.collideWith(ray, results);
System.out.println(playerLeftHand);
if (playerLeftHand.equals("")) {
System.out.println("ok");
if (results.size() > 0) {
System.out.println("results > 0");
CollisionResult closest = results.getClosestCollision();
System.out.println(closest.getGeometry().getName());
if (closest.getDistance() <= actionDistance) {
System.out.println("Distance > ");
Node countNode = (Node)closest.getGeometry().getParent();
System.out.println(countNode.getName());
if (!countNode.getName().equals("Game")) {
Node category = (Node) countNode.getParent();
if (category.getName().equals("Rocks")) {
int id = Integer.parseInt(closest.getGeometry().getName().split("-")[closest.getGeometry().getName().split("-").length-1]);
playerLeftHand = "Rocks-"+id;
}
if (category.getName().equals("Saplings")) {
int id = Integer.parseInt(closest.getGeometry().getName().split("-")[closest.getGeometry().getName().split("-").length-1]);
playerLeftHand = "Saplings-"+id;
}
if (category.getName().equals("Sticks")) {
int id = Integer.parseInt(closest.getGeometry().getName().split("-")[closest.getGeometry().getName().split("-").length-1]);
playerLeftHand = "Sticks-"+id;
}
}
}
}
} else {
if (playerLeftHand.split("-")[0].equals("Rocks") && !animateRock) {
int id = Integer.parseInt(playerLeftHand.split("-")[playerLeftHand.split("-").length-1]);
Node rock = (Node) rocks.getChild(id+"");
Vector3f camDir = cam.getDirection();
Vector3f vector;
vector = new Vector3f(0,0,0).addLocal(camDir.clone().multLocal(5f));
Vector3f newPos = new Vector3f(player.getPhysicsLocation().x+vector.x,player.getPhysicsLocation().y+vector.y,player.getPhysicsLocation().z+vector.z);
rock.setLocalTranslation(newPos);
rock.setLocalRotation(cam.getRotation());
playerLeftHand = "";
} else if (playerLeftHand.split("-")[0].equals("Saplings")) {
if (results.size() > 0) {
CollisionResult closest = results.getClosestCollision();
if (closest.getDistance() <= actionDistance && closest.getGeometry().getName().split("-")[0].equals("terrain")) {
int id = Integer.parseInt(playerLeftHand.split("-")[playerLeftHand.split("-").length-1]);
Node sapling = (Node) saplings.getChild(id+"");
Quaternion pitch0 = new Quaternion();
pitch0.fromAngleAxis(FastMath.PI / 2, new Vector3f(0,0,0));
sapling.setLocalTranslation(closest.getContactPoint());
sapling.setLocalRotation(pitch0);
sapling.setUserData("growUpTimeout", newGrowUpTimer());
playerLeftHand = "";
}
}
}
}
}
public void key_eAction() { // Take something in right Hand
CollisionResults results = new CollisionResults();
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
game.collideWith(ray, results);
if (playerRightHand.equals("")) {
if (results.size() > 0) {
CollisionResult closest = results.getClosestCollision();
if (closest.getDistance() <= actionDistance) {
Node countNode = (Node)closest.getGeometry().getParent();
if (!countNode.getName().equals("Game")) {
Node category = (Node) countNode.getParent();
if (category.getName().equals("Rocks")) {
int id = Integer.parseInt(closest.getGeometry().getName().split("-")[closest.getGeometry().getName().split("-").length-1]);
playerRightHand = "Rocks-"+id;
}
if (category.getName().equals("Saplings")) {
int id = Integer.parseInt(closest.getGeometry().getName().split("-")[closest.getGeometry().getName().split("-").length-1]);
playerRightHand = "Saplings-"+id;
}
if (category.getName().equals("Sticks")) {
int id = Integer.parseInt(closest.getGeometry().getName().split("-")[closest.getGeometry().getName().split("-").length-1]);
playerRightHand = "Sticks-"+id;
}
}
}
}
} else {
if (playerRightHand.split("-")[0].equals("Rocks") && !animateRock) {
int id = Integer.parseInt(playerRightHand.split("-")[playerRightHand.split("-").length-1]);
Node rock = (Node) rocks.getChild(id+"");
Vector3f camDir = cam.getDirection();
Vector3f vector;
vector = new Vector3f(0,0,0).addLocal(camDir.clone().multLocal(5f));
Vector3f newPos = new Vector3f(player.getPhysicsLocation().x+vector.x,player.getPhysicsLocation().y+vector.y,player.getPhysicsLocation().z+vector.z);
rock.setLocalTranslation(newPos);
rock.setLocalRotation(cam.getRotation());
playerRightHand = "";
} else if (playerRightHand.split("-")[0].equals("Saplings")) {
if (results.size() > 0) {
CollisionResult closest = results.getClosestCollision();
if (closest.getDistance() <= actionDistance && closest.getGeometry().getName().split("-")[0].equals("terrain")) {
int id = Integer.parseInt(playerRightHand.split("-")[playerRightHand.split("-").length-1]);
Node sapling = (Node) saplings.getChild(id+"");
Quaternion pitch0 = new Quaternion();
pitch0.fromAngleAxis(FastMath.PI / 2, new Vector3f(0,0,0));
sapling.setLocalTranslation(closest.getContactPoint());
sapling.setLocalRotation(pitch0);
sapling.setUserData("growUpTimeout", newGrowUpTimer());
playerRightHand = "";
}
}
}
}
}
@Override
public void simpleUpdate(float tpf) {
if (key_e) {
key_e = false;
key_eAction();
}
if (key_q) {
key_q = false;
key_qAction();
}
}