feat: split player movement into ground and noclip strategies
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using Game.Features.Players.Contracts;
|
||||
using Game.Features.Players.Domain;
|
||||
using Game.Features.Players.Domain.Movement;
|
||||
using Game.Godot;
|
||||
using Game.Godot.Extensions;
|
||||
using Game.Godot.Mappings;
|
||||
@@ -7,26 +9,44 @@ using Godot;
|
||||
|
||||
namespace Game.Features.Players.Godot;
|
||||
|
||||
public partial class PlayerController : Node3D
|
||||
public partial class PlayerController : Node3D, IMovementModeSwitcher
|
||||
{
|
||||
[Export] public CharacterBody3D Body;
|
||||
[Export] public CollisionShape3D Collision;
|
||||
[Export] public Camera3D Camera;
|
||||
|
||||
[Export] public float MoveSpeed = 4.0f;
|
||||
[Export] public float JumpVelocity = 4.5f;
|
||||
[Export] public float NoclipFlySpeed = 8.0f;
|
||||
[Export] public float MouseSensitivity = 0.003f;
|
||||
[Export] public float MaxLookAngleDegrees = 89.0f;
|
||||
|
||||
private PlayerMovement _movement;
|
||||
private Dictionary<MovementMode, IMovementStrategy> _movementStrategies;
|
||||
private IMovementStrategy _currentMovementStrategy;
|
||||
private PlayerLook _look;
|
||||
|
||||
public MovementMode ActiveMovementMode { get; private set; } = MovementMode.Ground;
|
||||
|
||||
public void SetMovementMode(MovementMode movementMode)
|
||||
{
|
||||
ActiveMovementMode = movementMode;
|
||||
_currentMovementStrategy = _movementStrategies[movementMode];
|
||||
Collision.Disabled = movementMode == MovementMode.Noclip;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
float gravity = (float)ProjectSettings.GetSetting(ProjectSetting.Gravity.SettingPath());
|
||||
_movementStrategies = new Dictionary<MovementMode, IMovementStrategy>
|
||||
{
|
||||
[MovementMode.Ground] = new GroundMovement(MoveSpeed, JumpVelocity, gravity),
|
||||
[MovementMode.Noclip] = new NoclipMovement(NoclipFlySpeed)
|
||||
};
|
||||
_currentMovementStrategy = _movementStrategies[ActiveMovementMode];
|
||||
}
|
||||
|
||||
public override void _UnhandledInput(InputEvent @event)
|
||||
@@ -53,10 +73,12 @@ public partial class PlayerController : Node3D
|
||||
Direction: inputDirection,
|
||||
IsOnFloor: Body.IsOnFloor(),
|
||||
JumpPressed: Input.IsActionJustPressed(InputAction.Jump.ActionName()),
|
||||
AscendPressed: Input.IsActionPressed(InputAction.Jump.ActionName()),
|
||||
DescendPressed: Input.IsActionPressed(InputAction.MoveDown.ActionName()),
|
||||
Delta: (float)delta
|
||||
);
|
||||
|
||||
Body.Velocity = _movement.CalculateVelocity(Body.Velocity, input);
|
||||
Body.Velocity = _currentMovementStrategy.CalculateVelocity(Body.Velocity, input);
|
||||
Body.MoveAndSlide();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user