♻️ Rebuild with DI
This commit is contained in:
		| @@ -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")] | ||||
|  | ||||
|   | ||||
							
								
								
									
										14
									
								
								Scripts/AutoLoad/DIInitializer.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								Scripts/AutoLoad/DIInitializer.cs
									
									
									
									
									
										Normal 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."); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										1
									
								
								Scripts/AutoLoad/DIInitializer.cs.uid
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								Scripts/AutoLoad/DIInitializer.cs.uid
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| uid://cr2a8w6ur4uei | ||||
| @@ -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
									
								
							
							
						
						
									
										27
									
								
								Scripts/Root.cs
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										1
									
								
								Scripts/Root.cs.uid
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| uid://dmint8ii0oj5g | ||||
| @@ -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"); | ||||
|   | ||||
| @@ -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" | ||||
|   | ||||
		Reference in New Issue
	
	Block a user