feat: implement first-person controller

This commit is contained in:
2026-08-22 15:58:21 +02:00
parent 470e70d51b
commit c95233b9f9
6 changed files with 159 additions and 19 deletions
+56 -3
View File
@@ -1,10 +1,63 @@
using Game.Features.Player.Domain;
using System.Linq;
using Game.Features.Players.Domain;
using Game.Godot.Extensions;
using Game.Godot.Mappings;
using Godot;
using System;
namespace Game.Features.Player.Godot;
namespace Game.Features.Players.Godot;
public partial class PlayerController : Node3D
{
[Export] public CharacterBody3D Body;
[Export] public Camera3D Camera;
[Export] public float MoveSpeed = 4.0f;
[Export] public float JumpVelocity = 4.5f;
[Export] public float MouseSensitivity = 0.003f;
[Export] public float MaxLookAngleDegrees = 89.0f;
private PlayerMovement _movement;
private PlayerLook _look;
private static bool IsMouseCaptured() => Input.MouseMode == Input.MouseModeEnum.Captured;
public override void _Ready()
{
this.AssertRequiredExports();
float gravity = (float)ProjectSettings.GetSetting(ProjectSetting.Gravity.SettingPath());
_movement = new PlayerMovement(MoveSpeed, JumpVelocity, gravity);
_look = new PlayerLook(MouseSensitivity, MaxLookAngleDegrees);
}
public override void _UnhandledInput(InputEvent @event)
{
if (@event is not InputEventMouseMotion mouseMotion || !IsMouseCaptured()) return;
Body.RotateY(_look.CalculateYawDelta(mouseMotion.Relative.X));
_look.UpdatePitch(mouseMotion.Relative.Y);
Camera.Rotation = new Vector3(_look.Pitch, 0f, 0f);
}
public override void _PhysicsProcess(double delta)
{
Vector2 inputDirection = Input.GetVector(
InputAction.MoveLeft.ActionName(),
InputAction.MoveRight.ActionName(),
InputAction.MoveForward.ActionName(),
InputAction.MoveBack.ActionName()
);
MovementInput input = new(
Right: Body.Transform.Basis.X,
Back: Body.Transform.Basis.Z,
Direction: inputDirection,
IsOnFloor: Body.IsOnFloor(),
JumpPressed: Input.IsActionJustPressed(InputAction.Jump.ActionName()),
Delta: (float)delta
);
Body.Velocity = _movement.CalculateVelocity(Body.Velocity, input);
Body.MoveAndSlide();
}
}