From cc3ca574675971f32990b96f41f7740ca6e29928 Mon Sep 17 00:00:00 2001 From: HumanoidSandvichDispenser Date: Mon, 24 Jul 2023 12:40:28 -0700 Subject: [PATCH] global game state manager --- Debug/DebugConsole.cs | 34 ++++++++++++++++++++++++++++++++++ Scenes/Level.tscn | 11 +++++++++++ State/Global/GlobalState.cs | 10 ++++++++++ State/Global/Progression.cs | 9 +++++++++ UI/Debug/DebugUI.tscn | 18 ++++++++++++++++++ Utils/World.cs | 22 ++++++++++++++++++++++ project.godot | 4 ++++ 7 files changed, 108 insertions(+) create mode 100644 Debug/DebugConsole.cs create mode 100644 State/Global/GlobalState.cs create mode 100644 State/Global/Progression.cs create mode 100644 UI/Debug/DebugUI.tscn diff --git a/Debug/DebugConsole.cs b/Debug/DebugConsole.cs new file mode 100644 index 0000000..585582f --- /dev/null +++ b/Debug/DebugConsole.cs @@ -0,0 +1,34 @@ +using Godot; + +namespace SupaLidlGame.Debug; + +public partial class DebugConsole : Node +{ + public void SetProp( + Utils.World world, + string entityName, + string property, + string value) + { + var ent = world.CurrentMap.Entities.GetNodeOrNull(entityName); + if (ent is not null) + { + ent.Set(property, value); + } + } + + public string CallMethod( + Utils.World world, + string entityName, + string method, + string[] args) + { + var ent = world.CurrentMap.Entities.GetNodeOrNull(entityName); + if (ent is not null) + { + var returnValue = ent.Call(method, args); + return returnValue.ToString(); + } + return ""; + } +} diff --git a/Scenes/Level.tscn b/Scenes/Level.tscn index 863d4a0..8c736b2 100644 --- a/Scenes/Level.tscn +++ b/Scenes/Level.tscn @@ -58,4 +58,15 @@ alignment = 1 visible = false layout_mode = 2 +[node name="DebugUI" type="CanvasLayer" parent="."] +layer = 2 + +[node name="Control" type="Control" parent="DebugUI"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 + [node name="MusicPlayer" type="AudioStreamPlayer" parent="."] diff --git a/State/Global/GlobalState.cs b/State/Global/GlobalState.cs new file mode 100644 index 0000000..0299471 --- /dev/null +++ b/State/Global/GlobalState.cs @@ -0,0 +1,10 @@ +using Godot; + +namespace SupaLidlGame.State.Global; + +public partial class GlobalState : Node +{ + public Utils.World World { get; set; } + + public Progression Progression { get; set; } +} diff --git a/State/Global/Progression.cs b/State/Global/Progression.cs new file mode 100644 index 0000000..187e60a --- /dev/null +++ b/State/Global/Progression.cs @@ -0,0 +1,9 @@ +using Godot; +using Godot.Collections; + +namespace SupaLidlGame.State.Global; + +public partial class Progression : Resource +{ + public Dictionary BossStatus { get; set; } +} diff --git a/UI/Debug/DebugUI.tscn b/UI/Debug/DebugUI.tscn new file mode 100644 index 0000000..cf880c7 --- /dev/null +++ b/UI/Debug/DebugUI.tscn @@ -0,0 +1,18 @@ +[gd_scene format=3 uid="uid://be8bc4eivsg4s"] + +[node name="DebugUI" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="VBoxContainer" type="VBoxContainer" parent="."] +layout_mode = 1 +anchors_preset = 12 +anchor_top = 1.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 0 diff --git a/Utils/World.cs b/Utils/World.cs index 367c489..d876215 100644 --- a/Utils/World.cs +++ b/Utils/World.cs @@ -17,6 +17,9 @@ public partial class World : Node2D [Export] public Player CurrentPlayer { get; set; } + [Export] + public Boss CurrentBoss { get; set; } + [Export] public UI.UIController UIController { get; set; } @@ -44,7 +47,18 @@ public partial class World : Node2D public override void _Ready() { + // check if world already exists + + var globalState = GetNode("/root/GlobalState"); + if (globalState.World is not null) + { + throw new System.InvalidOperationException(); + } + + globalState.World = this; + Godot.RenderingServer.SetDefaultClearColor(Godot.Colors.Black); + if (StartingArea is not null) { LoadScene(StartingArea); @@ -74,11 +88,19 @@ public partial class World : Node2D public void RegisterBoss(Boss boss) { + CurrentBoss = boss; UIController.BossBar.Boss = boss; MusicPlayer.Stream = boss.Music; MusicPlayer.Play(); } + public void DeregisterBoss(Boss boss) + { + CurrentBoss = null; + UIController.BossBar.Boss = null; + MusicPlayer.Stop(); + } + private void LoadMap(Map map) { GD.Print("Loading map " + map.Name); diff --git a/project.godot b/project.godot index 62ac2ac..ab0dc94 100644 --- a/project.godot +++ b/project.godot @@ -15,6 +15,10 @@ run/main_scene="res://Scenes/Level.tscn" config/features=PackedStringArray("4.1", "C#", "Forward Plus") config/icon="res://icon.svg" +[autoload] + +DebugConsole="*res://Debug/DebugConsole.cs" + [display] window/size/viewport_width=640