mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-07 10:15:53 +00:00
FSM Test
This commit is contained in:
parent
36147f955d
commit
dfd9abe91b
5 changed files with 126 additions and 6 deletions
44
Scripts/Components/FSM/ActorStateMachine.cs
Normal file
44
Scripts/Components/FSM/ActorStateMachine.cs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM;
|
||||
|
||||
public partial class ActorStateMachine : Node2D
|
||||
{
|
||||
public Dictionary<int, State> States { get; private set; } = new();
|
||||
|
||||
private int _currentStateIndex = 0;
|
||||
|
||||
public State CurrentState => States[_currentStateIndex];
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
var children = GetChildren();
|
||||
|
||||
foreach (var child in children)
|
||||
{
|
||||
if (child is State state)
|
||||
{
|
||||
States.Add(state.StateId, state);
|
||||
state.Init(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetState(int stateId)
|
||||
{
|
||||
CurrentState.ExitState();
|
||||
_currentStateIndex = stateId;
|
||||
CurrentState.EnterState();
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
CurrentState.ProcessState(delta);
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
CurrentState.PhysicsProcessState(delta);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue