cirnogodot/Scripts/Components/FSM/BaseState.cs

74 lines
1.8 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;
2025-06-11 15:28:26 +02:00
public abstract partial class BaseState<TKey, TType> : Node, IState<TKey, TType>
2025-03-04 17:50:16 +01:00
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 = [];
2025-03-20 18:22:40 +01:00
protected 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)
{
2025-03-14 15:13:00 +01:00
foreach (var module in _modules)
{
module.Process(delta);
}
//_modules.ForEach(module => module.Process(delta));
2025-03-04 18:16:39 +01:00
}
2025-03-04 17:50:16 +01:00
2025-03-04 18:16:39 +01:00
public virtual void PhysicsProcessState(double delta)
{
2025-03-14 15:13:00 +01:00
foreach (var module in _modules)
{
module.PhysicsProcess(delta);
}
//_modules.ForEach(module => module.PhysicsProcess(delta));
2025-03-04 18:16:39 +01:00
}
2025-03-04 17:50:16 +01:00
}