Inventory management focused game written in Godot / C#
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.4 KiB

public partial class CameraController : Node
{
[Export] public Camera3D Camera { get; set; }
[Export] public Vector2 MouseSensitivity { get; set; } = new(0.2f, 0.2f); // Degrees per pixel.
[Export] public float PitchMinimum { get; set; } = -85;
[Export] public float PitchMaximum { get; set; } = 85;
public float CurrentPitch { get; set; }
public float CurrentYaw { get; set; }
public static bool IsMouseCaptured
=> Input.MouseMode == Input.MouseModeEnum.Captured;
public override void _UnhandledInput(InputEvent @event)
{
if (@event.IsActionPressed("ui_cancel") && IsMouseCaptured) {
// When escape is pressed, release the mouse.
Input.MouseMode = Input.MouseModeEnum.Visible;
GetViewport().SetInputAsHandled();
} else switch (@event) {
case InputEventMouseButton { ButtonIndex: MouseButton.Left, Pressed: true } when !IsMouseCaptured:
// When left mouse button is pressed, capture the mouse.
Input.MouseMode = Input.MouseModeEnum.Captured;
GetViewport().SetInputAsHandled();
break;
case InputEventMouseMotion motion when IsMouseCaptured:
ApplyRotation(-motion.Relative * MouseSensitivity);
break;
}
}
void ApplyRotation(Vector2 delta)
{
delta *= Tau / 360; // degrees to radians
CurrentYaw += delta.X;
CurrentPitch += delta.Y;
CurrentPitch = Clamp(CurrentPitch, DegToRad(PitchMinimum), DegToRad(PitchMaximum));
}
}