2016-07-10 00:51:39 +02:00
|
|
|
package armory.trait;
|
2016-03-28 23:02:42 +02:00
|
|
|
|
2016-07-10 00:51:39 +02:00
|
|
|
import iron.math.Vec4;
|
2016-09-23 00:34:42 +02:00
|
|
|
import iron.system.Input;
|
2016-10-02 19:52:40 +02:00
|
|
|
import iron.object.Object;
|
2017-11-28 18:00:23 +01:00
|
|
|
import iron.object.CameraObject;
|
2017-09-30 00:32:06 +02:00
|
|
|
import armory.trait.physics.PhysicsWorld;
|
2016-10-02 19:52:40 +02:00
|
|
|
import armory.trait.internal.CameraController;
|
2016-03-28 23:02:42 +02:00
|
|
|
|
2016-10-02 19:52:40 +02:00
|
|
|
class FirstPersonController extends CameraController {
|
2016-03-28 23:02:42 +02:00
|
|
|
|
2018-09-11 15:11:46 +02:00
|
|
|
#if (!arm_physics)
|
2016-04-12 14:44:21 +02:00
|
|
|
public function new() { super(); }
|
|
|
|
#else
|
|
|
|
|
2019-12-19 23:54:08 +01:00
|
|
|
var head: Object;
|
|
|
|
static inline var rotationSpeed = 2.0;
|
2016-03-28 23:02:42 +02:00
|
|
|
|
2016-10-02 19:52:40 +02:00
|
|
|
public function new() {
|
|
|
|
super();
|
2016-03-28 23:02:42 +02:00
|
|
|
|
2018-07-24 21:28:03 +02:00
|
|
|
iron.Scene.active.notifyOnInit(init);
|
2016-10-02 19:52:40 +02:00
|
|
|
}
|
2019-12-19 23:54:08 +01:00
|
|
|
|
2016-03-28 23:02:42 +02:00
|
|
|
function init() {
|
2017-11-28 18:00:23 +01:00
|
|
|
head = object.getChildOfType(CameraObject);
|
2016-10-02 19:52:40 +02:00
|
|
|
|
|
|
|
PhysicsWorld.active.notifyOnPreUpdate(preUpdate);
|
|
|
|
notifyOnUpdate(update);
|
|
|
|
notifyOnRemove(removed);
|
2016-03-28 23:02:42 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 19:52:40 +02:00
|
|
|
var xVec = Vec4.xAxis();
|
|
|
|
var zVec = Vec4.zAxis();
|
|
|
|
function preUpdate() {
|
2016-12-07 02:01:42 +01:00
|
|
|
if (Input.occupied || !body.ready) return;
|
2019-12-19 23:54:08 +01:00
|
|
|
|
2017-04-10 21:17:17 +02:00
|
|
|
var mouse = Input.getMouse();
|
2017-12-14 18:12:01 +01:00
|
|
|
var kb = Input.getKeyboard();
|
2018-01-30 13:16:32 +01:00
|
|
|
|
2017-12-14 18:12:01 +01:00
|
|
|
if (mouse.started() && !mouse.locked) mouse.lock();
|
2018-06-28 16:26:15 +02:00
|
|
|
else if (kb.started("escape") && mouse.locked) mouse.unlock();
|
2019-12-19 23:54:08 +01:00
|
|
|
|
2018-01-30 13:16:32 +01:00
|
|
|
if (mouse.locked || mouse.down()) {
|
2017-04-10 21:17:17 +02:00
|
|
|
head.transform.rotate(xVec, -mouse.movementY / 250 * rotationSpeed);
|
|
|
|
transform.rotate(zVec, -mouse.movementX / 250 * rotationSpeed);
|
2016-10-02 19:52:40 +02:00
|
|
|
body.syncTransform();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function removed() {
|
|
|
|
PhysicsWorld.active.removePreUpdate(preUpdate);
|
|
|
|
}
|
2016-03-28 23:02:42 +02:00
|
|
|
|
2016-10-02 19:52:40 +02:00
|
|
|
var dir = new Vec4();
|
|
|
|
function update() {
|
2016-12-07 02:01:42 +01:00
|
|
|
if (!body.ready) return;
|
2016-10-02 19:52:40 +02:00
|
|
|
|
|
|
|
if (jump) {
|
2016-10-09 16:06:18 +02:00
|
|
|
body.applyImpulse(new Vec4(0, 0, 16));
|
2016-10-02 19:52:40 +02:00
|
|
|
jump = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move
|
2016-09-29 22:49:22 +02:00
|
|
|
dir.set(0, 0, 0);
|
2016-10-02 19:52:40 +02:00
|
|
|
if (moveForward) dir.add(transform.look());
|
|
|
|
if (moveBackward) dir.add(transform.look().mult(-1));
|
|
|
|
if (moveLeft) dir.add(transform.right().mult(-1));
|
|
|
|
if (moveRight) dir.add(transform.right());
|
|
|
|
|
|
|
|
// Push down
|
|
|
|
var btvec = body.getLinearVelocity();
|
2018-04-14 20:00:40 +02:00
|
|
|
body.setLinearVelocity(0.0, 0.0, btvec.z - 1.0);
|
2016-10-02 19:52:40 +02:00
|
|
|
|
2019-12-19 23:54:08 +01:00
|
|
|
if (moveForward || moveBackward || moveLeft || moveRight) {
|
2016-10-09 16:06:18 +02:00
|
|
|
dir.mult(6);
|
2016-03-28 23:02:42 +02:00
|
|
|
body.activate();
|
2018-04-14 20:00:40 +02:00
|
|
|
body.setLinearVelocity(dir.x, dir.y, btvec.z - 1.0);
|
2016-03-28 23:02:42 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 19:52:40 +02:00
|
|
|
// Keep vertical
|
2016-03-28 23:02:42 +02:00
|
|
|
body.setAngularFactor(0, 0, 0);
|
2016-10-09 16:06:18 +02:00
|
|
|
camera.buildMatrix();
|
2016-10-02 19:52:40 +02:00
|
|
|
}
|
2016-04-12 14:44:21 +02:00
|
|
|
#end
|
2016-03-28 23:02:42 +02:00
|
|
|
}
|