package edu.ups.gamedev.player;
import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.MouseInput;
import com.jme.input.action.KeyNodeBackwardAction;
import com.jme.input.action.KeyNodeForwardAction;
import com.jme.input.action.KeyNodeRotateLeftAction;
import com.jme.input.action.KeyNodeRotateRightAction;
import com.jme.math.Vector3f;
import com.jme.scene.Spatial;
import com.jme.system.DisplaySystem;
import edu.ups.gamedev.game.TankGame;
import edu.ups.gamedev.game.Tools;
/**
* Handles the input for a <code>Tank</code>.
* @author stefan
*
*/
public class TankInputHandler extends InputHandler {
private MouseLookCamera camera;
private DisplaySystem display;
protected float sensitivity = 5;
protected Tank tank;
/**
* Supply the node to control and the api that will handle input creation.
*
* @param node
* the node we wish to move
* @param api
* the library that will handle creation of the input.
*/
public TankInputHandler(Tank tank) {
this.tank = tank;
setKeyBindings();
setActions(tank);
this.display = TankGame.GAME.getDisplay();
this.camera = TankGame.GAMESTATE.getCamera();
}
/**
* creates the keyboard object, allowing us to obtain the values of a keyboard as keys are pressed. It then sets the actions to be triggered based on if certain keys are pressed (WSAD).
*/
private void setKeyBindings() {
KeyBindingManager keyboard = KeyBindingManager.getKeyBindingManager();
keyboard.set("forward", KeyInput.KEY_W);
keyboard.set("backward", KeyInput.KEY_S);
keyboard.set("turnRight", KeyInput.KEY_D);
keyboard.set("turnLeft", KeyInput.KEY_A);
}
/**
* assigns action classes to triggers. These actions handle moving the node forward, backward and rotating it.
*
* @param node
* the node to control.
*/
private void setActions(Spatial node) {
KeyNodeForwardAction forward = new KeyNodeForwardAction(node, 50f);
//PhysicsMoveKeyAction forward = new PhysicsMoveKeyAction(tank, 2000000f);
addAction(forward, "forward", true);
KeyNodeBackwardAction backward = new KeyNodeBackwardAction(node, 40f);
//PhysicsMoveKeyAction backward = new PhysicsMoveKeyAction(tank, -2000000f);
addAction(backward, "backward", true);
KeyNodeRotateRightAction rotateRight = new KeyNodeRotateRightAction(node, 5f);
rotateRight.setLockAxis(node.getLocalRotation().getRotationColumn(1));
//PhysicsRotateKeyAction rotateRight = new PhysicsRotateKeyAction(tank, -200000f);
addAction(rotateRight, "turnRight", true);
KeyNodeRotateLeftAction rotateLeft = new KeyNodeRotateLeftAction(node,5f);
rotateLeft.setLockAxis(node.getLocalRotation().getRotationColumn(1));
//PhysicsRotateKeyAction rotateLeft = new PhysicsRotateKeyAction(tank, 200000f);
addAction(rotateLeft, "turnLeft", true);
}
@Override
public void update(float arg0) {
super.update(arg0);
// set mouselook
int xCenter = display.getWidth() / 2;
int yCenter = display.getHeight() / 2;
// get mouse offset from center
float xDiff = (MouseInput.get().getXAbsolute() - xCenter);
float yDiff = (yCenter - MouseInput.get().getYAbsolute());
// rotate and incline the camera based on the mouse displacement
camera.rotate(xDiff * sensitivity / 50);
camera.incline(yDiff * sensitivity / 50);
// "spring" the mouse back to center
MouseInput.get().setCursorPosition(xCenter, yCenter);
// handle mouse firing
if (MouseInput.get().isButtonDown(0)) {
// TODO find a better way to get the tank's root node, and don't generate new vectors all the time.
Vector3f vector = new Vector3f();
Tools.pointPick(camera.getLookRay(), TankGame.GAMESTATE.getRoot(), vector);
tank.getTurret().orientTo(vector);
tank.firePrimaryWeapon();
}
// handle mouse firing
if (MouseInput.get().isButtonDown(1)) {
// TODO find a better way to get the tank's root node, and don't generate new vectors all the time.
Vector3f vector = new Vector3f();
Tools.pointPick(camera.getLookRay(), TankGame.GAMESTATE.getRoot(), vector);
tank.getTurret().orientTo(vector);
tank.fireSecondaryWeapon();
}
}
}