Files
MovementTests/player_controller/Scripts/Mouse.cs
2025-05-27 19:20:46 +02:00

57 lines
1.5 KiB
C#

using System;
using Godot;
namespace PolarBears.PlayerControllerAddon;
public partial class Mouse : Node3D
{
[Export(PropertyHint.Range, "0,0.1,0.001,or_greater")]
public float Sensitivity { get; set; } = 0.004f;
private Node3D _head;
private Camera3D _camera;
public delegate bool IsDead();
private IsDead _isPlayerDead;
public void Init(Node3D head, Camera3D cam, IsDead isDeadFunc)
{
Input.SetMouseMode(Input.MouseModeEnum.Captured);
_head = head;
_camera = cam;
_isPlayerDead = isDeadFunc;
}
public void LookAround(Vector2 lookDir)
{
// Horizontal movement of head
float angleForHorizontalRotation = lookDir.X * Sensitivity;
_head.RotateY(angleForHorizontalRotation);
// Vertical movement of head
Vector3 currentCameraRotation = _camera.Rotation;
currentCameraRotation.X += Convert.ToSingle(lookDir.Y * Sensitivity);
currentCameraRotation.X = Mathf.Clamp(currentCameraRotation.X, Mathf.DegToRad(-90f), Mathf.DegToRad(90f));
_camera.Rotation = currentCameraRotation;
}
public override void _UnhandledInput(InputEvent @event)
{
if (_isPlayerDead())
{
return;
}
if (@event is InputEventMouseMotion eventMouseMotion)
{
var lookDir = new Vector2(-eventMouseMotion.Relative.X, -eventMouseMotion.Relative.Y);
LookAround(lookDir);
}
}
}