mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-07-04 04:31:16 +00:00
Better movement momentum
This commit is contained in:
parent
c68e40b8c3
commit
5c0e05021d
2 changed files with 48 additions and 26 deletions
|
|
@ -288,7 +288,7 @@ Acceleration = 150.0
|
||||||
Deceleration = 20.0
|
Deceleration = 20.0
|
||||||
Gravity = -20.0
|
Gravity = -20.0
|
||||||
FallSpeed = 4.0
|
FallSpeed = 4.0
|
||||||
TurnSpeed = 1.0
|
TurnResistance = 0.8000000000029104
|
||||||
|
|
||||||
[node name="Storage" type="Node" parent="." node_paths=PackedStringArray("Root", "Shield")]
|
[node name="Storage" type="Node" parent="." node_paths=PackedStringArray("Root", "Shield")]
|
||||||
script = ExtResource("6_habpy")
|
script = ExtResource("6_habpy")
|
||||||
|
|
|
||||||
|
|
@ -8,18 +8,20 @@ namespace Cirno.Scripts.Components.FSM._3DPlayer;
|
||||||
|
|
||||||
public partial class IsoMovementModule : ModuleBase<PlayerState, CharacterBody3D>
|
public partial class IsoMovementModule : ModuleBase<PlayerState, CharacterBody3D>
|
||||||
{
|
{
|
||||||
[Export] public IsoPlayerStorageModule PlayerStorage { get; private set; }
|
[Export] public IsoPlayerStorageModule PlayerStorage { get; private set; }
|
||||||
[Export] private InputProvider _inputProvider;
|
[Export] private InputProvider _inputProvider;
|
||||||
|
|
||||||
[Export] public PlayerHitboxSpriteProvider3D HitboxSpriteProvider { get; private set; }
|
[Export] public PlayerHitboxSpriteProvider3D HitboxSpriteProvider { get; private set; }
|
||||||
|
|
||||||
[Export] public int Speed { get; set; } = 45;
|
[Export] public int Speed { get; set; } = 45;
|
||||||
[Export] public int StrafeSpeed { get; set; } = 35;
|
[Export] public int StrafeSpeed { get; set; } = 35;
|
||||||
[Export] public float Acceleration = 8f;
|
[Export] public float Acceleration = 8f;
|
||||||
[Export] public float Deceleration = 8f;
|
[Export] public float Deceleration = 8f;
|
||||||
[Export] public float Gravity = -9.8f;
|
[Export] public float Gravity = -9.8f;
|
||||||
[Export] public float FallSpeed = 20f;
|
[Export] public float FallSpeed = 20f;
|
||||||
|
|
||||||
|
[Export] public float TurnResistance { get; set; } = 0.5f;
|
||||||
|
|
||||||
private bool _isStrafing;
|
private bool _isStrafing;
|
||||||
private float _accelerationPerSecond;
|
private float _accelerationPerSecond;
|
||||||
private float _decelerationPerSecond;
|
private float _decelerationPerSecond;
|
||||||
|
|
@ -28,7 +30,7 @@ public partial class IsoMovementModule : ModuleBase<PlayerState, CharacterBody3D
|
||||||
|
|
||||||
private IStateMachine<PlayerState, CharacterBody3D> _stateMachine;
|
private IStateMachine<PlayerState, CharacterBody3D> _stateMachine;
|
||||||
private CharacterBody3D MainObject => _stateMachine.MainObject;
|
private CharacterBody3D MainObject => _stateMachine.MainObject;
|
||||||
|
|
||||||
public override void EnterState(PlayerState state)
|
public override void EnterState(PlayerState state)
|
||||||
{
|
{
|
||||||
_accelerationPerSecond = Speed / Acceleration;
|
_accelerationPerSecond = Speed / Acceleration;
|
||||||
|
|
@ -37,7 +39,6 @@ public partial class IsoMovementModule : ModuleBase<PlayerState, CharacterBody3D
|
||||||
|
|
||||||
public override void ExitState(PlayerState state)
|
public override void ExitState(PlayerState state)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Init(IStateMachine<PlayerState, CharacterBody3D> machine)
|
public override void Init(IStateMachine<PlayerState, CharacterBody3D> machine)
|
||||||
|
|
@ -48,10 +49,10 @@ public partial class IsoMovementModule : ModuleBase<PlayerState, CharacterBody3D
|
||||||
public override void Process(double delta)
|
public override void Process(double delta)
|
||||||
{
|
{
|
||||||
var movementInput = _inputProvider.GetMovementInput();
|
var movementInput = _inputProvider.GetMovementInput();
|
||||||
|
|
||||||
_isStrafing = _inputProvider.GetStrafePressed();
|
_isStrafing = _inputProvider.GetStrafePressed();
|
||||||
var rightStickInput = _inputProvider.GetAimInput().Normalized();
|
var rightStickInput = _inputProvider.GetAimInput().Normalized();
|
||||||
|
|
||||||
// Update Facing Direction
|
// Update Facing Direction
|
||||||
// if (!_isStrafing)
|
// if (!_isStrafing)
|
||||||
// {
|
// {
|
||||||
|
|
@ -68,7 +69,7 @@ public partial class IsoMovementModule : ModuleBase<PlayerState, CharacterBody3D
|
||||||
// }
|
// }
|
||||||
|
|
||||||
var rotatedMovementDirection = movementInput.Rotated(Mathf.DegToRad(-45f));
|
var rotatedMovementDirection = movementInput.Rotated(Mathf.DegToRad(-45f));
|
||||||
|
|
||||||
//PlayerStorage.MovementDirection = new Vector3(rotatedMovementDirection.X, 0, rotatedMovementDirection.Y);
|
//PlayerStorage.MovementDirection = new Vector3(rotatedMovementDirection.X, 0, rotatedMovementDirection.Y);
|
||||||
PlayerStorage.MovementDirection = rotatedMovementDirection;
|
PlayerStorage.MovementDirection = rotatedMovementDirection;
|
||||||
|
|
||||||
|
|
@ -78,12 +79,42 @@ public partial class IsoMovementModule : ModuleBase<PlayerState, CharacterBody3D
|
||||||
private Vector2 InterpolateVelocityAxes(
|
private Vector2 InterpolateVelocityAxes(
|
||||||
Vector2 current, Vector2 target, float accel, float decel, float delta)
|
Vector2 current, Vector2 target, float accel, float decel, float delta)
|
||||||
{
|
{
|
||||||
// Accelerate when we're increasing magnitude toward target on that axis; otherwise decelerate.
|
float newX = current.X;
|
||||||
float stepX = (Mathf.Abs(target.X) > Mathf.Abs(current.X)) ? accel : decel;
|
float newY = current.Y;
|
||||||
float stepY = (Mathf.Abs(target.Y) > Mathf.Abs(current.Y)) ? accel : decel;
|
|
||||||
|
// --- X Axis ---
|
||||||
|
if (Mathf.Sign(target.X) == Mathf.Sign(current.X) || Mathf.IsZeroApprox(current.X))
|
||||||
|
{
|
||||||
|
// Same direction or stopped → normal accel/decel
|
||||||
|
float step = (Mathf.Abs(target.X) > Mathf.Abs(current.X)) ? accel : decel;
|
||||||
|
newX = Mathf.MoveToward(current.X, target.X, step * delta);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Opposite direction → apply resistance multiplier to deceleration
|
||||||
|
float slowStep = decel * TurnResistance;
|
||||||
|
newX = Mathf.MoveToward(current.X, 0, slowStep * delta);
|
||||||
|
|
||||||
|
// Only start accelerating toward target if we've nearly stopped
|
||||||
|
if (Mathf.IsZeroApprox(newX))
|
||||||
|
newX = Mathf.MoveToward(newX, target.X, accel * delta);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Y Axis ---
|
||||||
|
if (Mathf.Sign(target.Y) == Mathf.Sign(current.Y) || Mathf.IsZeroApprox(current.Y))
|
||||||
|
{
|
||||||
|
float step = (Mathf.Abs(target.Y) > Mathf.Abs(current.Y)) ? accel : decel;
|
||||||
|
newY = Mathf.MoveToward(current.Y, target.Y, step * delta);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
float slowStep = decel * TurnResistance;
|
||||||
|
newY = Mathf.MoveToward(current.Y, 0, slowStep * delta);
|
||||||
|
|
||||||
|
if (Mathf.IsZeroApprox(newY))
|
||||||
|
newY = Mathf.MoveToward(newY, target.Y, accel * delta);
|
||||||
|
}
|
||||||
|
|
||||||
float newX = Mathf.MoveToward(current.X, target.X, stepX * delta);
|
|
||||||
float newY = Mathf.MoveToward(current.Y, target.Y, stepY * delta);
|
|
||||||
return new Vector2(newX, newY);
|
return new Vector2(newX, newY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -93,29 +124,20 @@ public partial class IsoMovementModule : ModuleBase<PlayerState, CharacterBody3D
|
||||||
Vector2 v = v3.ToVector2();
|
Vector2 v = v3.ToVector2();
|
||||||
float dt = (float)delta;
|
float dt = (float)delta;
|
||||||
|
|
||||||
// Read desired input direction for this physics tick
|
|
||||||
Vector2 inputDir = PlayerStorage.MovementDirection;
|
Vector2 inputDir = PlayerStorage.MovementDirection;
|
||||||
|
if (inputDir.Length() > 1f)
|
||||||
// Clamp diagonal magnitude so diagonals aren't faster (helps remove jittery speed jumps)
|
inputDir = inputDir.Normalized();
|
||||||
float len = inputDir.Length();
|
|
||||||
if (len > 1f)
|
|
||||||
inputDir /= len;
|
|
||||||
|
|
||||||
if (_isStrafing)
|
if (_isStrafing)
|
||||||
{
|
{
|
||||||
// Strafing stays instant/responsive if you want that behavior
|
|
||||||
v = inputDir * StrafeSpeed;
|
v = inputDir * StrafeSpeed;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Build a target *velocity* (not just direction)
|
|
||||||
Vector2 targetVel = inputDir * Speed;
|
Vector2 targetVel = inputDir * Speed;
|
||||||
|
|
||||||
// Per-axis interpolation prevents direction snaps when keys change
|
|
||||||
v = InterpolateVelocityAxes(v, targetVel, Acceleration, Deceleration, dt);
|
v = InterpolateVelocityAxes(v, targetVel, Acceleration, Deceleration, dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Y (gravity)
|
|
||||||
float vy = Mathf.Clamp(v3.Y + Gravity * dt, -FallSpeed, FallSpeed);
|
float vy = Mathf.Clamp(v3.Y + Gravity * dt, -FallSpeed, FallSpeed);
|
||||||
if (Input.IsKeyLabelPressed(Key.Z)) vy = 10;
|
if (Input.IsKeyLabelPressed(Key.Z)) vy = 10;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue