diff --git a/Scenes/Maps/PlayerFSMTest.tscn b/Scenes/Maps/PlayerFSMTest.tscn index 3a2f1b5b..e2296f84 100644 --- a/Scenes/Maps/PlayerFSMTest.tscn +++ b/Scenes/Maps/PlayerFSMTest.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=38 format=4 uid="uid://dqyfnby0t7gu1"] +[gd_scene load_steps=43 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,11 +32,14 @@ [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"] [sub_resource type="Resource" id="Resource_6wo78"] script = ExtResource("4_u1i8n") EggIndex = 0 -StartingEquipment = Array[ExtResource("5_u1i8n")]([]) +StartingEquipment = [] [sub_resource type="RectangleShape2D" id="RectangleShape2D_rff8l"] size = Vector2(30, 52.5) @@ -50,6 +53,10 @@ 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"] process_mode = 3 script = ExtResource("1_c3v4x") @@ -57,7 +64,7 @@ PlayerTemplate = ExtResource("8_c3v4x") SpawnMarkers = Dictionary[int, NodePath]({ 0: NodePath("PlayerStartPosition") }) -StartingEquipment = Array[ExtResource("5_u1i8n")]([ExtResource("3_6314l"), ExtResource("4_yyg8m")]) +StartingEquipment = [ExtResource("3_6314l"), ExtResource("4_yyg8m")] MapStartData = SubResource("Resource_6wo78") [node name="Tilemaps" type="Node2D" parent="."] @@ -193,3 +200,22 @@ script = ExtResource("13_8fnge") [node name="AudioStreamPlayer2D" parent="." instance=ExtResource("14_q7rh4")] process_mode = 3 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"] +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"] +script = ExtResource("35_mpb62") diff --git a/Scripts/Components/FSM/BaseState.cs b/Scripts/Components/FSM/BaseState.cs new file mode 100644 index 00000000..32ee49a5 --- /dev/null +++ b/Scripts/Components/FSM/BaseState.cs @@ -0,0 +1,28 @@ +using Godot; + +namespace Cirno.Scripts.Components.FSM; + +public abstract partial class BaseState : Node2D, IState + where TKey : notnull + where TType : Node +{ + public TKey StateId { get; } + public IStateMachine StateMachine => _stateMachine; + + private IStateMachine _stateMachine; + + public TType MainObject => _stateMachine.MainObject; + + public virtual void Init(IStateMachine machine) + { + _stateMachine = machine; + } + + public abstract void EnterState(); + + public abstract void ExitState(); + + public abstract void ProcessState(double delta); + + public abstract void PhysicsProcessState(double delta); +} \ No newline at end of file diff --git a/Scripts/Components/FSM/BaseState.cs.uid b/Scripts/Components/FSM/BaseState.cs.uid new file mode 100644 index 00000000..cb3594e1 --- /dev/null +++ b/Scripts/Components/FSM/BaseState.cs.uid @@ -0,0 +1 @@ +uid://dfx3ry6bq62qb diff --git a/Scripts/Components/FSM/Door/DoorFSM.cs b/Scripts/Components/FSM/Door/DoorFSM.cs new file mode 100644 index 00000000..f2ed4c26 --- /dev/null +++ b/Scripts/Components/FSM/Door/DoorFSM.cs @@ -0,0 +1,6 @@ +namespace Cirno.Scripts.Components.FSM.Door; + +public class DoorFSM +{ + +} \ No newline at end of file diff --git a/Scripts/Components/FSM/Door/DoorFSM.cs.uid b/Scripts/Components/FSM/Door/DoorFSM.cs.uid new file mode 100644 index 00000000..4a9fadda --- /dev/null +++ b/Scripts/Components/FSM/Door/DoorFSM.cs.uid @@ -0,0 +1 @@ +uid://y7d8x23abje2 diff --git a/Scripts/Components/FSM/IModule.cs b/Scripts/Components/FSM/IModule.cs new file mode 100644 index 00000000..a5a5e59d --- /dev/null +++ b/Scripts/Components/FSM/IModule.cs @@ -0,0 +1,6 @@ +namespace Cirno.Scripts.Components.FSM; + +public interface IModule +{ + +} \ No newline at end of file diff --git a/Scripts/Components/FSM/IState.cs b/Scripts/Components/FSM/IState.cs new file mode 100644 index 00000000..298b5037 --- /dev/null +++ b/Scripts/Components/FSM/IState.cs @@ -0,0 +1,18 @@ +using Godot; + +namespace Cirno.Scripts.Components.FSM; + +public interface IState + where TKey : notnull + where TType : Node +{ + public TKey StateId { get; } + public IStateMachine StateMachine { get; } + + public void Init(IStateMachine machine); + + public void EnterState(); + public void ExitState(); + public void ProcessState(double delta); + public void PhysicsProcessState(double delta); +} \ No newline at end of file diff --git a/Scripts/Components/FSM/IStateMachine.cs b/Scripts/Components/FSM/IStateMachine.cs new file mode 100644 index 00000000..d72b8c86 --- /dev/null +++ b/Scripts/Components/FSM/IStateMachine.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; +using Godot; + +namespace Cirno.Scripts.Components.FSM; + +public interface IStateMachine + where TKey : notnull + where TType : Node +{ + public Dictionary> States { get; set; } + protected TKey CurrentStateIndex { get; set; } + public IState CurrentState { get; } + public TKey InitialState { get; } + public void SetState(TKey stateId); + + public TType MainObject { get; } +} \ No newline at end of file diff --git a/Scripts/Components/FSM/ModuleBase.cs b/Scripts/Components/FSM/ModuleBase.cs new file mode 100644 index 00000000..0f83d22b --- /dev/null +++ b/Scripts/Components/FSM/ModuleBase.cs @@ -0,0 +1,8 @@ +using Godot; + +namespace Cirno.Scripts.Components.FSM; + +public partial class ModuleBase : Node2D, IModule +{ + +} \ No newline at end of file diff --git a/Scripts/Components/FSM/ModuleBase.cs.uid b/Scripts/Components/FSM/ModuleBase.cs.uid new file mode 100644 index 00000000..775a4c97 --- /dev/null +++ b/Scripts/Components/FSM/ModuleBase.cs.uid @@ -0,0 +1 @@ +uid://dgsf8t1a156jl diff --git a/Scripts/Components/FSM/NewPlayerStateMachine.cs b/Scripts/Components/FSM/NewPlayerStateMachine.cs new file mode 100644 index 00000000..8be7c1bc --- /dev/null +++ b/Scripts/Components/FSM/NewPlayerStateMachine.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using Godot; + +namespace Cirno.Scripts.Components.FSM; + +public partial class NewPlayerStateMachine : StateMachineBase +{ + [Export] public override PlayerState InitialState { get; protected set; } = PlayerState.Init; + + +} \ No newline at end of file diff --git a/Scripts/Components/FSM/NewPlayerStateMachine.cs.uid b/Scripts/Components/FSM/NewPlayerStateMachine.cs.uid new file mode 100644 index 00000000..283177ee --- /dev/null +++ b/Scripts/Components/FSM/NewPlayerStateMachine.cs.uid @@ -0,0 +1 @@ +uid://bgertv72tq1dt diff --git a/Scripts/Components/FSM/NodeClassTest.cs b/Scripts/Components/FSM/NodeClassTest.cs new file mode 100644 index 00000000..d104933c --- /dev/null +++ b/Scripts/Components/FSM/NodeClassTest.cs @@ -0,0 +1,8 @@ +using Godot; + +namespace Cirno.Scripts.Components.FSM; + +public partial class NodeClassTest : Node2D +{ + +} \ No newline at end of file diff --git a/Scripts/Components/FSM/NodeClassTest.cs.uid b/Scripts/Components/FSM/NodeClassTest.cs.uid new file mode 100644 index 00000000..4b27b341 --- /dev/null +++ b/Scripts/Components/FSM/NodeClassTest.cs.uid @@ -0,0 +1 @@ +uid://ht8lw77ouq1k diff --git a/Scripts/Components/FSM/Player/NewInit.cs b/Scripts/Components/FSM/Player/NewInit.cs new file mode 100644 index 00000000..009397fb --- /dev/null +++ b/Scripts/Components/FSM/Player/NewInit.cs @@ -0,0 +1,33 @@ +using Godot; + +namespace Cirno.Scripts.Components.FSM.Player; + +public partial class NewInit : BaseState +{ + public PlayerState StateId => PlayerState.Init; + + public override void Init(IStateMachine 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) + { + + } +} \ No newline at end of file diff --git a/Scripts/Components/FSM/Player/NewInit.cs.uid b/Scripts/Components/FSM/Player/NewInit.cs.uid new file mode 100644 index 00000000..1b3fc966 --- /dev/null +++ b/Scripts/Components/FSM/Player/NewInit.cs.uid @@ -0,0 +1 @@ +uid://btwuahxvreivs diff --git a/Scripts/Components/FSM/StateMachineBase.cs b/Scripts/Components/FSM/StateMachineBase.cs new file mode 100644 index 00000000..fcbe2d9f --- /dev/null +++ b/Scripts/Components/FSM/StateMachineBase.cs @@ -0,0 +1,58 @@ +using System.Collections.Generic; +using Godot; + +namespace Cirno.Scripts.Components.FSM; + +public abstract partial class StateMachineBase : Node2D, IStateMachine + where TKey : notnull + where TType : Node +{ + public Dictionary> States { get; set; } = new(); + public TKey CurrentStateIndex { get; set; } + public IState CurrentState => States[CurrentStateIndex]; + public abstract TKey InitialState { get; protected set; } + + private TType _mainObject; + public TType MainObject => _mainObject; + + public override void _Ready() + { + _mainObject = this.GetParent(); + var children = GetChildren(); + + foreach (var child in children) + { + if (child is IState 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); + } +} \ No newline at end of file diff --git a/Scripts/Components/FSM/StateMachineBase.cs.uid b/Scripts/Components/FSM/StateMachineBase.cs.uid new file mode 100644 index 00000000..1d3cb87e --- /dev/null +++ b/Scripts/Components/FSM/StateMachineBase.cs.uid @@ -0,0 +1 @@ +uid://b3egnis61ecrg