mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-06 19:35:54 +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);
|
||||
}
|
||||
}
|
||||
1
Scripts/Components/FSM/ActorStateMachine.cs.uid
Normal file
1
Scripts/Components/FSM/ActorStateMachine.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://v2f387aad33k
|
||||
36
Scripts/Components/FSM/EnemyIdle.cs
Normal file
36
Scripts/Components/FSM/EnemyIdle.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM;
|
||||
|
||||
public partial class EnemyIdle : State
|
||||
{
|
||||
//[Export]
|
||||
//public EnemyState State { get; private set; }
|
||||
public override int StateId => (int)EnemyState.Idle;
|
||||
|
||||
public override void EnterState()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void ExitState()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void ProcessState(double delta)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void PhysicsProcessState(double delta)
|
||||
{
|
||||
// Scan for player
|
||||
// Wait for alarms
|
||||
}
|
||||
|
||||
protected void ChangeState(EnemyState newState)
|
||||
{
|
||||
_stateMachine.SetState((int)newState);
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,22 @@
|
|||
|
||||
namespace Cirno.Scripts.Components.FSM;
|
||||
|
||||
public partial class State : Node2D
|
||||
public abstract partial class State : Node2D
|
||||
{
|
||||
public virtual int StateId { get; }
|
||||
|
||||
protected ActorStateMachine _stateMachine;
|
||||
|
||||
public virtual void Init(ActorStateMachine stateMachine)
|
||||
{
|
||||
_stateMachine = stateMachine;
|
||||
}
|
||||
|
||||
public abstract void EnterState();
|
||||
|
||||
public abstract void ExitState();
|
||||
|
||||
public abstract void ProcessState(double delta);
|
||||
public abstract void PhysicsProcessState(double delta);
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue