Compare commits

..

2 Commits

Author SHA1 Message Date
c72353716f ♻️ Rebuild with DI 2025-08-31 01:44:18 +08:00
1cc941d893 Hud basis 2025-08-30 23:41:49 +08:00
16 changed files with 178 additions and 31 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,38 +1,24 @@
[gd_scene load_steps=8 format=3 uid="uid://c22aprj452aha"]
[gd_scene load_steps=6 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"]
[ext_resource type="Script" uid="uid://cfbj72nm0eovg" path="res://Scripts/System/BuildingRegistry.cs" id="1_sxhdm"]
[ext_resource type="Script" uid="uid://cugfbvw70clgd" path="res://Scripts/System/NaturalResourceGenerator.cs" id="2_oss8w"]
[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"]
[node name="ResourceSystem" type="Node" parent="."]
script = ExtResource("1_pl8e4")
[node name="BuildingRegistry" type="Node" parent="."]
script = ExtResource("1_sxhdm")
[node name="NaturalResourceGenerator" type="Node2D" parent="." node_paths=PackedStringArray("Grid", "Registry")]
[node name="NaturalResourceGenerator" type="Node2D" parent="."]
script = ExtResource("2_oss8w")
Grid = NodePath("../GridSystem")
Registry = NodePath("../BuildingRegistry")
[node name="GridSystem" type="Node2D" parent="."]
script = ExtResource("1_knkkn")
[node name="PlacementSystem" type="Node2D" parent="." node_paths=PackedStringArray("Grid", "Inventory", "Registry")]
[node name="PlacementSystem" type="Node2D" parent="."]
script = ExtResource("2_sxhdm")
Grid = NodePath("../GridSystem")
Inventory = NodePath("../ResourceSystem")
Registry = NodePath("../BuildingRegistry")
[node name="Player" parent="." node_paths=PackedStringArray("Inventory") instance=ExtResource("3_oss8w")]
scale = Vector2(0.35, 0.35)
Inventory = NodePath("../ResourceSystem")
Inventory = NodePath("")
[node name="HUD" parent="." instance=ExtResource("8_hud_scene")]
[node name="ItemPickup" parent="." instance=ExtResource("7_is6ib")]
position = Vector2(-496, -245)

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

View File

@@ -0,0 +1,14 @@
using Godot;
using AceFieldNewHorizon.Scripts.System;
namespace AceFieldNewHorizon.Scripts.AutoLoad;
public partial class DIInitializer : Node
{
public override void _Ready()
{
// Initialize the Simple Injector container as early as possible
DependencyInjection.Initialize();
GD.Print("[DIInitializer] Dependency Injection container initialized via AutoLoad.");
}
}

View File

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

View File

@@ -20,7 +20,7 @@ public partial class Player : CharacterBody2D
[Export] public float ZoomDecay = 0.9f;
[Export] public float ZoomSmoothing = 10.0f;
[Export] public ResourceManager Inventory;
public ResourceManager Inventory { get; private set; }
private Camera2D _camera;
private Vector2 _cameraTargetZoom = Vector2.One;
@@ -33,6 +33,8 @@ public partial class Player : CharacterBody2D
_camera = GetNode<Camera2D>("Camera2D");
_cameraTargetZoom = _camera.Zoom;
Inventory = DependencyInjection.Container.GetInstance<ResourceManager>();
AddToGroup(ItemPickup.PickupGroupName);
AddToGroup(NaturalResourceGenerator.ChunkTrackerGroupName);
}

27
Scripts/Root.cs Normal file
View File

@@ -0,0 +1,27 @@
using Godot;
using AceFieldNewHorizon.Scripts.System;
using SimpleInjector;
namespace AceFieldNewHorizon.Scripts;
public partial class Root : Node
{
public override void _Ready()
{
// Dependency Injection container is now initialized via AutoLoad (DIInitializer.cs).
// Get references to the main system nodes from the scene tree
// and inject their dependencies.
// This assumes these nodes are direct children or easily accessible.
// You might need to adjust paths based on your scene setup.
// Example:
// var resourceManager = GetNode<ResourceManager>("ResourceSystem"); // Assuming ResourceManager is a child of Root
// DependencyInjection.Container.InjectProperties(resourceManager); // If ResourceManager had properties to inject
// For now, we'll manually resolve and assign for the main system nodes.
// The actual injection will happen in the _Ready methods of the system nodes themselves,
// by resolving from the static container.
// This is a common pattern when Godot instantiates the nodes.
}
}

1
Scripts/Root.cs.uid Normal file
View File

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

View File

@@ -40,15 +40,15 @@ public record BuildingData(
}
}
public partial class BuildingRegistry : Node
public class BuildingRegistry
{
private Dictionary<string, BuildingData> _registry = new();
[Export] public string JsonPath { get; set; } = "res://Data/Buildings.json";
public override void _Ready()
public BuildingRegistry(string jsonPath)
{
LoadFromJson(JsonPath);
LoadFromJson(jsonPath);
}
public void LoadFromJson(string path)

View File

@@ -0,0 +1,23 @@
using Godot;
using Container = SimpleInjector.Container;
namespace AceFieldNewHorizon.Scripts.System;
public static class DependencyInjection
{
public static Container Container { get; private set; }
public static void Initialize()
{
Container = new Container();
// Register your system services here
// As singletons, since they are typically unique global managers
Container.RegisterSingleton<ResourceManager>();
Container.RegisterSingleton<GridManager>();
Container.RegisterSingleton(() => new BuildingRegistry("res://Data/Buildings.json"));
Container.Verify();
GD.Print("[DI] Simple Injector container initialized and verified.");
}
}

View File

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

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 = DependencyInjection.Container.GetInstance<ResourceManager>();
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

@@ -12,8 +12,8 @@ public partial class NaturalResourceGenerator : Node2D
{
public const string ChunkTrackerGroupName = "NrgTrackingTarget";
[Export] public GridManager Grid { get; set; }
[Export] public BuildingRegistry Registry { get; set; }
public GridManager Grid { get; private set; }
public BuildingRegistry Registry { get; private set; }
[Export] public int ChunkSize = 16;
[Export] public int LoadDistance = 2; // Number of chunks to load in each direction
@@ -43,6 +43,8 @@ public partial class NaturalResourceGenerator : Node2D
public override void _Ready()
{
Grid = DependencyInjection.Container.GetInstance<GridManager>();
Registry = DependencyInjection.Container.GetInstance<BuildingRegistry>();
_rng = new RandomNumberGenerator();
_rng.Seed = (ulong)(Seed != 0 ? Seed : (int)GD.Randi());

View File

@@ -8,9 +8,9 @@ namespace AceFieldNewHorizon.Scripts.System;
public partial class PlacementManager : Node2D
{
[Export] public GridManager Grid { get; set; }
[Export] public ResourceManager Inventory { get; set; }
[Export] public BuildingRegistry Registry { get; set; }
public GridManager Grid { get; private set; }
public ResourceManager Inventory { get; private set; }
public BuildingRegistry Registry { get; private set; }
[Export] public int MaxConcurrentBuilds { get; set; } = 6; // Make it adjustable in editor
[Export] public bool Enabled { get; set; } = true;
@@ -30,6 +30,10 @@ public partial class PlacementManager : Node2D
{
base._Ready();
Grid = DependencyInjection.Container.GetInstance<GridManager>();
Inventory = DependencyInjection.Container.GetInstance<ResourceManager>();
Registry = DependencyInjection.Container.GetInstance<BuildingRegistry>();
// Setup completion sound
_completionSound = CreateAudioPlayer("res://Sounds/Events/ConstructionComplete.wav");
_buildingSound = CreateAudioPlayer("res://Sounds/Events/Building.wav");

View File

@@ -15,6 +15,11 @@ run/main_scene="uid://c22aprj452aha"
config/features=PackedStringArray("4.4", "C#", "GL Compatibility")
config/icon="res://icon.svg"
[autoload]
ResourceManager="res://Scripts/System/ResourceManager.cs"
DiInitializer="*res://Scripts/AutoLoad/DIInitializer.cs"
[display]
window/size/viewport_width=1920