mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 11:15:33 +00:00
Modules for FSM
This commit is contained in:
parent
5a09b7fcd1
commit
bdc310d204
9 changed files with 74 additions and 23 deletions
|
|
@ -1,4 +1,7 @@
|
|||
using Godot;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM;
|
||||
|
||||
|
|
@ -6,23 +9,43 @@ public abstract partial class BaseState<TKey, TType> : Node2D, IState<TKey, TTyp
|
|||
where TKey : notnull
|
||||
where TType : Node
|
||||
{
|
||||
public TKey StateId { get; }
|
||||
public virtual TKey StateId { get; }
|
||||
public IStateMachine<TKey, TType> StateMachine => _stateMachine;
|
||||
|
||||
private IStateMachine<TKey, TType> _stateMachine;
|
||||
|
||||
public TType MainObject => _stateMachine.MainObject;
|
||||
|
||||
[Export]
|
||||
private Array<Node> _moduleNodes = [];
|
||||
|
||||
private readonly List<IModule<TKey, TType>> _modules = [];
|
||||
|
||||
public virtual void Init(IStateMachine<TKey, TType> machine)
|
||||
{
|
||||
_stateMachine = machine;
|
||||
|
||||
foreach (var node in _moduleNodes)
|
||||
{
|
||||
if (node is IModule<TKey, TType> module)
|
||||
{
|
||||
_modules.Add(module);
|
||||
module.Init(_stateMachine);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void EnterState();
|
||||
|
||||
public abstract void ExitState();
|
||||
|
||||
public abstract void ProcessState(double delta);
|
||||
public virtual void ProcessState(double delta)
|
||||
{
|
||||
_modules.ForEach(module => module.Process(delta));
|
||||
}
|
||||
|
||||
public abstract void PhysicsProcessState(double delta);
|
||||
public virtual void PhysicsProcessState(double delta)
|
||||
{
|
||||
_modules.ForEach(module => module.PhysicsProcess(delta));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,12 @@
|
|||
namespace Cirno.Scripts.Components.FSM;
|
||||
using Godot;
|
||||
|
||||
public interface IModule
|
||||
namespace Cirno.Scripts.Components.FSM;
|
||||
|
||||
public interface IModule<TKey, TType>
|
||||
where TKey : notnull
|
||||
where TType : Node
|
||||
{
|
||||
|
||||
public void Init(IStateMachine<TKey, TType> machine);
|
||||
public void Process(double delta);
|
||||
public void PhysicsProcess(double delta);
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Cirno.Scripts.Components.FSM;
|
||||
|
||||
public interface IState<TKey, TType>
|
||||
public interface IState<TKey, [MustBeVariant] TType>
|
||||
where TKey : notnull
|
||||
where TType : Node
|
||||
{
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ using Godot;
|
|||
|
||||
namespace Cirno.Scripts.Components.FSM;
|
||||
|
||||
public interface IStateMachine<TKey, TType>
|
||||
public interface IStateMachine<TKey, [MustBeVariant] TType>
|
||||
where TKey : notnull
|
||||
where TType : Node
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,7 +2,11 @@
|
|||
|
||||
namespace Cirno.Scripts.Components.FSM;
|
||||
|
||||
public partial class ModuleBase : Node2D, IModule
|
||||
public abstract partial class ModuleBase<TKey, TType> : Node2D, IModule<TKey, TType>
|
||||
where TKey : notnull
|
||||
where TType : Node
|
||||
{
|
||||
|
||||
public abstract void Init(IStateMachine<TKey, TType> machine);
|
||||
public abstract void Process(double delta);
|
||||
public abstract void PhysicsProcess(double delta);
|
||||
}
|
||||
|
|
@ -4,7 +4,7 @@ namespace Cirno.Scripts.Components.FSM.Player;
|
|||
|
||||
public partial class NewInit : BaseState<PlayerState, CharacterBody2D>
|
||||
{
|
||||
public PlayerState StateId => PlayerState.Init;
|
||||
public override PlayerState StateId => PlayerState.Init;
|
||||
|
||||
public override void Init(IStateMachine<PlayerState, CharacterBody2D> machine)
|
||||
{
|
||||
|
|
|
|||
21
Scripts/Components/FSM/TestModule.cs
Normal file
21
Scripts/Components/FSM/TestModule.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM;
|
||||
|
||||
public partial class TestModule : ModuleBase<PlayerState, CharacterBody2D>
|
||||
{
|
||||
public override void Init(IStateMachine<PlayerState, CharacterBody2D> machine)
|
||||
{
|
||||
GD.Print($"Module Init {Name}");
|
||||
}
|
||||
|
||||
public override void Process(double delta)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void PhysicsProcess(double delta)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
1
Scripts/Components/FSM/TestModule.cs.uid
Normal file
1
Scripts/Components/FSM/TestModule.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dl50bcl8dx3k8
|
||||
Loading…
Add table
Add a link
Reference in a new issue