cirnogodot/Scripts/Components/FSM/BaseState.cs

51 lines
1.3 KiB
C#
Raw Normal View History

2025-03-04 18:16:39 +01:00
using System.Collections.Generic;
using System.Reflection;
using Godot;
using Godot.Collections;
2025-03-04 17:50:16 +01:00
namespace Cirno.Scripts.Components.FSM;
public abstract partial class BaseState<TKey, TType> : Node2D, IState<TKey, TType>
where TKey : notnull
where TType : Node
{
2025-03-04 18:16:39 +01:00
public virtual TKey StateId { get; }
2025-03-04 17:50:16 +01:00
public IStateMachine<TKey, TType> StateMachine => _stateMachine;
private IStateMachine<TKey, TType> _stateMachine;
public TType MainObject => _stateMachine.MainObject;
2025-03-04 18:16:39 +01:00
[Export]
private Array<Node> _moduleNodes = [];
private readonly List<IModule<TKey, TType>> _modules = [];
2025-03-04 17:50:16 +01:00
public virtual void Init(IStateMachine<TKey, TType> machine)
{
_stateMachine = machine;
2025-03-04 18:16:39 +01:00
foreach (var node in _moduleNodes)
{
if (node is IModule<TKey, TType> module)
{
_modules.Add(module);
module.Init(_stateMachine);
}
}
2025-03-04 17:50:16 +01:00
}
public abstract void EnterState();
public abstract void ExitState();
2025-03-04 18:16:39 +01:00
public virtual void ProcessState(double delta)
{
_modules.ForEach(module => module.Process(delta));
}
2025-03-04 17:50:16 +01:00
2025-03-04 18:16:39 +01:00
public virtual void PhysicsProcessState(double delta)
{
_modules.ForEach(module => module.PhysicsProcess(delta));
}
2025-03-04 17:50:16 +01:00
}