cirnogodot/Scripts/Components/FSM/Player/Active.cs

104 lines
2.6 KiB
C#
Raw Normal View History

2025-02-28 13:50:52 +01:00
using System;
2025-02-28 19:59:36 +01:00
using Cirno.Scripts.Components.Actors;
2025-02-28 13:50:52 +01:00
using Godot;
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; }
2025-02-28 22:49:55 +01:00
[Export]
2025-02-28 19:59:36 +01:00
private PlayerWeaponProvider _weaponProvider;
2025-02-28 22:49:55 +01:00
[Export]
private PlayerAnimationProvider _animationProvider;
[Export]
private InputProvider _inputProvider;
2025-02-28 19:59:36 +01:00
2025-02-28 13:50:52 +01:00
private bool _isStrafing { get; set; }
2025-02-28 19:59:36 +01:00
[Export]
public int Speed { get; set; } = 45;
[Export]
public int StrafeSpeed { get; set; } = 35;
public int MovementSpeed => _isStrafing ? StrafeSpeed : Speed;
public override void Init(ActorStateMachine stateMachine)
{
base.Init(stateMachine);
2025-02-28 22:49:55 +01:00
//_weaponProvider = stateMachine.GetNode<PlayerWeaponProvider>("WeaponProvider");
//_animationProvider = stateMachine.GetNode<PlayerAnimationProvider>("AnimationProvider");
2025-02-28 19:59:36 +01:00
_weaponProvider.Init(stateMachine);
}
2025-02-28 13:50:52 +01:00
public override void EnterState()
{
// enable sprite
// enable crosshair
2025-02-28 19:59:36 +01:00
GD.Print(this.State.ToString());
2025-02-28 13:50:52 +01:00
}
public override void ExitState()
{
2025-02-28 22:49:55 +01:00
_animationProvider.SetAnimation(Vector2.Zero);
2025-02-28 13:50:52 +01:00
}
public override void PhysicsProcessState(double delta)
{
2025-02-28 19:59:36 +01:00
_stateMachine.Velocity = _movementDirection * MovementSpeed;
2025-02-28 13:50:52 +01:00
2025-02-28 19:59:36 +01:00
_stateMachine.MoveAndSlide();
2025-02-28 13:50:52 +01:00
}
public override void ProcessState(double delta)
{
2025-02-28 22:49:55 +01:00
2025-02-28 13:50:52 +01:00
2025-02-28 22:49:55 +01:00
_movementDirection = _inputProvider.GetMovementInput();
_isStrafing = _inputProvider.GetStrafePressed();
2025-02-28 13:50:52 +01:00
// Toggle visibility of the hitbox sprite based on strafing
if (HitboxSprite != null)
{
HitboxSprite.Visible = _isStrafing;
}
2025-02-28 22:49:55 +01:00
var rightStickInput = _inputProvider.GetAimInput();
2025-02-28 13:50:52 +01:00
// Update Facing Direction
if (!_isStrafing)
{
if (rightStickInput.Length() > 0.1f) // If the right stick is moved
{
_facingDirection = rightStickInput.Normalized();
}
else if (_movementDirection != Vector2.Zero) // Fall back to movement direction
{
_facingDirection = _movementDirection;
}
}
2025-02-28 22:49:55 +01:00
_animationProvider.SetAnimation(_stateMachine.Velocity);
2025-02-28 19:59:36 +01:00
HandleShoot();
2025-02-28 13:50:52 +01:00
// FindInteractable();
// _crosshair.Position = CalculateCrosshairPosition();
}
2025-02-28 19:59:36 +01:00
private void HandleShoot()
{
2025-02-28 22:49:55 +01:00
if (!_inputProvider.GetShootPressed()) return;
2025-02-28 19:59:36 +01:00
_weaponProvider.Shoot(this._facingDirection);
}
2025-02-28 13:50:52 +01:00
}