/// <summary> /// Updates central chunk to a new chunk, regenerating LoDs and loaded chunks appropriately /// </summary> /// <param name="newCenter">New center chunk</param> public void UpdateCenter(Chunk newCenter) { if (center == newCenter) { return; } var oldCenter = center; center = newCenter; if (!IntCube.WithinCube( loadedCenter, MaxLoadedDistance, newCenter.Coords, MaxViewDistance )) { // Player needs to render chunks that were not yet loaded // Move the IntCube of loaded chunks to fit the player ClearFarChunks(); GenerateNewChunks( new IntCube(loadedCenter, MaxLoadedDistance), new IntCube(newCenter.Coords, MaxLoadedDistance) ); loadedCenter = newCenter.Coords; } UpdateLODs(); }
/// <summary> /// Generates new chunks that will complete oldArea to newArea /// </summary> /// <param name="oldArea">Already existing chunks</param> /// <param name="newArea">Area that should be filled with chunks</param> private void GenerateNewChunks(IntCube oldArea, IntCube newArea) { var diff = newArea.Diff(oldArea); foreach (var coords in diff) { var chunk = GenerateChunk(coords); activeChunks.Add(chunk); } Debug.Log("Generated new chunks: " + diff.Count); ProcessingChunksCount = diff.Count; }
/// <summary> /// Disposes chunks that are too far from player /// (farther then MaxLoadedDistance) /// </summary> private void ClearFarChunks() { int totalDeleted = 0; for (int i = 0; i < activeChunks.Count; i++) { var chunk = activeChunks[i]; if (!IntCube.WithinCube(center.Coords, MaxLoadedDistance, chunk.Coords)) { activeChunks.RemoveAt(i); chunk.Dispose(); --i; ++totalDeleted; } } Debug.Log("Removing far chunks: " + totalDeleted.ToString()); }