Hud basis

This commit is contained in:
2025-08-30 23:41:49 +08:00
parent 88647b1c41
commit 1cc941d893
6 changed files with 89 additions and 1 deletions

View File

@@ -4,4 +4,7 @@
<EnableDynamicLoading>true</EnableDynamicLoading>
<RootNamespace>AceFieldNewHorizon</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SimpleInjector" Version="5.5.0" />
</ItemGroup>
</Project>

View File

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

13
Scenes/System/HUD.tscn Normal file
View File

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

64
Scripts/System/Hud.cs Normal file
View File

@@ -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<string, Label> _resourceLabels = new();
public override void _Ready()
{
_resourceDisplay = GetNode<VBoxContainer>("ResourceDisplay");
_resourceManager = GetTree().CurrentScene.GetNode<ResourceManager>("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;
}
}
}

View File

@@ -0,0 +1 @@
uid://ddoqqcg77f60v

View File

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