Modules for FSM

This commit is contained in:
Marco 2025-03-04 18:16:39 +01:00
commit bdc310d204
9 changed files with 74 additions and 23 deletions

View file

@ -1,4 +1,7 @@
using Godot;
using System.Collections.Generic;
using System.Reflection;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Components.FSM;
@ -6,23 +9,43 @@ public abstract partial class BaseState<TKey, TType> : Node2D, IState<TKey, TTyp
where TKey : notnull
where TType : Node
{
public TKey StateId { get; }
public virtual TKey StateId { get; }
public IStateMachine<TKey, TType> StateMachine => _stateMachine;
private IStateMachine<TKey, TType> _stateMachine;
public TType MainObject => _stateMachine.MainObject;
[Export]
private Array<Node> _moduleNodes = [];
private readonly List<IModule<TKey, TType>> _modules = [];
public virtual void Init(IStateMachine<TKey, TType> machine)
{
_stateMachine = machine;
foreach (var node in _moduleNodes)
{
if (node is IModule<TKey, TType> module)
{
_modules.Add(module);
module.Init(_stateMachine);
}
}
}
public abstract void EnterState();
public abstract void ExitState();
public abstract void ProcessState(double delta);
public virtual void ProcessState(double delta)
{
_modules.ForEach(module => module.Process(delta));
}
public abstract void PhysicsProcessState(double delta);
public virtual void PhysicsProcessState(double delta)
{
_modules.ForEach(module => module.PhysicsProcess(delta));
}
}