This commit is contained in:
Marco 2025-03-04 17:50:16 +01:00
commit 5a09b7fcd1
18 changed files with 229 additions and 3 deletions

View file

@ -0,0 +1,28 @@
using Godot;
namespace Cirno.Scripts.Components.FSM;
public abstract partial class BaseState<TKey, TType> : Node2D, IState<TKey, TType>
where TKey : notnull
where TType : Node
{
public TKey StateId { get; }
public IStateMachine<TKey, TType> StateMachine => _stateMachine;
private IStateMachine<TKey, TType> _stateMachine;
public TType MainObject => _stateMachine.MainObject;
public virtual void Init(IStateMachine<TKey, TType> machine)
{
_stateMachine = machine;
}
public abstract void EnterState();
public abstract void ExitState();
public abstract void ProcessState(double delta);
public abstract void PhysicsProcessState(double delta);
}

View file

@ -0,0 +1 @@
uid://dfx3ry6bq62qb

View file

@ -0,0 +1,6 @@
namespace Cirno.Scripts.Components.FSM.Door;
public class DoorFSM
{
}

View file

@ -0,0 +1 @@
uid://y7d8x23abje2

View file

@ -0,0 +1,6 @@
namespace Cirno.Scripts.Components.FSM;
public interface IModule
{
}

View file

@ -0,0 +1,18 @@
using Godot;
namespace Cirno.Scripts.Components.FSM;
public interface IState<TKey, TType>
where TKey : notnull
where TType : Node
{
public TKey StateId { get; }
public IStateMachine<TKey, TType> StateMachine { get; }
public void Init(IStateMachine<TKey, TType> machine);
public void EnterState();
public void ExitState();
public void ProcessState(double delta);
public void PhysicsProcessState(double delta);
}

View file

@ -0,0 +1,17 @@
using System.Collections.Generic;
using Godot;
namespace Cirno.Scripts.Components.FSM;
public interface IStateMachine<TKey, TType>
where TKey : notnull
where TType : Node
{
public Dictionary<TKey, IState<TKey, TType>> States { get; set; }
protected TKey CurrentStateIndex { get; set; }
public IState<TKey, TType> CurrentState { get; }
public TKey InitialState { get; }
public void SetState(TKey stateId);
public TType MainObject { get; }
}

View file

@ -0,0 +1,8 @@
using Godot;
namespace Cirno.Scripts.Components.FSM;
public partial class ModuleBase : Node2D, IModule
{
}

View file

@ -0,0 +1 @@
uid://dgsf8t1a156jl

View file

@ -0,0 +1,11 @@
using System.Collections.Generic;
using Godot;
namespace Cirno.Scripts.Components.FSM;
public partial class NewPlayerStateMachine : StateMachineBase<PlayerState, CharacterBody2D>
{
[Export] public override PlayerState InitialState { get; protected set; } = PlayerState.Init;
}

View file

@ -0,0 +1 @@
uid://bgertv72tq1dt

View file

@ -0,0 +1,8 @@
using Godot;
namespace Cirno.Scripts.Components.FSM;
public partial class NodeClassTest : Node2D
{
}

View file

@ -0,0 +1 @@
uid://ht8lw77ouq1k

View file

@ -0,0 +1,33 @@
using Godot;
namespace Cirno.Scripts.Components.FSM.Player;
public partial class NewInit : BaseState<PlayerState, CharacterBody2D>
{
public PlayerState StateId => PlayerState.Init;
public override void Init(IStateMachine<PlayerState, CharacterBody2D> machine)
{
base.Init(machine);
}
public override void EnterState()
{
GD.Print($"{Name} Entered");
}
public override void ExitState()
{
GD.Print($"{Name} Exited");
}
public override void ProcessState(double delta)
{
}
public override void PhysicsProcessState(double delta)
{
}
}

View file

@ -0,0 +1 @@
uid://btwuahxvreivs

View file

@ -0,0 +1,58 @@
using System.Collections.Generic;
using Godot;
namespace Cirno.Scripts.Components.FSM;
public abstract partial class StateMachineBase<TKey, TType> : Node2D, IStateMachine<TKey, TType>
where TKey : notnull
where TType : Node
{
public Dictionary<TKey, IState<TKey, TType>> States { get; set; } = new();
public TKey CurrentStateIndex { get; set; }
public IState<TKey, TType> CurrentState => States[CurrentStateIndex];
public abstract TKey InitialState { get; protected set; }
private TType _mainObject;
public TType MainObject => _mainObject;
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);
}
}
GD.Print("FSM Ready");
SetState(InitialState);
}
public void SetState(TKey stateId)
{
if (CurrentStateIndex is not null)
{
CurrentState.ExitState();
}
CurrentStateIndex = stateId;
CurrentState.EnterState();
}
public override void _Process(double delta)
{
if (CurrentStateIndex is null) return;
CurrentState.ProcessState(delta);
}
public override void _PhysicsProcess(double delta)
{
if (CurrentStateIndex is null) return;
CurrentState.PhysicsProcessState(delta);
}
}

View file

@ -0,0 +1 @@
uid://b3egnis61ecrg