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)
|
|
|
|
|
|
{
|
2025-03-08 11:33:26 +01:00
|
|
|
|
if (node is not IModule<TKey, TType> module) continue;
|
|
|
|
|
|
_modules.Add(module);
|
|
|
|
|
|
module.Init(_stateMachine);
|
2025-03-04 18:16:39 +01:00
|
|
|
|
}
|
2025-03-04 17:50:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-05 10:55:14 +01:00
|
|
|
|
protected void ChangeState(TKey newState)
|
|
|
|
|
|
{
|
|
|
|
|
|
_stateMachine.SetState(newState);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-08 11:33:26 +01:00
|
|
|
|
public virtual void EnterState()
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var module in _modules)
|
|
|
|
|
|
{
|
|
|
|
|
|
module.EnterState(StateId);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-03-04 17:50:16 +01:00
|
|
|
|
|
2025-03-08 11:33:26 +01:00
|
|
|
|
public virtual void ExitState()
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var module in _modules)
|
|
|
|
|
|
{
|
|
|
|
|
|
module.ExitState(StateId);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-03-04 17:50:16 +01:00
|
|
|
|
|
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
|
|
|
|
}
|