armory/Sources/armory/trait/ThirdPersonController.hx

84 lines
1.9 KiB
Haxe
Raw Normal View History

2016-10-02 19:52:40 +02:00
package armory.trait;
import iron.math.Vec4;
import iron.system.Input;
import armory.trait.internal.PhysicsWorld;
import armory.trait.internal.CameraController;
2016-10-12 17:52:27 +02:00
@:keep
2016-10-02 19:52:40 +02:00
class ThirdPersonController extends CameraController {
2016-10-12 18:26:56 +02:00
#if (!arm_physics)
2016-10-02 19:52:40 +02:00
public function new() { super(); }
#else
static inline var rotationSpeed = 1.0;
public function new() {
super();
Scene.active.notifyOnInit(init);
}
function init() {
PhysicsWorld.active.notifyOnPreUpdate(preUpdate);
notifyOnUpdate(update);
notifyOnRemove(removed);
}
var xVec = Vec4.xAxis();
var zVec = Vec4.zAxis();
function preUpdate() {
if (Input.occupied || !body.bodyReady) return;
if (Input.touch) {
// kha.SystemImpl.lockMouse();
camera.transform.rotate(xVec, Input.deltaY / 250 * rotationSpeed);
transform.rotate(zVec, -Input.deltaX / 250 * rotationSpeed);
2016-10-09 16:06:18 +02:00
camera.buildMatrix();
2016-10-02 19:52:40 +02:00
body.syncTransform();
}
}
function removed() {
PhysicsWorld.active.removePreUpdate(preUpdate);
}
var dir = new Vec4();
function update() {
if (!body.bodyReady) return;
if (jump) {
body.applyImpulse(new Vec4(0, 0, 20));
jump = false;
}
// Move
dir.set(0, 0, 0);
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();
body.setLinearVelocity(0.0, 0.0, btvec.z() - 1.0);
2016-10-09 16:06:18 +02:00
var arm = object.getChild("Ballie");
arm.animation.player.paused = true;
2016-10-02 19:52:40 +02:00
if (moveForward || moveBackward || moveLeft || moveRight) {
2016-10-09 16:06:18 +02:00
arm.animation.player.paused = false;
arm.animation.player.dir = moveBackward ? -1 : 1;
dir.mult(-4 * 0.7);
2016-10-02 19:52:40 +02:00
body.activate();
body.setLinearVelocity(dir.x, dir.y, btvec.z() - 1.0);
}
// Keep vertical
body.setAngularFactor(0, 0, 0);
2016-10-09 16:06:18 +02:00
camera.buildMatrix();
2016-10-02 19:52:40 +02:00
}
#end
}