Strafing and crosshair for FSM player

This commit is contained in:
Marco 2025-03-01 10:05:13 +01:00
commit af0261ce8e
6 changed files with 170 additions and 23 deletions

View file

@ -0,0 +1,32 @@
using Godot;
namespace Cirno.Scripts.Components.Actors;
public partial class PlayerCrosshairProvider : Node2D
{
[Export]
public AnimatedSprite2D AnimatedSprite { get; private set; }
[Export]
public float CrosshairDistance { get; private set; }
public void UpdatePosition(Vector2 facingDirection)
{
AnimatedSprite.Position = CalculateCrosshairPosition(facingDirection);
}
private Vector2 CalculateCrosshairPosition(Vector2 facingDirection)
{
return facingDirection * CrosshairDistance;
}
public void Show()
{
AnimatedSprite.Show();
}
public void Hide()
{
AnimatedSprite.Hide();
}
}

View file

@ -0,0 +1 @@
uid://d208gvthkstvc

View file

@ -0,0 +1,35 @@
using Godot;
namespace Cirno.Scripts.Components.Actors;
public partial class PlayerHitboxSpriteProvider : Node2D
{
[Export]
public AnimatedSprite2D Hitbox { get; private set; }
[Export]
public AnimatedSprite2D Circle { get; private set; }
public void SetVisibility(bool isVisible)
{
if (isVisible)
{
Show();
}
else
{
Hide();
}
}
public void Show()
{
Hitbox.Show();
Circle.Show();
}
public void Hide()
{
Hitbox.Hide();
Circle.Hide();
}
}

View file

@ -0,0 +1 @@
uid://3jxrxcqp3ndr

View file

@ -7,24 +7,29 @@ namespace Cirno.Scripts.Components.FSM.Player;
public partial class Active : PlayerFSMState
{
private Vector2 _movementDirection { get; set; }
private Vector2 _facingDirection { get; set; }
[Export] public Sprite2D HitboxSprite { get; set; }
[Export]
private PlayerWeaponProvider _weaponProvider;
[Export]
private PlayerAnimationProvider _animationProvider;
[Export]
private InputProvider _inputProvider;
private bool _isStrafing { get; set; }
public Vector2 FacingDirection { get; private set; }
[Export]
public int Speed { get; set; } = 45;
[Export]
public int StrafeSpeed { get; set; } = 35;
[ExportCategory("Providers")]
[Export]
private PlayerWeaponProvider _weaponProvider;
[Export]
private PlayerAnimationProvider _animationProvider;
[Export]
private PlayerCrosshairProvider _crosshairProvider;
[Export] private PlayerHitboxSpriteProvider _hitboxSpriteProvider;
[Export]
private InputProvider _inputProvider;
private bool _isStrafing;
public int MovementSpeed => _isStrafing ? StrafeSpeed : Speed;
@ -42,12 +47,14 @@ public partial class Active : PlayerFSMState
{
// enable sprite
// enable crosshair
GD.Print(this.State.ToString());
_crosshairProvider.Show();
}
public override void ExitState()
{
_animationProvider.SetAnimation(Vector2.Zero);
_crosshairProvider.Hide();
_hitboxSpriteProvider.Hide();
}
public override void PhysicsProcessState(double delta)
@ -59,34 +66,31 @@ public partial class Active : PlayerFSMState
public override void ProcessState(double delta)
{
_movementDirection = _inputProvider.GetMovementInput();
_movementDirection = _inputProvider.GetMovementInput().Normalized();
_isStrafing = _inputProvider.GetStrafePressed();
// Toggle visibility of the hitbox sprite based on strafing
if (HitboxSprite != null)
{
HitboxSprite.Visible = _isStrafing;
}
var rightStickInput = _inputProvider.GetAimInput();
_hitboxSpriteProvider.SetVisibility(_isStrafing);
var rightStickInput = _inputProvider.GetAimInput().Normalized();
// Update Facing Direction
if (!_isStrafing)
{
if (rightStickInput.Length() > 0.1f) // If the right stick is moved
{
_facingDirection = rightStickInput.Normalized();
FacingDirection = rightStickInput.Normalized();
}
else if (_movementDirection != Vector2.Zero) // Fall back to movement direction
{
_facingDirection = _movementDirection;
FacingDirection = _movementDirection;
}
}
_animationProvider.SetAnimation(_stateMachine.Velocity);
_crosshairProvider.UpdatePosition(FacingDirection);
HandleShoot();
// FindInteractable();
@ -97,7 +101,7 @@ public partial class Active : PlayerFSMState
private void HandleShoot()
{
if (!_inputProvider.GetShootPressed()) return;
_weaponProvider.Shoot(this._facingDirection);
_weaponProvider.Shoot(this.FacingDirection);
}