/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package aspect.example;
import static aspect.core.AspectRenderer.*;
import aspect.entity.Entity;
import aspect.entity.behavior.Behavior;
import aspect.physics.Motion;
import aspect.physics.Time;
import aspect.util.Matrix4x4;
import aspect.util.Vector3;
import static org.lwjgl.input.Keyboard.*;
import org.lwjgl.input.Mouse;
import static org.lwjgl.input.Mouse.getX;
import static org.lwjgl.input.Mouse.getY;
import static org.lwjgl.input.Mouse.setCursorPosition;
import org.lwjgl.opengl.Display;
import org.lwjgl.util.glu.GLU;
/**
*
* @author vincent
*/
public class PlayerShip extends Entity {
private Matrix4x4 rot;
public float mouseSensitivity = 5.0f;
public PlayerShip() {
Mouse.setGrabbed(true);
Motion m = new Motion();
rot = Matrix4x4.identity();
addBehavior(m);
addBehavior(new ShipControl());
centerMouse();
}
public void cam() {
Vector3 up = rot.applyRotation(Vector3.yAxis());
Vector3 forward = rot.applyRotation(Vector3.zAxis().negate());
//GLU.gluLookAt(0, 0, 0, forward.x, forward.y, forward.z, up.x, up.y, up.z);
transform(rot.invert());
translate(pos.negate());
}
private class ShipControl extends Behavior {
@Override
public void update() {
Vector3 up = Vector3.yAxis();
Vector3 right = Vector3.xAxis();
Vector3 forward = Vector3.zAxis().negate();
Motion m = getBehavior(Motion.class);
float yaw = -getMouseDX() * mouseSensitivity;
float pitch = getMouseDY() * mouseSensitivity;
if (isKeyDown(KEY_UP)) {
pitch += 50;
}
if (isKeyDown(KEY_DOWN)) {
pitch -= 50;
}
if (isKeyDown(KEY_LEFT)) {
yaw += 50;
}
if (isKeyDown(KEY_RIGHT)) {
yaw -= 50;
}
yaw = Math.min(Math.abs(yaw), 120) * Math.signum(yaw);
pitch = Math.min(Math.abs(pitch), 120) * Math.signum(pitch);
rot = rot.rotate(up, yaw * Time.deltaTime());
rot = rot.rotate(right, pitch * Time.deltaTime());
if (isKeyDown(KEY_A)) {
rot = rot.rotate(forward, -90 * Time.deltaTime());
}
if (isKeyDown(KEY_D)) {
rot = rot.rotate(forward, 90 * Time.deltaTime());
}
float thrust = 00.0f;
if (isKeyDown(KEY_W)) {
thrust = 20.0f;
} else if (isKeyDown(KEY_S)) {
thrust = -20.0f;
}
m.velocity = rot.applyRotation(new Vector3(0, 0, -thrust));
centerMouse();
}
}
private int getMouseDX() {
return getX() - getCenterX();
}
private int getMouseDY() {
return getY() - getCenterY();
}
private int getCenterX() {
return Display.getWidth() / 2;
}
private int getCenterY() {
return Display.getHeight() / 2;
}
private void centerMouse() {
setCursorPosition(getCenterX(), getCenterY());
}
}