♻️ Rebuild with DI
This commit is contained in:
@@ -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)
|
||||
|
23
Scripts/System/DependencyInjection.cs
Normal file
23
Scripts/System/DependencyInjection.cs
Normal 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.");
|
||||
}
|
||||
}
|
1
Scripts/System/DependencyInjection.cs.uid
Normal file
1
Scripts/System/DependencyInjection.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://hppsxnesg0ys
|
@@ -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!");
|
||||
|
@@ -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());
|
||||
|
||||
|
@@ -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");
|
||||
|
Reference in New Issue
Block a user