Minor cleanup WorldView.cs

WorldChunkRefactoring
Martin Felis 2023-11-01 12:33:43 +01:00
parent 0160e72eec
commit 0304b635a6
1 changed files with 8 additions and 7 deletions

View File

@ -57,11 +57,13 @@ public class WorldView : Spatial
private PackedScene _hexTile3DScene = GD.Load<PackedScene>("res://scenes/HexTile3D.tscn"); private PackedScene _hexTile3DScene = GD.Load<PackedScene>("res://scenes/HexTile3D.tscn");
private HexGrid HexGrid = new(); private HexGrid HexGrid = new();
public SceneTileChunk(Vector2 chunkIndex, int size) public SceneTileChunk(Vector2 chunkIndex)
{ {
foreach (int i in Enumerable.Range(0, size)) int chunkSize = global::World.ChunkSize;
foreach (int i in Enumerable.Range(0, chunkSize))
{ {
foreach (int j in Enumerable.Range(0, size)) foreach (int j in Enumerable.Range(0, chunkSize))
{ {
HexTile3D tile3D = (HexTile3D)_hexTile3DScene.Instance(); HexTile3D tile3D = (HexTile3D)_hexTile3DScene.Instance();
@ -96,7 +98,7 @@ public class WorldView : Spatial
SceneTileChunk CreateSceneTileChunk(Vector2 chunkIndex) SceneTileChunk CreateSceneTileChunk(Vector2 chunkIndex)
{ {
SceneTileChunk sceneTileChunk = new SceneTileChunk(chunkIndex, global::World.ChunkSize); SceneTileChunk sceneTileChunk = new SceneTileChunk(chunkIndex);
foreach (HexTile3D hexTile3D in sceneTileChunk.TileNodes) foreach (HexTile3D hexTile3D in sceneTileChunk.TileNodes)
{ {
@ -107,14 +109,13 @@ public class WorldView : Spatial
return sceneTileChunk; return sceneTileChunk;
} }
SceneTileChunk RemoveChunkFromScene(Vector2 chunkIndex) SceneTileChunk FindSceneTileChunkAtIndex(Vector2 chunkIndex)
{ {
foreach (Spatial child in GetChildren()) foreach (Spatial child in GetChildren())
{ {
SceneTileChunk sceneTileChunk = child as SceneTileChunk; SceneTileChunk sceneTileChunk = child as SceneTileChunk;
if (sceneTileChunk == null) if (sceneTileChunk == null)
{ {
RemoveChild(child);
continue; continue;
} }
@ -168,7 +169,7 @@ public class WorldView : Spatial
Array<SceneTileChunk> removedChunks = new(); Array<SceneTileChunk> removedChunks = new();
foreach (Vector2 chunkIndex in removedChunkIndices) foreach (Vector2 chunkIndex in removedChunkIndices)
{ {
SceneTileChunk chunk = RemoveChunkFromScene(chunkIndex); SceneTileChunk chunk = FindSceneTileChunkAtIndex(chunkIndex);
if (chunk != null) if (chunk != null)
{ {
removedChunks.Add(chunk); removedChunks.Add(chunk);