Files
LNXSDK/leenkx/Sources/leenkx/logicnode/MouseLookNode.hx

159 lines
4.3 KiB
Haxe

package leenkx.logicnode;
import iron.math.Vec4;
import iron.system.Input;
import iron.object.Object;
import kha.System;
import kha.FastFloat;
class MouseLookNode extends LogicNode {
public var property0: String;
public var property1: Bool;
public var property2: Bool;
public var property3: Bool;
public var property4: Bool;
public var property5: Bool;
public var property7: Bool;
var smoothX: Float = 0.0;
var smoothY: Float = 0.0;
var maxHorizontal: Float = Math.PI;
var maxVertical: Float = Math.PI / 2;
var currentHorizontal: Float = 0.0;
var currentVertical: Float = 0.0;
var baseResolutionWidth: Float = 1920.0;
static inline var BASE_SCALE: Float = 1500.0;
static var RADIAN_SCALING_FACTOR: Float = Math.PI * 50.0 / 180.0;
public function new(tree: LogicTree) {
super(tree);
}
override function run(from: Int) {
var bodyObject: Object = inputs[1].get();
var headObject: Object = inputs[2].get();
var sensitivity: FastFloat = inputs[3].get();
var smoothing: FastFloat = inputs[4].get();
if (bodyObject == null) {
runOutput(0);
return;
}
var mouse = Input.getMouse();
if (property1) {
if (mouse.started() && !mouse.locked) {
mouse.lock();
}
}
if (!mouse.locked && !mouse.down()) {
runOutput(0);
return;
}
var deltaX: Float = mouse.movementX;
var deltaY: Float = mouse.movementY;
if (property2) deltaX = -deltaX;
if (property3) deltaY = -deltaY;
// Always apply resolution-adaptive scaling
var resolutionMultiplier: Float = System.windowWidth() / baseResolutionWidth;
if (smoothing > 0.0) {
var smoothingFactor: Float = Math.min(smoothing, 0.99);
smoothX = smoothX * smoothingFactor + deltaX * (1.0 - smoothingFactor);
smoothY = smoothY * smoothingFactor + deltaY * (1.0 - smoothingFactor);
deltaX = smoothX;
deltaY = smoothY;
}
var horizontalAxis = new Vec4();
var verticalAxis = new Vec4();
switch (property0) {
case "X":
horizontalAxis.set(0, 0, 1);
verticalAxis.set(0, 1, 0);
case "Y":
#if lnx_yaxisup
horizontalAxis.set(0, 0, 1);
verticalAxis.set(1, 0, 0);
#else
horizontalAxis.set(0, 0, 1);
verticalAxis.set(1, 0, 0);
#end
case "Z":
horizontalAxis.set(0, 1, 0);
verticalAxis.set(1, 0, 0);
}
// Always apply resolution-adaptive scaling
var finalScale: Float = BASE_SCALE * resolutionMultiplier;
deltaX *= sensitivity;
deltaY *= sensitivity;
var horizontalRotation: Float = (-deltaX / finalScale) * RADIAN_SCALING_FACTOR;
var verticalRotation: Float = (-deltaY / finalScale) * RADIAN_SCALING_FACTOR;
if (property4) {
currentHorizontal += horizontalRotation;
if (currentHorizontal > maxHorizontal) {
horizontalRotation -= (currentHorizontal - maxHorizontal);
currentHorizontal = maxHorizontal;
} else if (currentHorizontal < -maxHorizontal) {
horizontalRotation -= (currentHorizontal + maxHorizontal);
currentHorizontal = -maxHorizontal;
}
}
if (property5) {
currentVertical += verticalRotation;
if (currentVertical > maxVertical) {
verticalRotation -= (currentVertical - maxVertical);
currentVertical = maxVertical;
} else if (currentVertical < -maxVertical) {
verticalRotation -= (currentVertical + maxVertical);
currentVertical = -maxVertical;
}
}
if (horizontalRotation != 0.0) {
bodyObject.transform.rotate(horizontalAxis, horizontalRotation);
#if lnx_physics
var rigidBody = bodyObject.getTrait(leenkx.trait.physics.RigidBody);
if (rigidBody != null) rigidBody.syncTransform();
#end
}
if (headObject != null && verticalRotation != 0.0) {
if (property7) {
// Local space rotation (recommended for FPS)
headObject.transform.rotate(verticalAxis, verticalRotation);
} else {
// World space rotation
var headVerticalAxis = headObject.transform.world.right();
headObject.transform.rotate(headVerticalAxis, verticalRotation);
}
#if lnx_physics
var headRigidBody = headObject.getTrait(leenkx.trait.physics.RigidBody);
if (headRigidBody != null) headRigidBody.syncTransform();
#end
} else if (headObject == null && verticalRotation != 0.0) {
bodyObject.transform.rotate(verticalAxis, verticalRotation);
#if lnx_physics
var rigidBody = bodyObject.getTrait(leenkx.trait.physics.RigidBody);
if (rigidBody != null) rigidBody.syncTransform();
#end
}
runOutput(0);
}
}