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

243 lines
7.4 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;
using Cirno.Scripts.Utils;
2025-02-28 13:50:52 +01:00
using Godot;
namespace Cirno.Scripts.Components.FSM.Player;
2025-03-05 10:55:14 +01:00
public partial class Active : PlayerStateBase
2025-02-28 13:50:52 +01:00
{
2025-03-05 10:55:14 +01:00
public override PlayerState StateId => PlayerState.Active;
2025-02-28 13:50:52 +01:00
private Vector2 _movementDirection { get; set; }
2025-05-05 13:59:12 +02:00
2025-03-15 17:56:55 +01:00
public Vector2 FacingDirection
{
get => _storageModule.FacingDirection;
private set => _storageModule.FacingDirection = value;
}
2025-02-28 13:50:52 +01:00
2025-05-05 13:59:12 +02:00
[Export] public int Speed { get; set; } = 45;
[Export] public int StrafeSpeed { get; set; } = 35;
[Export] public float Acceleration = 8f;
[Export] public float Deceleration = 8f;
// Providers previously exported on the state — resolve them from the actor via the state machine
private PlayerWeaponProvider _weaponProvider;
private PlayerAnimationProvider _animationProvider;
private PlayerCrosshairProvider _crosshairProvider;
private PlayerHitboxSpriteProvider _hitboxSprite_provider;
2025-02-28 22:49:55 +01:00
private InputProvider _inputProvider;
2025-02-28 19:59:36 +01:00
private PlayerDamageReceiver _damageReceiver;
private ActivationProvider _activationProvider;
2025-02-28 13:50:52 +01:00
private InteractionController _interaction_controller;
2025-05-05 13:59:12 +02:00
private PlayerStorageModule _storageModule;
2025-03-03 10:58:20 +01:00
2025-03-01 14:08:31 +01:00
private bool _isStrafing;
2025-05-05 13:59:12 +02:00
2025-02-28 19:59:36 +01:00
public int MovementSpeed => _isStrafing ? StrafeSpeed : Speed;
2025-03-05 10:55:14 +01:00
private CharacterBody2D _player;
2025-05-05 13:59:12 +02:00
2025-03-01 20:50:47 +01:00
private Hud _hud;
2025-03-01 14:08:31 +01:00
2025-03-05 10:55:14 +01:00
public override void Init(IStateMachine<PlayerState, CharacterBody2D> machine)
2025-02-28 19:59:36 +01:00
{
2025-03-05 10:55:14 +01:00
base.Init(machine);
2025-02-28 19:59:36 +01:00
2025-03-01 20:50:47 +01:00
_hud = Hud.Instance;
// Resolve modules from the actor attached to this state machine
_weaponProvider ??= StateMachine.GetModule<PlayerWeaponProvider>();
_animationProvider ??= StateMachine.GetModule<PlayerAnimationProvider>();
_crosshairProvider ??= StateMachine.GetModule<PlayerCrosshairProvider>();
_hitboxSprite_provider ??= StateMachine.GetModule<PlayerHitboxSpriteProvider>();
2025-03-01 14:08:31 +01:00
_inputProvider ??= StateMachine.GetModule<InputProvider>();
_damageReceiver ??= StateMachine.GetModule<PlayerDamageReceiver>();
_activationProvider ??= StateMachine.GetModule<ActivationProvider>();
_interaction_controller ??= StateMachine.GetModule<InteractionController>();
_storageModule ??= StateMachine.GetModule<PlayerStorageModule>();
2025-03-01 14:08:31 +01:00
if (_damageReceiver != null)
2025-03-01 14:08:31 +01:00
{
_damageReceiver.Death += () => { ChangeState(PlayerState.Dead); };
2025-05-05 13:59:12 +02:00
_damageReceiver.HealthDecreased += (_, _, _) =>
{
_animationProvider?.Blink();
//_hud.UpdateHealth(value, maxValue);
};
_damageReceiver.ShieldDecreased += (_, _, _) =>
{
_animationProvider?.PlayShieldAnimation();
//_hud.UpdateShield(value, maxValue);
};
2025-05-05 13:59:12 +02:00
_damageReceiver.Init(StateMachine);
2025-05-05 13:59:12 +02:00
_damageReceiver.RefillHealth();
_damageReceiver.RefillShield();
}
2025-05-05 13:59:12 +02:00
_activationProvider?.Init(MainObject);
2025-02-28 19:59:36 +01:00
_weaponProvider?.Init(MainObject);
2025-02-28 19:59:36 +01:00
}
2025-02-28 13:50:52 +01:00
public override void EnterState()
{
2025-03-08 11:33:26 +01:00
base.EnterState();
2025-02-28 13:50:52 +01:00
// enable sprite
// enable crosshair
_crosshairProvider?.Show();
_animationProvider?.ShowSprite();
if (_damageReceiver != null) _damageReceiver.Enabled = true;
if (_activationProvider != null) _activationProvider.Enabled = true;
if (_interaction_controller != null) _interaction_controller.Enabled = true;
2025-05-05 13:59:12 +02:00
_accelerationPerSecond = Speed / Acceleration;
_decelerationPerSecond = Speed / Deceleration;
2025-02-28 13:50:52 +01:00
}
public override void ExitState()
{
2025-03-08 11:33:26 +01:00
base.ExitState();
_animationProvider?.SetAnimationSpeed(Vector2.Zero);
//_animation_provider.SetAnimation(Vector2.Zero);
_crosshairProvider?.Hide();
_hitboxSprite_provider?.Hide();
if (_damageReceiver != null) _damageReceiver.Enabled = false;
if (_activationProvider != null) _activationProvider.Enabled = false;
if (_interaction_controller != null) _interaction_controller.Enabled = false;
2025-02-28 13:50:52 +01:00
}
2025-05-05 13:59:12 +02:00
private float _accelerationPerSecond;
private float _decelerationPerSecond;
2025-02-28 13:50:52 +01:00
public override void PhysicsProcessState(double delta)
2025-05-05 13:59:12 +02:00
{
2025-03-14 15:13:00 +01:00
// Reset at start of frame
2025-05-05 13:59:12 +02:00
//MainObject.Velocity = Vector2.Zero;
2025-03-14 15:13:00 +01:00
// Process modules
base.PhysicsProcessState(delta);
2025-05-05 13:59:12 +02:00
if (_isStrafing)
{
// Instant movement at strafe speed
MainObject.Velocity = _movementDirection * StrafeSpeed;
}
else
{
Vector2 targetVelocity = _movementDirection * Speed;
2025-03-14 15:13:00 +01:00
2025-05-05 13:59:12 +02:00
if (_movementDirection != Vector2.Zero)
{
MainObject.Velocity = MainObject.Velocity.MoveToward(targetVelocity, Acceleration * (float)delta);
}
else
{
MainObject.Velocity = MainObject.Velocity.MoveToward(Vector2.Zero, Deceleration * (float)delta);
}
}
2025-03-14 15:13:00 +01:00
2025-05-05 13:59:12 +02:00
//MainObject.Velocity += _movementDirection * MovementSpeed;
MainObject.MoveAndSlide();
2025-02-28 13:50:52 +01:00
}
public override void ProcessState(double delta)
{
2025-03-14 15:13:00 +01:00
base.ProcessState(delta);
2025-05-05 13:59:12 +02:00
2025-03-01 10:05:13 +01:00
_movementDirection = _inputProvider.GetMovementInput().Normalized();
2025-02-28 22:49:55 +01:00
_isStrafing = _inputProvider.GetStrafePressed();
2025-02-28 13:50:52 +01:00
// Toggle visibility of the hitbox sprite based on strafing
_hitboxSprite_provider?.SetVisibility(_isStrafing);
2025-05-05 13:59:12 +02:00
2025-03-01 10:05:13 +01:00
var rightStickInput = _inputProvider.GetAimInput().Normalized();
2025-02-28 13:50:52 +01:00
// Update Facing Direction
2025-03-13 16:59:16 +01:00
// if (!_isStrafing)
// {
2025-05-05 13:59:12 +02:00
if (rightStickInput.Length() > 0.1f) // If the right stick is moved
{
FacingDirection = rightStickInput;
}
else if (_movementDirection != Vector2.Zero) // Fall back to movement direction
{
FacingDirection = _movementDirection;
}
2025-03-13 16:59:16 +01:00
// }
2025-02-28 13:50:52 +01:00
_animationProvider?.SetAnimationSpeed(MainObject.Velocity);
_animationProvider?.SetAnimation(FacingDirection);
2025-02-28 22:49:55 +01:00
2025-05-06 10:53:49 +02:00
HandleWeaponSwitch();
_weaponProvider?.Update(delta);
2025-05-06 10:53:49 +02:00
//_crosshair_provider.UpdatePosition(FacingDirection);
2025-05-05 13:59:12 +02:00
2025-02-28 19:59:36 +01:00
HandleShoot();
2025-05-05 13:59:12 +02:00
2025-03-01 18:02:11 +01:00
HandleInteraction();
2025-02-28 13:50:52 +01:00
// FindInteractable();
// _crosshair.Position = CalculateCrosshairPosition();
2025-03-01 20:50:47 +01:00
if (_inputProvider.GetInventoryJustPressed())
{
GameStateManager.SetState(GameState.Inventory);
2025-03-01 20:50:47 +01:00
}
if (_inputProvider.GetPauseJustPressed())
{
//CallDeferred(MethodName.PauseDeferred);
PauseDeferred();
}
}
private void PauseDeferred()
{
GameStateManager.Instance.Pause();
2025-02-28 13:50:52 +01:00
}
2025-02-28 19:59:36 +01:00
private void HandleShoot()
{
2025-05-07 21:50:00 +02:00
if (_inputProvider.GetReloadJustPressed())
{
_weaponProvider?.Reload();
2025-05-07 21:50:00 +02:00
return;
}
2025-02-28 22:49:55 +01:00
if (!_inputProvider.GetShootPressed()) return;
_weaponProvider?.Shoot(this.FacingDirection);
2025-02-28 19:59:36 +01:00
}
2025-03-01 18:02:11 +01:00
private void HandleInteraction()
{
_activationProvider?.HandleInteraction();
2025-03-01 18:02:11 +01:00
}
2025-04-09 23:24:55 +02:00
private void HandleWeaponSwitch()
{
if (_inputProvider.GetWeaponNextJustPressed())
{
_weaponProvider?.NextWeapon();
2025-04-09 23:24:55 +02:00
}
else if (_inputProvider.GetWeaponPreviousJustPressed())
{
_weaponProvider?.PreviousWeapon();
2025-04-09 23:24:55 +02:00
}
}
2025-05-05 13:59:12 +02:00
}