diff --git a/AceField New Horizon.csproj b/AceField New Horizon.csproj index d2ec174..3b8c4c6 100644 --- a/AceField New Horizon.csproj +++ b/AceField New Horizon.csproj @@ -4,4 +4,7 @@ true AceFieldNewHorizon + + + \ No newline at end of file diff --git a/Scenes/Root.tscn b/Scenes/Root.tscn index 8daefc5..2a0261f 100644 --- a/Scenes/Root.tscn +++ b/Scenes/Root.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=8 format=3 uid="uid://c22aprj452aha"] +[gd_scene load_steps=9 format=3 uid="uid://c22aprj452aha"] [ext_resource type="Script" uid="uid://cudpc3w17mbsw" path="res://Scripts/System/GridManager.cs" id="1_knkkn"] [ext_resource type="Script" uid="uid://dfi2snip78eq6" path="res://Scripts/System/ResourceManager.cs" id="1_pl8e4"] @@ -7,6 +7,7 @@ [ext_resource type="Script" uid="uid://bx1wj7gn6vrqe" path="res://Scripts/System/PlacementManager.cs" id="2_sxhdm"] [ext_resource type="PackedScene" uid="uid://doxy60afddg1m" path="res://Scenes/Entities/Player.tscn" id="3_oss8w"] [ext_resource type="PackedScene" uid="uid://xwkplaxmye3v" path="res://Scenes/System/ItemPickup.tscn" id="7_is6ib"] +[ext_resource type="PackedScene" uid="uid://byv2vu0k2drdd" path="res://Scenes/System/HUD.tscn" id="8_hud_scene"] [node name="Root" type="Node2D"] @@ -34,6 +35,8 @@ Registry = NodePath("../BuildingRegistry") scale = Vector2(0.35, 0.35) Inventory = NodePath("../ResourceSystem") +[node name="HUD" parent="." instance=ExtResource("8_hud_scene")] + [node name="ItemPickup" parent="." instance=ExtResource("7_is6ib")] position = Vector2(-496, -245) ItemId = "stone" diff --git a/Scenes/System/HUD.tscn b/Scenes/System/HUD.tscn new file mode 100644 index 0000000..3c8462e --- /dev/null +++ b/Scenes/System/HUD.tscn @@ -0,0 +1,13 @@ +[gd_scene load_steps=2 format=3 uid="uid://byv2vu0k2drdd"] + +[ext_resource type="Script" uid="uid://ddoqqcg77f60v" path="res://Scripts/System/Hud.cs" id="1_yw4ou"] + +[node name="HUD" type="CanvasLayer"] +script = ExtResource("1_yw4ou") + +[node name="ResourceDisplay" type="VBoxContainer" parent="."] +layout_mode = 0 +offset_left = 10.0 +offset_top = 10.0 +offset_right = 100.0 +offset_bottom = 100.0 diff --git a/Scripts/System/Hud.cs b/Scripts/System/Hud.cs new file mode 100644 index 0000000..5a7f6c4 --- /dev/null +++ b/Scripts/System/Hud.cs @@ -0,0 +1,64 @@ +using Godot; +using System.Collections.Generic; + +namespace AceFieldNewHorizon.Scripts.System; + +public partial class Hud : CanvasLayer +{ + private ResourceManager _resourceManager; + private VBoxContainer _resourceDisplay; + private readonly Dictionary _resourceLabels = new(); + + public override void _Ready() + { + _resourceDisplay = GetNode("ResourceDisplay"); + + _resourceManager = GetTree().CurrentScene.GetNode("ResourceSystem"); + if (_resourceManager == null) + { + GD.PushError("ResourceSystem not found in the scene tree!"); + return; + } + + _resourceManager.OnResourceChanged += UpdateResourceDisplay; + + // Initialize display with current resources + foreach (var entry in _resourceManager.GetAllResources()) + { + UpdateResourceDisplay(entry.Key, entry.Value); + } + } + + private void UpdateResourceDisplay(string resourceId, int newAmount) + { + if (!_resourceLabels.TryGetValue(resourceId, out Label label)) + { + label = new Label(); + _resourceDisplay.AddChild(label); + _resourceLabels.Add(resourceId, label); + } + + if (newAmount <= 0) + { + // Remove label if resource amount is zero or less + if (label.GetParent() != null) + { + _resourceDisplay.RemoveChild(label); + } + _resourceLabels.Remove(resourceId); + label.QueueFree(); // Free the label from memory + } + else + { + label.Text = $"{resourceId}: {newAmount}"; + } + } + + public override void _ExitTree() + { + if (_resourceManager != null) + { + _resourceManager.OnResourceChanged -= UpdateResourceDisplay; + } + } +} diff --git a/Scripts/System/Hud.cs.uid b/Scripts/System/Hud.cs.uid new file mode 100644 index 0000000..ba2dcef --- /dev/null +++ b/Scripts/System/Hud.cs.uid @@ -0,0 +1 @@ +uid://ddoqqcg77f60v diff --git a/project.godot b/project.godot index 9addbf7..dd89990 100644 --- a/project.godot +++ b/project.godot @@ -88,3 +88,7 @@ anti_aliasing/quality/msaa_2d=3 anti_aliasing/quality/msaa_3d=3 anti_aliasing/quality/screen_space_aa=1 anti_aliasing/quality/use_taa=true + +[autoload] + +ResourceManager="res://Scripts/System/ResourceManager.cs"