refacto: moved systems from player controller physics process to their own signal based systems
This commit is contained in:
@ -18,37 +18,15 @@ public partial class PlayerController : CharacterBody3D
|
||||
public CapsuleCollider CapsuleCollider;
|
||||
public Gravity Gravity;
|
||||
public HealthSystem HealthSystem;
|
||||
|
||||
public MoveSystem MoveSystem;
|
||||
public TweenQueueSystem TweenQueueSystem;
|
||||
|
||||
[Export(PropertyHint.Range, "0,20,0.1,or_greater")]
|
||||
public float WalkSpeed { get; set; } = 5.0f;
|
||||
[Export(PropertyHint.Range, "0,20,0.1,or_greater")]
|
||||
public float SprintSpeed { get; set; } = 7.2f;
|
||||
[Export(PropertyHint.Range, "0,10,0.1,or_greater")]
|
||||
public float CrouchSpeed { get; set; } = 2.5f;
|
||||
[Export(PropertyHint.Range, "0,100,0.1,or_greater")]
|
||||
public float CrouchTransitionSpeed { get; set; } = 20.0f;
|
||||
|
||||
[Export(PropertyHint.Range, "0,5,0.1,or_greater")]
|
||||
public float DoubleJumpSpeedFactor { get; set; } = 2f;
|
||||
|
||||
private bool _canDoubleJump = true;
|
||||
private bool _movementEnabled = true;
|
||||
|
||||
private bool _isTweening = false;
|
||||
private record TweenInputs(Vector3 Location, float Duration);
|
||||
|
||||
private Queue<TweenInputs> _tweenInputs = new Queue<TweenInputs>();
|
||||
|
||||
private bool _shouldMantle = false;
|
||||
private Vector3 _dashLocation = Vector3.Zero;
|
||||
private Vector3 _mantleLocation = Vector3.Zero;
|
||||
|
||||
private float _currentSpeed;
|
||||
|
||||
private const float DecelerationSpeedFactorFloor = 15.0f;
|
||||
private const float DecelerationSpeedFactorAir = 7.0f;
|
||||
|
||||
private float _lastFrameWasOnFloor = -Mathf.Inf;
|
||||
|
||||
private const int NumOfHeadCollisionDetectors = 4;
|
||||
@ -91,12 +69,18 @@ public partial class PlayerController : CharacterBody3D
|
||||
GD.Print("Aim canceled");
|
||||
}
|
||||
|
||||
public void OnInputJumpPressed()
|
||||
{
|
||||
bool doesCapsuleHaveCrouchingHeight = CapsuleCollider.IsCrouchingHeight();
|
||||
bool isPlayerDead = HealthSystem.IsDead();
|
||||
|
||||
if (!doesCapsuleHaveCrouchingHeight && !isPlayerDead)
|
||||
MoveSystem.Jump(IsOnFloor());
|
||||
}
|
||||
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_currentSpeed = WalkSpeed;
|
||||
|
||||
HeadSystem = GetNode<HeadSystem>("HeadSystem");
|
||||
HeadSystem.Init();
|
||||
|
||||
@ -137,23 +121,31 @@ public partial class PlayerController : CharacterBody3D
|
||||
FieldOfView = GetNode<FieldOfView>("FieldOfView");
|
||||
FieldOfView.Init(camera);
|
||||
|
||||
Stamina = GetNode<Stamina>("Stamina");
|
||||
Stamina.SetSpeeds(WalkSpeed, SprintSpeed);
|
||||
|
||||
StairsSystem = GetNode<StairsSystem>("StairsSystem");
|
||||
StairsSystem.Init(stairsBelowRayCast3D, stairsAheadRayCast3D, cameraSmooth);
|
||||
|
||||
MantleSystem = GetNode<MantleSystem>("MantleSystem");
|
||||
MantleSystem.Init(HeadSystem);
|
||||
|
||||
DashSystem = GetNode<DashSystem>("DashSystem");
|
||||
DashSystem.Init(HeadSystem, camera);
|
||||
|
||||
CapsuleCollider = GetNode<CapsuleCollider>("CapsuleCollider");
|
||||
|
||||
Gravity = GetNode<Gravity>("Gravity");
|
||||
Gravity.Init(gravitySetting);
|
||||
|
||||
MantleSystem = GetNode<MantleSystem>("MantleSystem");
|
||||
MantleSystem.Init(HeadSystem);
|
||||
|
||||
TweenQueueSystem = GetNode<TweenQueueSystem>("TweenQueueSystem");
|
||||
TweenQueueSystem.Init(this);
|
||||
|
||||
MoveSystem = GetNode<MoveSystem>("MoveSystem");
|
||||
var moveSystemParams = new MoveSystem.MoveSystemParameters(this, Gravity, MantleSystem, TweenQueueSystem,
|
||||
HeadSystem, CapsuleCollider);
|
||||
MoveSystem.Init(moveSystemParams);
|
||||
|
||||
Stamina = GetNode<Stamina>("Stamina");
|
||||
Stamina.SetSpeeds(MoveSystem.WalkSpeed, MoveSystem.SprintSpeed);
|
||||
|
||||
StairsSystem = GetNode<StairsSystem>("StairsSystem");
|
||||
StairsSystem.Init(stairsBelowRayCast3D, stairsAheadRayCast3D, cameraSmooth);
|
||||
|
||||
DashSystem = GetNode<DashSystem>("DashSystem");
|
||||
DashSystem.Init(HeadSystem, camera);
|
||||
|
||||
HealthSystem = GetNode<HealthSystem>("HealthSystem");
|
||||
|
||||
HealthSystem.HealthSystemInitParams healthSystemParams = new HealthSystem.HealthSystemInitParams()
|
||||
@ -170,50 +162,9 @@ public partial class PlayerController : CharacterBody3D
|
||||
HealthSystem.Init(healthSystemParams);
|
||||
}
|
||||
|
||||
private void DisableMovement()
|
||||
{
|
||||
_movementEnabled = false;
|
||||
}
|
||||
public void EnableMovement()
|
||||
{
|
||||
_movementEnabled = true;
|
||||
}
|
||||
|
||||
public void EndTween()
|
||||
{
|
||||
EnableMovement();
|
||||
_isTweening = false;
|
||||
}
|
||||
|
||||
private void TweenToLocation(TweenInputs inputs)
|
||||
{
|
||||
var (location, duration) = inputs;
|
||||
|
||||
var tween = GetTree().CreateTween();
|
||||
var callback = new Callable(this, MethodName.EndTween);
|
||||
|
||||
tween.TweenProperty(this, "position", location, duration);
|
||||
tween.TweenCallback(callback);
|
||||
|
||||
DisableMovement();
|
||||
_isTweening = true;
|
||||
tween.Play();
|
||||
}
|
||||
|
||||
private void QueueTween(TweenInputs inputs)
|
||||
{
|
||||
_tweenInputs.Enqueue(inputs);
|
||||
}
|
||||
|
||||
private void QueueTween(Vector3 location, float duration)
|
||||
{
|
||||
QueueTween(new TweenInputs(location, duration));
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
if (_tweenInputs.Count > 0 && !_isTweening)
|
||||
TweenToLocation(_tweenInputs.Dequeue());
|
||||
TweenQueueSystem.ProcessTweens();
|
||||
|
||||
if (Input.IsActionPressed("aim_dash"))
|
||||
{
|
||||
@ -223,154 +174,20 @@ public partial class PlayerController : CharacterBody3D
|
||||
if (Input.IsActionJustReleased("aim_dash"))
|
||||
{
|
||||
DashSystem.Dash();
|
||||
QueueTween(_dashLocation, 0.1f);
|
||||
TweenQueueSystem.QueueTween(_dashLocation, 0.1f);
|
||||
if (_shouldMantle)
|
||||
{
|
||||
QueueTween(_mantleLocation, 0.1f);
|
||||
}
|
||||
}
|
||||
|
||||
var mantleLocationResult = MantleSystem.FindMantleInFrontOfPlayer();
|
||||
if (isOnFloorCustom())
|
||||
{
|
||||
_lastFrameWasOnFloor = Engine.GetPhysicsFrames();
|
||||
_canDoubleJump = true;
|
||||
}
|
||||
|
||||
// Adding the gravity
|
||||
if (!isOnFloorCustom())
|
||||
{
|
||||
Velocity = new Vector3(
|
||||
x: Velocity.X,
|
||||
y: Velocity.Y - (Gravity.CalculateGravityForce() * (float)delta),
|
||||
z: Velocity.Z);
|
||||
}
|
||||
|
||||
bool doesCapsuleHaveCrouchingHeight = CapsuleCollider.IsCrouchingHeight();
|
||||
|
||||
bool isPlayerDead = HealthSystem.IsDead();
|
||||
|
||||
// Handle Jump input
|
||||
if (Input.IsActionJustPressed("jump")
|
||||
&& !doesCapsuleHaveCrouchingHeight
|
||||
&& !isPlayerDead)
|
||||
{
|
||||
if (mantleLocationResult.IsSome(out var mantleLocation))
|
||||
{
|
||||
var duration = 0.1f * mantleLocation.DistanceTo(Position);
|
||||
QueueTween(mantleLocation, duration);
|
||||
}
|
||||
else if (isOnFloorCustom())
|
||||
{
|
||||
Velocity = new Vector3(
|
||||
x: Velocity.X,
|
||||
y: Gravity.CalculateJumpForce() * (float)delta,
|
||||
z: Velocity.Z);
|
||||
}
|
||||
else if (!isOnFloorCustom()
|
||||
&& _canDoubleJump)
|
||||
{
|
||||
_canDoubleJump = false;
|
||||
Velocity = new Vector3(
|
||||
x: Velocity.X,
|
||||
y: Gravity.CalculateJumpForce() * (float)delta * DoubleJumpSpeedFactor,
|
||||
z: Velocity.Z);
|
||||
}
|
||||
}
|
||||
|
||||
bool isHeadTouchingCeiling = IsHeadTouchingCeiling();
|
||||
bool doesCapsuleHaveDefaultHeight = CapsuleCollider.IsDefaultHeight();
|
||||
|
||||
// The code below is required to quickly adjust player's position on Y-axis when there's a ceiling on the
|
||||
// trajectory of player's jump and player is standing
|
||||
if (isHeadTouchingCeiling && doesCapsuleHaveDefaultHeight)
|
||||
{
|
||||
Velocity = new Vector3(
|
||||
x: Velocity.X,
|
||||
y: Velocity.Y - 2.0f,
|
||||
z: Velocity.Z);
|
||||
}
|
||||
|
||||
if (!isPlayerDead)
|
||||
{
|
||||
|
||||
// Used both for detecting the moment when we enter into crouching mode and the moment when we're already
|
||||
// in the crouching mode
|
||||
if (Input.IsActionPressed("crouch") ||
|
||||
(doesCapsuleHaveCrouchingHeight && isHeadTouchingCeiling))
|
||||
{
|
||||
CapsuleCollider.Crouch((float)delta, CrouchTransitionSpeed);
|
||||
_currentSpeed = CrouchSpeed;
|
||||
}
|
||||
// Used both for the moment when we exit the crouching mode and for the moment when we just walk
|
||||
else
|
||||
{
|
||||
CapsuleCollider.UndoCrouching((float)delta, CrouchTransitionSpeed);
|
||||
_currentSpeed = WalkSpeed;
|
||||
TweenQueueSystem.QueueTween(_mantleLocation, 0.1f);
|
||||
}
|
||||
}
|
||||
|
||||
// Each component of the boolean statement for sprinting is required
|
||||
if (Input.IsActionPressed("sprint") && !isHeadTouchingCeiling &&
|
||||
!doesCapsuleHaveCrouchingHeight && !isPlayerDead)
|
||||
{
|
||||
_currentSpeed = SprintSpeed;
|
||||
}
|
||||
|
||||
var isPlayerDead = HealthSystem.IsDead();
|
||||
var isHeadTouchingCeiling = IsHeadTouchingCeiling();
|
||||
|
||||
MoveSystem.MoveAround(delta, _inputMove, isOnFloorCustom(), isPlayerDead, isHeadTouchingCeiling);
|
||||
Vector2 inputLookDir = new Vector2(_inputRotateY, _inputRotateFloorplane);
|
||||
HeadSystem.LookAround(inputLookDir);
|
||||
|
||||
// Basis is a 3x4 matrix. It contains information about scaling and rotation of head.
|
||||
// By multiplying our Vector3 by this matrix we're doing multiple things:
|
||||
// a) We start to operate in global space;
|
||||
// b) We're applying to Vector3 the current rotation of "head" object;
|
||||
// c) We're applying to Vector3 the current scaling of "head" object;
|
||||
Vector3 direction = HeadSystem.Transform.Basis * _inputMove;
|
||||
|
||||
if (isPlayerDead)
|
||||
{
|
||||
direction = Vector3.Zero;
|
||||
}
|
||||
|
||||
if (isOnFloorCustom())
|
||||
{
|
||||
// Set velocity based on input direction when on the floor
|
||||
if (direction.Length() > 0)
|
||||
{
|
||||
float availableSpeed = Stamina.AccountStamina(delta, _currentSpeed);
|
||||
|
||||
float newX = direction.X * availableSpeed;
|
||||
float newZ = direction.Z * availableSpeed;
|
||||
|
||||
Velocity = new Vector3(newX, Velocity.Y, newZ);
|
||||
}
|
||||
// If there is no input, smoothly decelerate the character on the floor
|
||||
else
|
||||
{
|
||||
float xDeceleration = Mathf.Lerp(Velocity.X, direction.X * _currentSpeed,
|
||||
(float)delta * DecelerationSpeedFactorFloor);
|
||||
float zDeceleration = Mathf.Lerp(Velocity.Z, direction.Z * _currentSpeed,
|
||||
(float)delta * DecelerationSpeedFactorFloor);
|
||||
|
||||
Velocity = new Vector3(xDeceleration, Velocity.Y, zDeceleration);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
float xDeceleration = Mathf.Lerp(Velocity.X, direction.X * _currentSpeed,
|
||||
(float)delta * DecelerationSpeedFactorAir);
|
||||
float zDeceleration = Mathf.Lerp(Velocity.Z, direction.Z * _currentSpeed,
|
||||
(float)delta * DecelerationSpeedFactorAir);
|
||||
|
||||
Velocity = new Vector3(xDeceleration, Velocity.Y, zDeceleration);
|
||||
}
|
||||
|
||||
if (isPlayerDead)
|
||||
{
|
||||
MoveAndSlide();
|
||||
return;
|
||||
}
|
||||
|
||||
Bobbing.CameraBobbingParams cameraBobbingParams = new Bobbing.CameraBobbingParams
|
||||
{
|
||||
Delta = (float)delta,
|
||||
@ -384,7 +201,7 @@ public partial class PlayerController : CharacterBody3D
|
||||
{
|
||||
IsCrouchingHeight = CapsuleCollider.IsCrouchingHeight(),
|
||||
Delta = (float)delta,
|
||||
SprintSpeed = SprintSpeed,
|
||||
SprintSpeed = MoveSystem.SprintSpeed,
|
||||
Velocity = Velocity
|
||||
};
|
||||
|
||||
@ -394,7 +211,7 @@ public partial class PlayerController : CharacterBody3D
|
||||
{
|
||||
IsOnFloorCustom = isOnFloorCustom(),
|
||||
IsCapsuleHeightLessThanNormal = CapsuleCollider.IsCapsuleHeightLessThanNormal(),
|
||||
CurrentSpeedGreaterThanWalkSpeed = _currentSpeed > WalkSpeed,
|
||||
CurrentSpeedGreaterThanWalkSpeed = MoveSystem._currentSpeed > MoveSystem.WalkSpeed,
|
||||
IsCrouchingHeight = CapsuleCollider.IsCrouchingHeight(),
|
||||
Delta = (float)delta,
|
||||
FloorMaxAngle = FloorMaxAngle,
|
||||
@ -443,7 +260,7 @@ public partial class PlayerController : CharacterBody3D
|
||||
StairsSystem.SlideCameraParams slideCameraParams = new StairsSystem.SlideCameraParams
|
||||
{
|
||||
IsCapsuleHeightLessThanNormal = CapsuleCollider.IsCapsuleHeightLessThanNormal(),
|
||||
CurrentSpeedGreaterThanWalkSpeed = _currentSpeed > WalkSpeed,
|
||||
CurrentSpeedGreaterThanWalkSpeed = MoveSystem._currentSpeed > MoveSystem.WalkSpeed,
|
||||
BetweenCrouchingAndNormalHeight = CapsuleCollider.IsBetweenCrouchingAndNormalHeight(),
|
||||
Delta = (float)delta
|
||||
};
|
||||
|
Reference in New Issue
Block a user