diff --git a/scripts/world/ChunkManager.cs b/scripts/world/ChunkManager.cs index c1c17ff..97d1677 100644 --- a/scripts/world/ChunkManager.cs +++ b/scripts/world/ChunkManager.cs @@ -5,9 +5,9 @@ using System.Text.Json; public partial class ChunkManager : Node2D { - [Export] public NodePath PlayerPath; - [Export] public NodePath PackManagerPath; - [Export] public int LoadRadius = 1; // 1 -> 3x3 + [Export] public NodePath PlayerPath { get; set; } + [Export] public NodePath PackManagerPath { get; set; } + [Export] public int LoadRadius { get; set; } = 1; // 1 => 3x3 private CharacterBody2D? _player; private PackManager? _packs; @@ -28,13 +28,7 @@ public partial class ChunkManager : Node2D _packs = GetNodeOrNull(PackManagerPath); if (_packs == null) { - // fallback: search up the tree for PackManager - _packs = GetParent()?.GetNodeOrNull("PackManager"); - } - - if (_packs == null) - { - GD.PrintErr("ChunkManager: PackManager not found. Add it to your Main scene."); + GD.PrintErr("ChunkManager: PackManagerPath not set or invalid."); return; } @@ -43,7 +37,7 @@ public partial class ChunkManager : Node2D private void LoadWorldIndex() { - var worldPath = _packs.Resolve("data/world/world_main.json"); + var worldPath = _packs!.Resolve("data/world/world_main.json"); if (worldPath == null) { GD.PrintErr("ChunkManager: Could not resolve data/world/world_main.json from packs."); @@ -68,22 +62,23 @@ public partial class ChunkManager : Node2D var (cx, cy) = WorldPosToChunk(_player.GlobalPosition, _world.chunk_size_px); + // Load required chunks for (int dy = -LoadRadius; dy <= LoadRadius; dy++) for (int dx = -LoadRadius; dx <= LoadRadius; dx++) { - var key = (cx + dx, cy + dy); - EnsureLoaded(key.x, key.y); + EnsureLoaded(cx + dx, cy + dy); } - // Unload chunks that are too far - var keysToRemove = new List<(int x, int y)>(); + // Unload chunks too far away + var toRemove = new List<(int x, int y)>(); foreach (var kv in _loaded) { var (x, y) = kv.Key; if (Math.Abs(x - cx) > LoadRadius || Math.Abs(y - cy) > LoadRadius) - keysToRemove.Add((x, y)); + toRemove.Add((x, y)); } - foreach (var k in keysToRemove) + + foreach (var k in toRemove) { _loaded[k].QueueFree(); _loaded.Remove(k); @@ -96,7 +91,6 @@ public partial class ChunkManager : Node2D if (_loaded.ContainsKey(key)) return; if (!_chunkMap.TryGetValue(key, out var relPath)) return; - // Use pack resolve for the chunk file itself var chunkPath = _packs!.Resolve(relPath); if (chunkPath == null) { @@ -104,8 +98,6 @@ public partial class ChunkManager : Node2D return; } - // For now, we don't parse chunk contents yet; we just visualize existence + biome by file naming/placeholder. - // Next step: parse biome from JSON and use it to color. var chunkJson = FileAccess.GetFileAsString(chunkPath); using var doc = JsonDocument.Parse(chunkJson); var biome = doc.RootElement.TryGetProperty("biome", out var b) ? b.GetString() : "biome:unknown"; @@ -124,23 +116,25 @@ public partial class ChunkManager : Node2D private static Node2D CreateDebugChunkNode(int cx, int cy, int chunkSizePx, string biome) { - var n = new Node2D(); - n.Name = $"Chunk_{cx}_{cy}"; - n.Position = new Vector2(cx * chunkSizePx, cy * chunkSizePx); - - var rect = new ColorRect(); - rect.Size = new Vector2(chunkSizePx, chunkSizePx); - rect.MouseFilter = Control.MouseFilterEnum.Ignore; - - rect.Color = biome switch + var n = new Node2D { - "biome:forest" => new Color(0.1f, 0.35f, 0.1f, 0.25f), - "biome:plains" => new Color(0.35f, 0.35f, 0.1f, 0.25f), - "biome:town" => new Color(0.25f, 0.25f, 0.25f, 0.25f), - _ => new Color(0.2f, 0.2f, 0.2f, 0.25f) + Name = $"Chunk_{cx}_{cy}", + Position = new Vector2(cx * chunkSizePx, cy * chunkSizePx) + }; + + var rect = new ColorRect + { + Size = new Vector2(chunkSizePx, chunkSizePx), + MouseFilter = Control.MouseFilterEnum.Ignore, + Color = biome switch + { + "biome:forest" => new Color(0.1f, 0.35f, 0.1f, 0.25f), + "biome:plains" => new Color(0.35f, 0.35f, 0.1f, 0.25f), + "biome:town" => new Color(0.25f, 0.25f, 0.25f, 0.25f), + _ => new Color(0.2f, 0.2f, 0.2f, 0.25f) + } }; - // Add an outline using a Panel (cheap) or just rely on transparency for now n.AddChild(rect); return n; }