[Add] Chunk Limit
This commit is contained in:
@@ -60,7 +60,7 @@ namespace InfiniteWorld
|
||||
}
|
||||
|
||||
lastGeneratedCenter = playerChunk;
|
||||
GenerateAround(playerChunk);
|
||||
UpdateActiveChunks(playerChunk);
|
||||
}
|
||||
|
||||
private void EnsureSceneInfrastructure()
|
||||
@@ -111,6 +111,12 @@ namespace InfiniteWorld
|
||||
return true;
|
||||
}
|
||||
|
||||
private void UpdateActiveChunks(Vector2Int centerChunk)
|
||||
{
|
||||
GenerateAround(centerChunk);
|
||||
UnloadDistantChunks(centerChunk);
|
||||
}
|
||||
|
||||
private void GenerateAround(Vector2Int centerChunk)
|
||||
{
|
||||
for (int y = -generationRadius; y <= generationRadius; y++)
|
||||
@@ -132,6 +138,47 @@ namespace InfiniteWorld
|
||||
}
|
||||
}
|
||||
|
||||
private void UnloadDistantChunks(Vector2Int centerChunk)
|
||||
{
|
||||
List<Vector2Int> coordsToRemove = new List<Vector2Int>();
|
||||
foreach (KeyValuePair<Vector2Int, GeneratedChunk> pair in chunks)
|
||||
{
|
||||
if (IsWithinActiveRadius(pair.Key, centerChunk))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
coordsToRemove.Add(pair.Key);
|
||||
}
|
||||
|
||||
for (int i = 0; i < coordsToRemove.Count; i++)
|
||||
{
|
||||
Vector2Int coord = coordsToRemove[i];
|
||||
if (!chunks.TryGetValue(coord, out GeneratedChunk chunk))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
chunks.Remove(coord);
|
||||
if (chunk.Root != null)
|
||||
{
|
||||
Destroy(chunk.Root.gameObject);
|
||||
}
|
||||
|
||||
RefreshNeighborBorders(coord + Vector2Int.up);
|
||||
RefreshNeighborBorders(coord + Vector2Int.right);
|
||||
RefreshNeighborBorders(coord + Vector2Int.down);
|
||||
RefreshNeighborBorders(coord + Vector2Int.left);
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsWithinActiveRadius(Vector2Int coord, Vector2Int centerChunk)
|
||||
{
|
||||
int dx = Mathf.Abs(coord.x - centerChunk.x);
|
||||
int dy = Mathf.Abs(coord.y - centerChunk.y);
|
||||
return dx <= generationRadius && dy <= generationRadius;
|
||||
}
|
||||
|
||||
private GeneratedChunk CreateChunk(Vector2Int coord)
|
||||
{
|
||||
GameObject chunkObject = new GameObject($"Chunk_{coord.x}_{coord.y}");
|
||||
|
||||
Reference in New Issue
Block a user