package sg.input;

import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.scene.Node;
import com.jmex.physics.DynamicPhysicsNode;


public class SGFirstPersonHandler extends GlobalInputHandler{
	private final Vector3f appliedForce = new Vector3f();
	private Vector3f rotation;
	private Node ship;
	private DynamicPhysicsNode shipNode;
	private Camera cam;

	public SGFirstPersonHandler(Node ship, DynamicPhysicsNode shipNode, Camera cam){
		this.ship = ship;
		this.shipNode = shipNode;
		this.cam = cam;

		KeyBindingManager manager = KeyBindingManager.getKeyBindingManager();
		manager.set("yaw", KeyInput.KEY_K);
		manager.set("yaw2", KeyInput.KEY_I);
		manager.set("roll", KeyInput.KEY_U);
		manager.set("roll2", KeyInput.KEY_O);
		manager.set("pitch", KeyInput.KEY_J);
		manager.set("pitch2", KeyInput.KEY_L);
		manager.set("forw", KeyInput.KEY_2);
		manager.set("backw", KeyInput.KEY_1);
	}

	public void update(float time){
		super.update(time);
		rotation = ship.getLocalRotation().getRotationColumn(2);
		if ( KeyBindingManager.getKeyBindingManager().isValidCommand("forw", true ) ) {
			appliedForce.set(rotation.mult(-700)).multLocal(time);
			shipNode.addForce(appliedForce);
		}
		else if ( KeyBindingManager.getKeyBindingManager().isValidCommand("backw", true ) ) {
			appliedForce.set(rotation.mult(700)).multLocal(time);
			shipNode.addForce(appliedForce);
		}

	}
}
