diff --git a/Scenes/Root.tscn b/Scenes/Root.tscn index 2a0261f..4bad00f 100644 --- a/Scenes/Root.tscn +++ b/Scenes/Root.tscn @@ -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")] diff --git a/Scripts/AutoLoad/DIInitializer.cs b/Scripts/AutoLoad/DIInitializer.cs new file mode 100644 index 0000000..1d09fac --- /dev/null +++ b/Scripts/AutoLoad/DIInitializer.cs @@ -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."); + } +} diff --git a/Scripts/AutoLoad/DIInitializer.cs.uid b/Scripts/AutoLoad/DIInitializer.cs.uid new file mode 100644 index 0000000..ef4e211 --- /dev/null +++ b/Scripts/AutoLoad/DIInitializer.cs.uid @@ -0,0 +1 @@ +uid://cr2a8w6ur4uei diff --git a/Scripts/Entities/Player.cs b/Scripts/Entities/Player.cs index 92c3f78..0b8b70c 100644 --- a/Scripts/Entities/Player.cs +++ b/Scripts/Entities/Player.cs @@ -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"); _cameraTargetZoom = _camera.Zoom; + Inventory = DependencyInjection.Container.GetInstance(); + AddToGroup(ItemPickup.PickupGroupName); AddToGroup(NaturalResourceGenerator.ChunkTrackerGroupName); } diff --git a/Scripts/Root.cs b/Scripts/Root.cs new file mode 100644 index 0000000..2ab3c71 --- /dev/null +++ b/Scripts/Root.cs @@ -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("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. + } +} diff --git a/Scripts/Root.cs.uid b/Scripts/Root.cs.uid new file mode 100644 index 0000000..2a6073e --- /dev/null +++ b/Scripts/Root.cs.uid @@ -0,0 +1 @@ +uid://dmint8ii0oj5g diff --git a/Scripts/System/BuildingRegistry.cs b/Scripts/System/BuildingRegistry.cs index 3e0f05a..a3a04aa 100644 --- a/Scripts/System/BuildingRegistry.cs +++ b/Scripts/System/BuildingRegistry.cs @@ -40,15 +40,15 @@ public record BuildingData( } } -public partial class BuildingRegistry : Node +public class BuildingRegistry { private Dictionary _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) diff --git a/Scripts/System/DependencyInjection.cs b/Scripts/System/DependencyInjection.cs new file mode 100644 index 0000000..4b7c954 --- /dev/null +++ b/Scripts/System/DependencyInjection.cs @@ -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(); + Container.RegisterSingleton(); + Container.RegisterSingleton(() => new BuildingRegistry("res://Data/Buildings.json")); + + Container.Verify(); + GD.Print("[DI] Simple Injector container initialized and verified."); + } +} \ No newline at end of file diff --git a/Scripts/System/DependencyInjection.cs.uid b/Scripts/System/DependencyInjection.cs.uid new file mode 100644 index 0000000..6a717fd --- /dev/null +++ b/Scripts/System/DependencyInjection.cs.uid @@ -0,0 +1 @@ +uid://hppsxnesg0ys diff --git a/Scripts/System/Hud.cs b/Scripts/System/Hud.cs index 5a7f6c4..932e8ca 100644 --- a/Scripts/System/Hud.cs +++ b/Scripts/System/Hud.cs @@ -13,7 +13,7 @@ public partial class Hud : CanvasLayer { _resourceDisplay = GetNode("ResourceDisplay"); - _resourceManager = GetTree().CurrentScene.GetNode("ResourceSystem"); + _resourceManager = DependencyInjection.Container.GetInstance(); if (_resourceManager == null) { GD.PushError("ResourceSystem not found in the scene tree!"); diff --git a/Scripts/System/NaturalResourceGenerator.cs b/Scripts/System/NaturalResourceGenerator.cs index 5a09b81..34294c7 100644 --- a/Scripts/System/NaturalResourceGenerator.cs +++ b/Scripts/System/NaturalResourceGenerator.cs @@ -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(); + Registry = DependencyInjection.Container.GetInstance(); _rng = new RandomNumberGenerator(); _rng.Seed = (ulong)(Seed != 0 ? Seed : (int)GD.Randi()); diff --git a/Scripts/System/PlacementManager.cs b/Scripts/System/PlacementManager.cs index 5aebf1c..2e20f5b 100644 --- a/Scripts/System/PlacementManager.cs +++ b/Scripts/System/PlacementManager.cs @@ -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(); + Inventory = DependencyInjection.Container.GetInstance(); + Registry = DependencyInjection.Container.GetInstance(); + // Setup completion sound _completionSound = CreateAudioPlayer("res://Sounds/Events/ConstructionComplete.wav"); _buildingSound = CreateAudioPlayer("res://Sounds/Events/Building.wav"); diff --git a/project.godot b/project.godot index dd89990..b46c40b 100644 --- a/project.godot +++ b/project.godot @@ -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"