48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
using Godot;
|
|
|
|
using static FactoryGame.Utils.Constants.PropertyHints;
|
|
|
|
public partial class CameraController : Camera3D {
|
|
[Export(PropertyHint.Range, PrimaRange)]
|
|
public float MouseSensitivity { get; set; } = 0.5f;
|
|
[Export(PropertyHint.Range, PrimaRange)]
|
|
public float JoystickSensitivity { get; set; } = 0.5f;
|
|
[Export(PropertyHint.Range, PrimaRange)]
|
|
public float DeadZone { get; set; } = 0.1f;
|
|
|
|
private float _lastX = 0f;
|
|
private float _lastY = 0f;
|
|
|
|
private Node3D? _parent;
|
|
|
|
public override void _Ready() {
|
|
_parent = GetParent<Node3D>();
|
|
Input.MouseMode = Input.MouseModeEnum.Captured;
|
|
}
|
|
|
|
public override void _Process(double delta) {
|
|
var input = Input.GetVector("camera_left", "camera_right", "camera_up", "camera_down", DeadZone);
|
|
RotateCamera(input * -(JoystickSensitivity / 10f));
|
|
|
|
base._Process(delta);
|
|
}
|
|
|
|
public override void _Input(InputEvent @event) {
|
|
if (@event is InputEventMouseMotion mouseMotion)
|
|
RotateCamera(new Vector2(mouseMotion.Relative.X, mouseMotion.Relative.Y) * -(MouseSensitivity / 100f));
|
|
|
|
base._Input(@event);
|
|
}
|
|
|
|
private void RotateCamera(Vector2 direction) {
|
|
if (direction == Vector2.Zero) return;
|
|
|
|
_parent?.RotateObjectLocal(Vector3.Up, direction.X);
|
|
//_parent!.RotateY(direction.X);
|
|
|
|
var rotationX = Rotation.X + direction.Y;
|
|
rotationX = Mathf.Clamp(rotationX, Mathf.DegToRad(-90), Mathf.DegToRad(90));
|
|
Rotation = new Vector3(rotationX, Rotation.Y, Rotation.Z);
|
|
}
|
|
}
|