mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-15 08:13:47 +00:00
Add FSM components for player and enemy state management, including initialization and module resolution
This commit is contained in:
parent
18683c0680
commit
b6cc5a00e8
57 changed files with 526 additions and 506 deletions
72
Scripts/Components/FSM/Enemy/3D/InitState.cs
Normal file
72
Scripts/Components/FSM/Enemy/3D/InitState.cs
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
using Cirno.Scripts.Components.Actors;
|
||||
using Cirno.Scripts.Enums;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM.Enemy._3D;
|
||||
|
||||
public partial class InitState : EnemyStateBase3D
|
||||
{
|
||||
public override EnemyState StateId => EnemyState.Init;
|
||||
private EnemyStorage3D Storage { get; set; }
|
||||
private PlayerDetection3D DetectionProvider { get; set; }
|
||||
private ActorResourceProvider HealthProvider { get; set; }
|
||||
|
||||
private int _initRetryCount = 0;
|
||||
private const int MaxInitRetries = 20;
|
||||
private const float InitRetryDelaySeconds = 0.05f;
|
||||
|
||||
public override void Init(IStateMachine<EnemyState, CharacterBody3D> machine)
|
||||
{
|
||||
base.Init(machine);
|
||||
// Try an initial resolve; EnterState will retry if necessary
|
||||
Storage ??= StateMachine.GetModule<EnemyStorage3D>();
|
||||
DetectionProvider ??= StateMachine.GetModule<PlayerDetection3D>();
|
||||
HealthProvider ??= StateMachine.GetModule<ActorResourceProvider>();
|
||||
}
|
||||
|
||||
|
||||
public override void EnterState()
|
||||
{
|
||||
// Attempt to complete initialization; if modules are missing, retry with a timer
|
||||
TryCompleteInit();
|
||||
}
|
||||
|
||||
private void TryCompleteInit()
|
||||
{
|
||||
// Attempt to resolve modules again in case they were attached after Init
|
||||
Storage ??= StateMachine.GetModule<EnemyStorage3D>();
|
||||
DetectionProvider ??= StateMachine.GetModule<PlayerDetection3D>();
|
||||
HealthProvider ??= StateMachine.GetModule<ActorResourceProvider>();
|
||||
|
||||
if (Storage is null || DetectionProvider is null || HealthProvider is null)
|
||||
{
|
||||
_initRetryCount++;
|
||||
if (_initRetryCount <= MaxInitRetries)
|
||||
{
|
||||
// Schedule a retry shortly to wait for other nodes to finish _Ready
|
||||
var timer = GetTree().CreateTimer(InitRetryDelaySeconds);
|
||||
timer.Timeout += TryCompleteInit;
|
||||
return;
|
||||
}
|
||||
|
||||
// Final failure: log detailed error and bail out to avoid throws
|
||||
var actorName = MainObject?.Name ?? "<unknown>";
|
||||
GD.PrintErr($"[InitState] Failed to resolve required modules on actor '{actorName}' after {_initRetryCount} attempts.\n" +
|
||||
$" Storage: {(Storage is null ? "MISSING" : "OK")},\n" +
|
||||
$" DetectionProvider: {(DetectionProvider is null ? "MISSING" : "OK")},\n" +
|
||||
$" HealthProvider: {(HealthProvider is null ? "MISSING" : "OK")}");
|
||||
return;
|
||||
}
|
||||
|
||||
// All modules present, proceed with initialization
|
||||
DetectionProvider.Initialize(MainObject);
|
||||
Storage.AiState = Storage.Root.StartingAiState;
|
||||
Storage.HomePosition = MainObject.GlobalPosition;
|
||||
|
||||
HealthProvider.MaxResource = Storage.Root.EnemyResource.MaxHealth;
|
||||
HealthProvider.FillResource();
|
||||
|
||||
// Transition to the Idle state now that initialization is complete
|
||||
StateMachine.SetState(EnemyState.Idle);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue