fix x y error
This commit is contained in:
parent
1058938043
commit
500d852630
1 changed files with 28 additions and 34 deletions
|
|
@ -5,9 +5,9 @@ using System.Text.Json;
|
||||||
|
|
||||||
public partial class ChunkManager : Node2D
|
public partial class ChunkManager : Node2D
|
||||||
{
|
{
|
||||||
[Export] public NodePath PlayerPath;
|
[Export] public NodePath PlayerPath { get; set; }
|
||||||
[Export] public NodePath PackManagerPath;
|
[Export] public NodePath PackManagerPath { get; set; }
|
||||||
[Export] public int LoadRadius = 1; // 1 -> 3x3
|
[Export] public int LoadRadius { get; set; } = 1; // 1 => 3x3
|
||||||
|
|
||||||
private CharacterBody2D? _player;
|
private CharacterBody2D? _player;
|
||||||
private PackManager? _packs;
|
private PackManager? _packs;
|
||||||
|
|
@ -28,13 +28,7 @@ public partial class ChunkManager : Node2D
|
||||||
_packs = GetNodeOrNull<PackManager>(PackManagerPath);
|
_packs = GetNodeOrNull<PackManager>(PackManagerPath);
|
||||||
if (_packs == null)
|
if (_packs == null)
|
||||||
{
|
{
|
||||||
// fallback: search up the tree for PackManager
|
GD.PrintErr("ChunkManager: PackManagerPath not set or invalid.");
|
||||||
_packs = GetParent()?.GetNodeOrNull<PackManager>("PackManager");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_packs == null)
|
|
||||||
{
|
|
||||||
GD.PrintErr("ChunkManager: PackManager not found. Add it to your Main scene.");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,7 +37,7 @@ public partial class ChunkManager : Node2D
|
||||||
|
|
||||||
private void LoadWorldIndex()
|
private void LoadWorldIndex()
|
||||||
{
|
{
|
||||||
var worldPath = _packs.Resolve("data/world/world_main.json");
|
var worldPath = _packs!.Resolve("data/world/world_main.json");
|
||||||
if (worldPath == null)
|
if (worldPath == null)
|
||||||
{
|
{
|
||||||
GD.PrintErr("ChunkManager: Could not resolve data/world/world_main.json from packs.");
|
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);
|
var (cx, cy) = WorldPosToChunk(_player.GlobalPosition, _world.chunk_size_px);
|
||||||
|
|
||||||
|
// Load required chunks
|
||||||
for (int dy = -LoadRadius; dy <= LoadRadius; dy++)
|
for (int dy = -LoadRadius; dy <= LoadRadius; dy++)
|
||||||
for (int dx = -LoadRadius; dx <= LoadRadius; dx++)
|
for (int dx = -LoadRadius; dx <= LoadRadius; dx++)
|
||||||
{
|
{
|
||||||
var key = (cx + dx, cy + dy);
|
EnsureLoaded(cx + dx, cy + dy);
|
||||||
EnsureLoaded(key.x, key.y);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unload chunks that are too far
|
// Unload chunks too far away
|
||||||
var keysToRemove = new List<(int x, int y)>();
|
var toRemove = new List<(int x, int y)>();
|
||||||
foreach (var kv in _loaded)
|
foreach (var kv in _loaded)
|
||||||
{
|
{
|
||||||
var (x, y) = kv.Key;
|
var (x, y) = kv.Key;
|
||||||
if (Math.Abs(x - cx) > LoadRadius || Math.Abs(y - cy) > LoadRadius)
|
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[k].QueueFree();
|
||||||
_loaded.Remove(k);
|
_loaded.Remove(k);
|
||||||
|
|
@ -96,7 +91,6 @@ public partial class ChunkManager : Node2D
|
||||||
if (_loaded.ContainsKey(key)) return;
|
if (_loaded.ContainsKey(key)) return;
|
||||||
if (!_chunkMap.TryGetValue(key, out var relPath)) return;
|
if (!_chunkMap.TryGetValue(key, out var relPath)) return;
|
||||||
|
|
||||||
// Use pack resolve for the chunk file itself
|
|
||||||
var chunkPath = _packs!.Resolve(relPath);
|
var chunkPath = _packs!.Resolve(relPath);
|
||||||
if (chunkPath == null)
|
if (chunkPath == null)
|
||||||
{
|
{
|
||||||
|
|
@ -104,8 +98,6 @@ public partial class ChunkManager : Node2D
|
||||||
return;
|
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);
|
var chunkJson = FileAccess.GetFileAsString(chunkPath);
|
||||||
using var doc = JsonDocument.Parse(chunkJson);
|
using var doc = JsonDocument.Parse(chunkJson);
|
||||||
var biome = doc.RootElement.TryGetProperty("biome", out var b) ? b.GetString() : "biome:unknown";
|
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)
|
private static Node2D CreateDebugChunkNode(int cx, int cy, int chunkSizePx, string biome)
|
||||||
{
|
{
|
||||||
var n = new Node2D();
|
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
|
|
||||||
{
|
{
|
||||||
"biome:forest" => new Color(0.1f, 0.35f, 0.1f, 0.25f),
|
Name = $"Chunk_{cx}_{cy}",
|
||||||
"biome:plains" => new Color(0.35f, 0.35f, 0.1f, 0.25f),
|
Position = new Vector2(cx * chunkSizePx, cy * chunkSizePx)
|
||||||
"biome:town" => new Color(0.25f, 0.25f, 0.25f, 0.25f),
|
};
|
||||||
_ => new Color(0.2f, 0.2f, 0.2f, 0.25f)
|
|
||||||
|
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);
|
n.AddChild(rect);
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue