public enum InputMapping{
RotateLeft, RotateRight, LookUp, LookDown, StrafeLeft, StrafeRight,
MoveForward, MoveBackward, Fire, LeanLeft, LeanRight, LeanFree
}
private void addInputMappings(){
inputManager.addMapping(InputMapping.LeanLeft.name(),
new KeyTrigger(KeyInput.KEY_Q));
inputManager.addMapping(InputMapping.LeanRight.name(),
new KeyTrigger(KeyInput.KEY_E));
inputManager.addMapping(InputMapping.LeanFree.name(),
new KeyTrigger(KeyInput.KEY_V));
inputManager.addMapping(InputMapping.Fire.name(),
new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addMapping(InputMapping.RotateLeft.name(),
new MouseAxisTrigger(MouseInput.AXIS_X, true));
inputManager.addMapping(InputMapping.RotateRight.name(),
new MouseAxisTrigger(MouseInput.AXIS_X, false));
inputManager.addMapping(InputMapping.LookUp.name(),
new MouseAxisTrigger(MouseInput.AXIS_Y, false));
inputManager.addMapping(InputMapping.LookDown.name(),
new MouseAxisTrigger(MouseInput.AXIS_Y, true));
inputManager.addMapping(InputMapping.StrafeLeft.name(),
new KeyTrigger(KeyInput.KEY_LEFT));
inputManager.addMapping(InputMapping.StrafeRight.name(),
new KeyTrigger(KeyInput.KEY_RIGHT));
inputManager.addMapping(InputMapping.MoveForward.name(),
new KeyTrigger(KeyInput.KEY_UP));
inputManager.addMapping(InputMapping.MoveBackward.name(),
new KeyTrigger(KeyInput.KEY_DOWN));
for(InputMapping i : InputMapping.values()){
inputManager.addListener(this, i.name());
}
}
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.inputManager = app.getInputManager();
addInputMappings();
//TODO: initialize your AppState, e.g. attach spatials to rootNode
//this is called on the OpenGL thread after the AppState has been attached
}
public void onAnalog(String name, float value, float tpf){
if(character != null){
character.onAnalog(name, value, tpf);
}
}
public void onAction(String name, boolean isPressed, float tpf){
if(character != null){
character.onAction(name, isPressed, tpf);
}
if(name.equals("Fire")){
if(isPressed && character.getCooldown() == 0f){
fire();
}else{
character.onAction(name, isPressed, tpf);
}
}
}
public void fire(){
Ray ray = new Ray(cam.getCamera().getLocation(), //error on cam.getCamera()
cam.getCamera().getDirection());
collResults = new CollisionResults();
for(Geometry g : targets){
g.collideWith(ray, collResults);
}
if(collResults.size()>0){
System.out.println("hit"+collResults.getClosestCollision()
.getContactPoint());
character.onFire();
}
}
@Override
public void update(float tpf) {
//TODO: implement behavior during runtime
}
@Override
public void cleanup() {
super.cleanup();
for(InputMapping i : InputMapping.values()){
if(inputManager.hasMapping(i.name())){
inputManager.deleteMapping(i.name());
}
}
inputManager.removeListener(this);
//TODO: clean up what you initialized in the initialize method,
//e.g. remove all spatials from rootNode
//this is called on the OpenGL thread after the AppState has been detached
}
}
//WHY DO I HAVE THAT FREAKING GET CAMERA ERROR! lol it’s been buggin’ the crap out of me