♻️ Rebuild with DI

This commit is contained in:
2025-08-31 01:44:18 +08:00
parent 1cc941d893
commit c72353716f
13 changed files with 95 additions and 36 deletions

View File

@@ -1,8 +1,5 @@
[gd_scene load_steps=9 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"]
@@ -11,29 +8,15 @@
[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")]

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

View File

@@ -13,7 +13,7 @@ public partial class Hud : CanvasLayer
{
_resourceDisplay = GetNode<VBoxContainer>("ResourceDisplay");
_resourceManager = GetTree().CurrentScene.GetNode<ResourceManager>("ResourceSystem");
_resourceManager = DependencyInjection.Container.GetInstance<ResourceManager>();
if (_resourceManager == null)
{
GD.PushError("ResourceSystem not found in the scene tree!");

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
@@ -88,7 +93,3 @@ 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"