mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-09 02:55:54 +00:00
New FSM
This commit is contained in:
parent
b0d5edc84e
commit
5a09b7fcd1
18 changed files with 229 additions and 3 deletions
28
Scripts/Components/FSM/BaseState.cs
Normal file
28
Scripts/Components/FSM/BaseState.cs
Normal 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);
|
||||
}
|
||||
1
Scripts/Components/FSM/BaseState.cs.uid
Normal file
1
Scripts/Components/FSM/BaseState.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dfx3ry6bq62qb
|
||||
6
Scripts/Components/FSM/Door/DoorFSM.cs
Normal file
6
Scripts/Components/FSM/Door/DoorFSM.cs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
namespace Cirno.Scripts.Components.FSM.Door;
|
||||
|
||||
public class DoorFSM
|
||||
{
|
||||
|
||||
}
|
||||
1
Scripts/Components/FSM/Door/DoorFSM.cs.uid
Normal file
1
Scripts/Components/FSM/Door/DoorFSM.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://y7d8x23abje2
|
||||
6
Scripts/Components/FSM/IModule.cs
Normal file
6
Scripts/Components/FSM/IModule.cs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
namespace Cirno.Scripts.Components.FSM;
|
||||
|
||||
public interface IModule
|
||||
{
|
||||
|
||||
}
|
||||
18
Scripts/Components/FSM/IState.cs
Normal file
18
Scripts/Components/FSM/IState.cs
Normal 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);
|
||||
}
|
||||
17
Scripts/Components/FSM/IStateMachine.cs
Normal file
17
Scripts/Components/FSM/IStateMachine.cs
Normal 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; }
|
||||
}
|
||||
8
Scripts/Components/FSM/ModuleBase.cs
Normal file
8
Scripts/Components/FSM/ModuleBase.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM;
|
||||
|
||||
public partial class ModuleBase : Node2D, IModule
|
||||
{
|
||||
|
||||
}
|
||||
1
Scripts/Components/FSM/ModuleBase.cs.uid
Normal file
1
Scripts/Components/FSM/ModuleBase.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dgsf8t1a156jl
|
||||
11
Scripts/Components/FSM/NewPlayerStateMachine.cs
Normal file
11
Scripts/Components/FSM/NewPlayerStateMachine.cs
Normal 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;
|
||||
|
||||
|
||||
}
|
||||
1
Scripts/Components/FSM/NewPlayerStateMachine.cs.uid
Normal file
1
Scripts/Components/FSM/NewPlayerStateMachine.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://bgertv72tq1dt
|
||||
8
Scripts/Components/FSM/NodeClassTest.cs
Normal file
8
Scripts/Components/FSM/NodeClassTest.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM;
|
||||
|
||||
public partial class NodeClassTest : Node2D
|
||||
{
|
||||
|
||||
}
|
||||
1
Scripts/Components/FSM/NodeClassTest.cs.uid
Normal file
1
Scripts/Components/FSM/NodeClassTest.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://ht8lw77ouq1k
|
||||
33
Scripts/Components/FSM/Player/NewInit.cs
Normal file
33
Scripts/Components/FSM/Player/NewInit.cs
Normal 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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
1
Scripts/Components/FSM/Player/NewInit.cs.uid
Normal file
1
Scripts/Components/FSM/Player/NewInit.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://btwuahxvreivs
|
||||
58
Scripts/Components/FSM/StateMachineBase.cs
Normal file
58
Scripts/Components/FSM/StateMachineBase.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
1
Scripts/Components/FSM/StateMachineBase.cs.uid
Normal file
1
Scripts/Components/FSM/StateMachineBase.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://b3egnis61ecrg
|
||||
Loading…
Add table
Add a link
Reference in a new issue