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

@ -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")

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