diff --git a/scenes/World.cs b/scenes/World.cs index 8b96e56..97a7beb 100644 --- a/scenes/World.cs +++ b/scenes/World.cs @@ -2,7 +2,7 @@ using Godot; using System; using System.Collections.Generic; using System.Diagnostics; -using System.Linq; +using Godot.Collections; public class World : Spatial { @@ -25,6 +25,9 @@ public class World : Spatial // [Export] public Vector2 Size = new Vector2(1, 1); // signals + [Signal] + delegate void OnTilesChanged(Array removedChunkIndices, Array addedChunkIndices); + // delegate void OnCoordClicked(Vector2 world_pos); // other members @@ -33,16 +36,16 @@ public class World : Spatial private int[] _previousCenterChunkCoord = { 0, 0 }; private Rect2 _centerChunkRect2 = new Rect2(); private Random _debugColorRandom = new Random(); - private Dictionary, WorldChunk> _cachedWorldChunks; - private List> _activeChunkIndices = new(); - private List> _addedChunkIndices = new(); - private List> _removedChunkIndices = new(); + private Godot.Collections.Dictionary _cachedWorldChunks; + private List _activeChunkIndices = new(); + private List _addedChunkIndices = new(); + private List _removedChunkIndices = new(); public World() { Debug.Assert(ChunkSize % 2 == 0); - _cachedWorldChunks = new Dictionary, WorldChunk>(); + _cachedWorldChunks = new Godot.Collections.Dictionary(); } // Called when the node enters the scene tree for the first time. @@ -58,7 +61,7 @@ public class World : Spatial { if (IsTileCached(xIndex, yIndex)) { - WorldChunk cachedChunk = _cachedWorldChunks[new Tuple(xIndex, yIndex)]; + WorldChunk cachedChunk = _cachedWorldChunks[new Vector2(xIndex, yIndex)]; return cachedChunk; } @@ -67,7 +70,7 @@ public class World : Spatial private bool IsTileCached(int xIndex, int yIndex) { - return _cachedWorldChunks.ContainsKey(new Tuple(xIndex, yIndex)); + return _cachedWorldChunks.ContainsKey(new Vector2(xIndex, yIndex)); } private WorldChunk CreateWorldChunk(int xIndex, int yIndex, Color debugColor) @@ -91,7 +94,7 @@ public class World : Spatial result.DebugColor.a = 0.6f; Chunks.AddChild(result); - Tuple chunkIndex = new Tuple(xIndex, yIndex); + Vector2 chunkIndex = new Vector2(xIndex, yIndex); _cachedWorldChunks.Add(chunkIndex, result); return result; @@ -100,7 +103,7 @@ public class World : Spatial public void UpdateCenterChunkFromPlaneCoord(Vector2 planeCoord) { // mark all chunks as retired - Dictionary, WorldChunk> oldCachedChunks = new(_cachedWorldChunks); + Godot.Collections.Dictionary oldCachedChunks = new(_cachedWorldChunks); // set new center chunk var chunkIndex = GetChunkTupleFromPlaneCoord(planeCoord); @@ -109,22 +112,22 @@ public class World : Spatial _centerChunkRect2 = currentChunk.PlaneRect; // load or create adjacent chunks - _activeChunkIndices = new List>(); - _activeChunkIndices.Add(new Tuple(chunkIndex.Item1 - 1, chunkIndex.Item2 - 1)); - _activeChunkIndices.Add(new Tuple(chunkIndex.Item1, chunkIndex.Item2 - 1)); - _activeChunkIndices.Add(new Tuple(chunkIndex.Item1 + 1, chunkIndex.Item2 - 1)); + _activeChunkIndices = new List(); + _activeChunkIndices.Add(new Vector2(chunkIndex.Item1 - 1, chunkIndex.Item2 - 1)); + _activeChunkIndices.Add(new Vector2(chunkIndex.Item1, chunkIndex.Item2 - 1)); + _activeChunkIndices.Add(new Vector2(chunkIndex.Item1 + 1, chunkIndex.Item2 - 1)); - _activeChunkIndices.Add(new Tuple(chunkIndex.Item1 - 1, chunkIndex.Item2)); - _activeChunkIndices.Add(new Tuple(chunkIndex.Item1, chunkIndex.Item2)); - _activeChunkIndices.Add(new Tuple(chunkIndex.Item1 + 1, chunkIndex.Item2)); + _activeChunkIndices.Add(new Vector2(chunkIndex.Item1 - 1, chunkIndex.Item2)); + _activeChunkIndices.Add(new Vector2(chunkIndex.Item1, chunkIndex.Item2)); + _activeChunkIndices.Add(new Vector2(chunkIndex.Item1 + 1, chunkIndex.Item2)); - _activeChunkIndices.Add(new Tuple(chunkIndex.Item1 - 1, chunkIndex.Item2 + 1)); - _activeChunkIndices.Add(new Tuple(chunkIndex.Item1, chunkIndex.Item2 + 1)); - _activeChunkIndices.Add(new Tuple(chunkIndex.Item1 + 1, chunkIndex.Item2 + 1)); + _activeChunkIndices.Add(new Vector2(chunkIndex.Item1 - 1, chunkIndex.Item2 + 1)); + _activeChunkIndices.Add(new Vector2(chunkIndex.Item1, chunkIndex.Item2 + 1)); + _activeChunkIndices.Add(new Vector2(chunkIndex.Item1 + 1, chunkIndex.Item2 + 1)); - foreach(Tuple activeChunkIndex in _activeChunkIndices) + foreach(Vector2 activeChunkIndex in _activeChunkIndices) { - GetOrCreateWorldChunk(activeChunkIndex.Item1, activeChunkIndex.Item2, new Color(GD.Randf(), GD.Randf(), GD.Randf())); + GetOrCreateWorldChunk((int) activeChunkIndex.x, (int) activeChunkIndex.y, new Color(GD.Randf(), GD.Randf(), GD.Randf())); } // unload retired chunks @@ -147,18 +150,17 @@ public class World : Spatial } } - GD.Print("Removed Chunks " + _removedChunkIndices.Count); - GD.Print("Added Chunks " + _addedChunkIndices.Count); + EmitSignal("OnTilesChanged", _removedChunkIndices.ToArray(), _addedChunkIndices.ToArray()); } - private void RemoveChunk(Tuple cachedChunkKey) + private void RemoveChunk(Vector2 cachedChunkKey) { _cachedWorldChunks.Remove(cachedChunkKey); _removedChunkIndices.Add(cachedChunkKey); foreach (WorldChunk chunk in Chunks.GetChildren()) { - if (chunk.ChunkAddress == new Vector2(cachedChunkKey.Item1, cachedChunkKey.Item2)) + if (chunk.ChunkAddress == new Vector2(cachedChunkKey.x, cachedChunkKey.y)) { chunk.QueueFree(); } diff --git a/scenes/WorldView.cs b/scenes/WorldView.cs index 8149cbc..1e99237 100644 --- a/scenes/WorldView.cs +++ b/scenes/WorldView.cs @@ -1,5 +1,5 @@ using Godot; -using System; +using Godot.Collections; public class WorldView : Spatial { @@ -23,9 +23,18 @@ public class WorldView : Spatial public override void _Ready() { _world = GetNode(World); + + _world.Connect("OnTilesChanged", this, nameof(HandleWorldTileChange)); } public override void _Process(float delta) { } + + private void HandleWorldTileChange(Array removedChunkIndices, Array addedChunkIndices) + { + + GD.Print("Removed Chunks " + removedChunkIndices.Count); + GD.Print("Added Chunks " + addedChunkIndices.Count); + } } \ No newline at end of file