diff --git a/Scenes/Maps/PlayerFSMTest.tscn b/Scenes/Maps/PlayerFSMTest.tscn index e2296f84..e82b949b 100644 --- a/Scenes/Maps/PlayerFSMTest.tscn +++ b/Scenes/Maps/PlayerFSMTest.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=43 format=4 uid="uid://dqyfnby0t7gu1"] +[gd_scene load_steps=42 format=4 uid="uid://dqyfnby0t7gu1"] [ext_resource type="Script" uid="uid://doxmbokehw8ci" path="res://Scripts/GameManager.cs" id="1_c3v4x"] [ext_resource type="Resource" uid="uid://cs3ihltcn2166" path="res://Resources/Items/IcicleGun.tres" id="3_6314l"] @@ -32,9 +32,9 @@ [ext_resource type="Resource" uid="uid://dau0s8ob7qnpc" path="res://Resources/Items/IceShotgun.tres" id="22_4gtx8"] [ext_resource type="Resource" uid="uid://ct1fa2huvy34n" path="res://Resources/Items/Ammo1.tres" id="23_1ajuh"] [ext_resource type="PackedScene" uid="uid://bjskkeb3ppcs8" path="res://Scenes/Actors/Turret360.tscn" id="24_rff8l"] -[ext_resource type="Script" uid="uid://ht8lw77ouq1k" path="res://Scripts/Components/FSM/NodeClassTest.cs" id="33_k6t8d"] [ext_resource type="Script" uid="uid://bgertv72tq1dt" path="res://Scripts/Components/FSM/NewPlayerStateMachine.cs" id="34_2vu2h"] [ext_resource type="Script" uid="uid://btwuahxvreivs" path="res://Scripts/Components/FSM/Player/NewInit.cs" id="35_mpb62"] +[ext_resource type="Script" uid="uid://dl50bcl8dx3k8" path="res://Scripts/Components/FSM/TestModule.cs" id="36_4rfvg"] [sub_resource type="Resource" id="Resource_6wo78"] script = ExtResource("4_u1i8n") @@ -53,8 +53,6 @@ colors = PackedColorArray(1, 1, 1, 1, 1, 1, 1, 1) [sub_resource type="GradientTexture1D" id="GradientTexture1D_4gtx8"] gradient = SubResource("Gradient_2vu2h") -[sub_resource type="CircleShape2D" id="CircleShape2D_2vu2h"] - [sub_resource type="CircleShape2D" id="CircleShape2D_mpb62"] [node name="GameScene" type="Node2D"] @@ -203,12 +201,6 @@ autoplay = true [node name="Nodetest" type="Node2D" parent="."] -[node name="CharacterBody2D" type="CharacterBody2D" parent="Nodetest"] -script = ExtResource("33_k6t8d") - -[node name="CollisionShape2D" type="CollisionShape2D" parent="Nodetest/CharacterBody2D"] -shape = SubResource("CircleShape2D_2vu2h") - [node name="Body" type="CharacterBody2D" parent="Nodetest"] [node name="CollisionShape2D" type="CollisionShape2D" parent="Nodetest/Body"] @@ -217,5 +209,9 @@ shape = SubResource("CircleShape2D_mpb62") [node name="FSM" type="Node2D" parent="Nodetest/Body"] script = ExtResource("34_2vu2h") -[node name="Init" type="Node2D" parent="Nodetest/Body/FSM"] +[node name="Init" type="Node2D" parent="Nodetest/Body/FSM" node_paths=PackedStringArray("_moduleNodes")] script = ExtResource("35_mpb62") +_moduleNodes = [NodePath("../../TestModule")] + +[node name="TestModule" type="Node2D" parent="Nodetest/Body"] +script = ExtResource("36_4rfvg") diff --git a/Scripts/Components/FSM/BaseState.cs b/Scripts/Components/FSM/BaseState.cs index 32ee49a5..da0e95a4 100644 --- a/Scripts/Components/FSM/BaseState.cs +++ b/Scripts/Components/FSM/BaseState.cs @@ -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 : Node2D, IState StateMachine => _stateMachine; private IStateMachine _stateMachine; public TType MainObject => _stateMachine.MainObject; + + [Export] + private Array _moduleNodes = []; + + private readonly List> _modules = []; public virtual void Init(IStateMachine machine) { _stateMachine = machine; + + foreach (var node in _moduleNodes) + { + if (node is IModule 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)); + } } \ No newline at end of file diff --git a/Scripts/Components/FSM/IModule.cs b/Scripts/Components/FSM/IModule.cs index a5a5e59d..19b0c1b4 100644 --- a/Scripts/Components/FSM/IModule.cs +++ b/Scripts/Components/FSM/IModule.cs @@ -1,6 +1,12 @@ -namespace Cirno.Scripts.Components.FSM; +using Godot; -public interface IModule +namespace Cirno.Scripts.Components.FSM; + +public interface IModule + where TKey : notnull + where TType : Node { - + public void Init(IStateMachine machine); + public void Process(double delta); + public void PhysicsProcess(double delta); } \ No newline at end of file diff --git a/Scripts/Components/FSM/IState.cs b/Scripts/Components/FSM/IState.cs index 298b5037..1d38e194 100644 --- a/Scripts/Components/FSM/IState.cs +++ b/Scripts/Components/FSM/IState.cs @@ -2,7 +2,7 @@ namespace Cirno.Scripts.Components.FSM; -public interface IState +public interface IState where TKey : notnull where TType : Node { diff --git a/Scripts/Components/FSM/IStateMachine.cs b/Scripts/Components/FSM/IStateMachine.cs index d72b8c86..47aab622 100644 --- a/Scripts/Components/FSM/IStateMachine.cs +++ b/Scripts/Components/FSM/IStateMachine.cs @@ -3,7 +3,7 @@ using Godot; namespace Cirno.Scripts.Components.FSM; -public interface IStateMachine +public interface IStateMachine where TKey : notnull where TType : Node { diff --git a/Scripts/Components/FSM/ModuleBase.cs b/Scripts/Components/FSM/ModuleBase.cs index 0f83d22b..e005de67 100644 --- a/Scripts/Components/FSM/ModuleBase.cs +++ b/Scripts/Components/FSM/ModuleBase.cs @@ -2,7 +2,11 @@ namespace Cirno.Scripts.Components.FSM; -public partial class ModuleBase : Node2D, IModule +public abstract partial class ModuleBase : Node2D, IModule + where TKey : notnull + where TType : Node { - + public abstract void Init(IStateMachine machine); + public abstract void Process(double delta); + public abstract void PhysicsProcess(double delta); } \ No newline at end of file diff --git a/Scripts/Components/FSM/Player/NewInit.cs b/Scripts/Components/FSM/Player/NewInit.cs index 009397fb..0a8a083f 100644 --- a/Scripts/Components/FSM/Player/NewInit.cs +++ b/Scripts/Components/FSM/Player/NewInit.cs @@ -4,7 +4,7 @@ namespace Cirno.Scripts.Components.FSM.Player; public partial class NewInit : BaseState { - public PlayerState StateId => PlayerState.Init; + public override PlayerState StateId => PlayerState.Init; public override void Init(IStateMachine machine) { diff --git a/Scripts/Components/FSM/TestModule.cs b/Scripts/Components/FSM/TestModule.cs new file mode 100644 index 00000000..cc1f5719 --- /dev/null +++ b/Scripts/Components/FSM/TestModule.cs @@ -0,0 +1,21 @@ +using Godot; + +namespace Cirno.Scripts.Components.FSM; + +public partial class TestModule : ModuleBase +{ + public override void Init(IStateMachine machine) + { + GD.Print($"Module Init {Name}"); + } + + public override void Process(double delta) + { + + } + + public override void PhysicsProcess(double delta) + { + + } +} \ No newline at end of file diff --git a/Scripts/Components/FSM/TestModule.cs.uid b/Scripts/Components/FSM/TestModule.cs.uid new file mode 100644 index 00000000..83945488 --- /dev/null +++ b/Scripts/Components/FSM/TestModule.cs.uid @@ -0,0 +1 @@ +uid://dl50bcl8dx3k8