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;
|
|
|
|
|
|
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-03-15 17:56:55 +01:00
|
|
|
public Vector2 FacingDirection
|
|
|
|
|
{
|
|
|
|
|
get => _storageModule.FacingDirection;
|
|
|
|
|
private set => _storageModule.FacingDirection = value;
|
|
|
|
|
}
|
2025-03-01 10:05:13 +01:00
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
|
public int Speed { get; set; } = 45;
|
2025-02-28 13:50:52 +01:00
|
|
|
|
2025-03-01 10:05:13 +01:00
|
|
|
[Export]
|
|
|
|
|
public int StrafeSpeed { get; set; } = 35;
|
|
|
|
|
|
|
|
|
|
[ExportCategory("Providers")]
|
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;
|
2025-03-01 10:05:13 +01:00
|
|
|
[Export]
|
|
|
|
|
private PlayerCrosshairProvider _crosshairProvider;
|
|
|
|
|
[Export] private PlayerHitboxSpriteProvider _hitboxSpriteProvider;
|
2025-02-28 22:49:55 +01:00
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
|
private InputProvider _inputProvider;
|
2025-02-28 19:59:36 +01:00
|
|
|
|
2025-03-01 14:08:31 +01:00
|
|
|
[Export] private PlayerDamageReceiver _damageReceiver;
|
2025-03-01 18:02:11 +01:00
|
|
|
[Export] private ActivationProvider _activationProvider;
|
2025-02-28 13:50:52 +01:00
|
|
|
|
2025-03-03 10:58:20 +01:00
|
|
|
[Export] private InteractionController _interactionController;
|
2025-03-15 17:56:55 +01:00
|
|
|
|
|
|
|
|
[Export] private PlayerStorageModule _storageModule;
|
2025-03-03 10:58:20 +01:00
|
|
|
|
2025-03-01 14:08:31 +01:00
|
|
|
private bool _isStrafing;
|
2025-03-01 10:05:13 +01: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-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;
|
|
|
|
|
|
2025-03-01 14:08:31 +01:00
|
|
|
_damageReceiver.Death += () =>
|
|
|
|
|
{
|
|
|
|
|
ChangeState(PlayerState.Dead);
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-03 10:58:20 +01:00
|
|
|
_damageReceiver.HealthDecreased += (value, newValue, maxValue) =>
|
2025-03-01 14:08:31 +01:00
|
|
|
{
|
|
|
|
|
_animationProvider.Blink();
|
2025-03-03 10:58:20 +01:00
|
|
|
//_hud.UpdateHealth(value, maxValue);
|
2025-03-01 14:08:31 +01:00
|
|
|
};
|
|
|
|
|
|
2025-03-03 10:58:20 +01:00
|
|
|
_damageReceiver.ShieldDecreased += (value, newValue, maxValue) =>
|
2025-03-01 14:08:31 +01:00
|
|
|
{
|
|
|
|
|
_animationProvider.PlayShieldAnimation();
|
2025-03-03 10:58:20 +01:00
|
|
|
//_hud.UpdateShield(value, maxValue);
|
2025-03-01 14:08:31 +01:00
|
|
|
};
|
|
|
|
|
|
2025-03-11 17:58:46 +01:00
|
|
|
_damageReceiver.Init(StateMachine);
|
2025-03-01 14:08:31 +01:00
|
|
|
|
2025-03-03 09:20:11 +01:00
|
|
|
_damageReceiver.RefillHealth();
|
|
|
|
|
_damageReceiver.RefillShield();
|
|
|
|
|
|
2025-03-05 10:55:14 +01:00
|
|
|
_activationProvider.Init(MainObject);
|
2025-03-01 18:02:11 +01:00
|
|
|
|
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
|
|
|
|
2025-03-05 10:55:14 +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
|
2025-03-01 10:05:13 +01:00
|
|
|
_crosshairProvider.Show();
|
2025-03-02 11:58:30 +01:00
|
|
|
_animationProvider.ShowSprite();
|
2025-03-01 14:08:31 +01:00
|
|
|
_damageReceiver.Enabled = true;
|
2025-03-01 18:02:11 +01:00
|
|
|
_activationProvider.Enabled = true;
|
2025-03-03 10:58:20 +01:00
|
|
|
_interactionController.Enabled = true;
|
2025-02-28 13:50:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void ExitState()
|
|
|
|
|
{
|
2025-03-08 11:33:26 +01:00
|
|
|
base.ExitState();
|
2025-04-08 10:44:06 +02:00
|
|
|
_animationProvider.SetAnimationSpeed(Vector2.Zero);
|
|
|
|
|
//_animationProvider.SetAnimation(Vector2.Zero);
|
2025-03-01 10:05:13 +01:00
|
|
|
_crosshairProvider.Hide();
|
|
|
|
|
_hitboxSpriteProvider.Hide();
|
2025-03-01 14:08:31 +01:00
|
|
|
|
|
|
|
|
_damageReceiver.Enabled = false;
|
2025-03-01 18:02:11 +01:00
|
|
|
_activationProvider.Enabled = false;
|
2025-03-03 10:58:20 +01:00
|
|
|
_interactionController.Enabled = false;
|
2025-02-28 13:50:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void PhysicsProcessState(double delta)
|
2025-03-14 15:13:00 +01:00
|
|
|
{
|
|
|
|
|
// Reset at start of frame
|
|
|
|
|
MainObject.Velocity = Vector2.Zero;
|
|
|
|
|
|
|
|
|
|
// Process modules
|
|
|
|
|
base.PhysicsProcessState(delta);
|
|
|
|
|
|
|
|
|
|
MainObject.Velocity += _movementDirection * MovementSpeed;
|
2025-02-28 13:50:52 +01:00
|
|
|
|
2025-03-05 10:55:14 +01:00
|
|
|
MainObject.MoveAndSlide();
|
2025-03-14 15:13:00 +01:00
|
|
|
|
|
|
|
|
|
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-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
|
2025-03-01 10:05:13 +01:00
|
|
|
_hitboxSpriteProvider.SetVisibility(_isStrafing);
|
|
|
|
|
|
|
|
|
|
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-02-28 13:50:52 +01:00
|
|
|
if (rightStickInput.Length() > 0.1f) // If the right stick is moved
|
|
|
|
|
{
|
2025-03-13 16:59:16 +01:00
|
|
|
FacingDirection = rightStickInput;
|
2025-02-28 13:50:52 +01:00
|
|
|
}
|
|
|
|
|
else if (_movementDirection != Vector2.Zero) // Fall back to movement direction
|
|
|
|
|
{
|
2025-03-01 10:05:13 +01:00
|
|
|
FacingDirection = _movementDirection;
|
2025-02-28 13:50:52 +01:00
|
|
|
}
|
2025-03-13 16:59:16 +01:00
|
|
|
// }
|
2025-02-28 13:50:52 +01:00
|
|
|
|
2025-04-08 10:44:06 +02:00
|
|
|
_animationProvider.SetAnimationSpeed(MainObject.Velocity);
|
|
|
|
|
_animationProvider.SetAnimation(FacingDirection);
|
2025-02-28 22:49:55 +01:00
|
|
|
|
2025-03-01 10:05:13 +01:00
|
|
|
_crosshairProvider.UpdatePosition(FacingDirection);
|
|
|
|
|
|
2025-02-28 19:59:36 +01:00
|
|
|
HandleShoot();
|
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())
|
|
|
|
|
{
|
|
|
|
|
GameManager.Instance.ChangeState(GameState.Inventory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_inputProvider.GetPauseJustPressed())
|
|
|
|
|
{
|
|
|
|
|
//CallDeferred(MethodName.PauseDeferred);
|
|
|
|
|
PauseDeferred();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PauseDeferred()
|
|
|
|
|
{
|
|
|
|
|
GameManager.Instance.Pause();
|
2025-02-28 13:50:52 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-28 19:59:36 +01:00
|
|
|
private void HandleShoot()
|
|
|
|
|
{
|
2025-02-28 22:49:55 +01:00
|
|
|
if (!_inputProvider.GetShootPressed()) return;
|
2025-03-01 10:05:13 +01:00
|
|
|
_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-04-09 23:24:55 +02:00
|
|
|
|
|
|
|
|
private void HandleWeaponSwitch()
|
|
|
|
|
{
|
|
|
|
|
if (_inputProvider.GetWeaponNextJustPressed())
|
|
|
|
|
{
|
|
|
|
|
_weaponProvider.NextWeapon();
|
|
|
|
|
}
|
|
|
|
|
else if (_inputProvider.GetWeaponPreviousJustPressed())
|
|
|
|
|
{
|
|
|
|
|
_weaponProvider.PreviousWeapon();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-28 13:50:52 +01:00
|
|
|
}
|