mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 08:35:34 +00:00
78 lines
No EOL
2.1 KiB
C#
78 lines
No EOL
2.1 KiB
C#
#nullable enable
|
|
using System.Collections.Generic;
|
|
using Godot;
|
|
|
|
namespace Cirno.Scripts.Components.FSM;
|
|
|
|
public abstract partial class IsoStateMachineBase<TKey, TType> : Node, IStateMachine<TKey, TType>
|
|
where TKey : notnull
|
|
where TType : Node
|
|
{
|
|
public Dictionary<TKey, IState<TKey, TType>> States { get; set; } = new();
|
|
public TKey CurrentStateIndex { get; set; } = default!;
|
|
public IState<TKey, TType> CurrentState => States[CurrentStateIndex];
|
|
public abstract TKey InitialState { get; protected set; }
|
|
|
|
private TType _mainObject = default!;
|
|
public TType MainObject => _mainObject;
|
|
|
|
private bool _hasState;
|
|
|
|
public override void _Ready()
|
|
{
|
|
_mainObject = this.GetParent<TType>();
|
|
var children = GetChildren();
|
|
|
|
foreach (var child in children)
|
|
{
|
|
if (child is IState<TKey, TType> state)
|
|
{
|
|
States.Add(state.StateId, state);
|
|
state.Init(this);
|
|
}
|
|
}
|
|
SetState(InitialState);
|
|
}
|
|
|
|
public TKey GetState()
|
|
{
|
|
return CurrentState.StateId;
|
|
}
|
|
|
|
public void SetState(TKey stateId)
|
|
{
|
|
if (_hasState)
|
|
{
|
|
// Keep track of previous state and exit it
|
|
// PreviousStateIndex is not part of this base; derived classes may add it if needed
|
|
CurrentState.ExitState();
|
|
}
|
|
|
|
CurrentStateIndex = stateId;
|
|
CurrentState.EnterState();
|
|
_hasState = true;
|
|
}
|
|
|
|
// Implement the GetModule<T>() helper so this base class satisfies the IStateMachine interface
|
|
public T? GetModule<T>() where T : Node
|
|
{
|
|
if (MainObject is null) return default;
|
|
foreach (var child in MainObject.GetChildren())
|
|
{
|
|
if (child is T t) return t;
|
|
}
|
|
return default;
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
if (!_hasState) return;
|
|
CurrentState.ProcessState(delta);
|
|
}
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
if (!_hasState) return;
|
|
CurrentState.PhysicsProcessState(delta);
|
|
}
|
|
} |