3D_RPG_Action_Game/Objects/Player/CameraController.cs

56 lines
2.1 KiB
C#

using Godot;
namespace DRPGActionGame.Player;
public partial class CameraController : Node3D {
[Export] private float sensitivity = 5f;
[Export] private SpringArm3D? springArm3D;
[Export] private Player? player;
private const float maxXRot = .25f;
private const float maxZoom = 2f;
private const float minZoom = 10f;
public override void _Ready() {
Input.MouseMode = Input.MouseModeEnum.Captured;
if (springArm3D is null) GD.PrintErr("No SprinArm3D found!");
if (player is null) GD.PrintErr("No Player found!");
}
// public override void _Process(double delta) => GlobalPosition = GetParent<Node3D>().GlobalPosition;
public override void _Input(InputEvent @event) {
if (springArm3D != null) {
springArm3D.SpringLength += @event.GetActionStrength("ZoomOut") - @event.GetActionStrength("ZoomIn");
float viewLeftRight = @event.GetActionStrength("ViewLeft") - @event.GetActionStrength("ViewRight");
float viewUpDown = @event.GetActionStrength("ViewUp") - @event.GetActionStrength("ViewDown");
float xRot = Mathf.Clamp(Rotation.X - viewLeftRight / 1000 * sensitivity, -maxXRot, +maxXRot * 2);
float yRot = Rotation.Y - viewUpDown / 1000 * sensitivity;
GD.Print($"{viewLeftRight}:{viewUpDown} -> {xRot}:{yRot}");
// Rotation = new(xRot, yRot, 0);
}
switch (@event) {
case InputEventMouseMotion inputEventMouseMotion: {
float xRot = Mathf.Clamp(Rotation.X - inputEventMouseMotion.Relative.Y / 1000 * sensitivity, -maxXRot, +maxXRot * 2);
float yRot = Rotation.Y - inputEventMouseMotion.Relative.X / 1000 * sensitivity;
Rotation = new(xRot, yRot, 0);
break;
}
case InputEventMouseButton inputEventMouseButton: {
if (inputEventMouseButton.ButtonIndex == MouseButton.WheelDown)
if (springArm3D?.SpringLength < minZoom) springArm3D.SpringLength += 0.1f;
else if (inputEventMouseButton.ButtonIndex == MouseButton.WheelUp)
if (springArm3D?.SpringLength > maxZoom)
springArm3D.SpringLength -= 0.1f;
break;
}
case InputEventKey inputEventKey: {
if (inputEventKey.Keycode == Key.Escape)
Input.MouseMode = Input.MouseModeEnum.Visible;
break;
}
}
}
}