/// <summary> /// <para>Notify the chunk loading. If is a parallel execution, is added to the ChunksLoaded list in order to be build at BuildParallelyLoadedChunks method.</para> /// <para>If the chunk is for map, it is added to ChunksForMap list.</para> /// <para>Otherwise, the chunk is build.</para> /// <para>IMPORTANT: If is a parallel execution, this method can't instanciate any child of GameObject.</para> /// </summary> /// <param name="chunk">Chunk instance.</param> /// <param name="isParellel">If the chunk is loading in parallel.</param> /// <param name="isForMap">If the chunk is loading for the terrain mapping.</param> protected virtual void ChunkLoaded(Chunk chunk, bool isParallel, bool isForMap) { if (isParallel) { Monitor.Enter(ChunksLoadingLock); } if (isForMap) { ChunksLoadingForMap.Remove(chunk.Position); ChunksForMap.Add(chunk.Position, chunk); } else if (isParallel) { ChunksLoading.Remove(chunk.Position); ChunksLoaded.Add(chunk.Position, chunk); } if (isParallel) { Monitor.Exit(ChunksLoadingLock); } else if (!isForMap) { BuildChunk(chunk); CurrentChunks.Add(chunk.Position, chunk); } }
/// <summary> /// Equivalent to DynamicChunksUpdate but for terrain mapping. Loads the chunks required for the map and unloads the unrequired ones. /// </summary> /// <param name="bottomLeftPos">Bottom left corner of the map.</param> /// <param name="topRightPos">Top right corner of the map.</param> /// <param name="inMaxUpdateTime">Function from the mapping class that checks if the corresponding MaxUpdateTime is exceeded.</param> public virtual void UpdateChunksForMap(Vector2Int bottomLeftPos, Vector2Int topRightPos, System.Func <bool> inMaxUpdateTime) { Vector2Int bottomLeftChunkPos = TerrainPosToChunk(bottomLeftPos); Vector2Int topRightChunkPos = TerrainPosToChunk(topRightPos); Vector2Int mapSize = topRightChunkPos - bottomLeftChunkPos; // Destroy the don't required chunks List <Vector2Int> chunksToDestroy = new List <Vector2Int>(); foreach (KeyValuePair <Vector2Int, Chunk> entry in ChunksForMap) { if (entry.Key.x < bottomLeftChunkPos.x || entry.Key.x > topRightChunkPos.x || entry.Key.y < bottomLeftChunkPos.y || entry.Key.y > topRightChunkPos.y) { entry.Value.Destroy(); chunksToDestroy.Add(entry.Key); } } foreach (Vector2Int pos in chunksToDestroy) { ChunksForMap.Remove(pos); } // Load the chunks required for map Vector2Int chunkPos = new Vector2Int(); for (int x = 0; x <= mapSize.x; x++) { for (int y = 0; y <= mapSize.y; y++) { chunkPos.x = bottomLeftChunkPos.x + x; chunkPos.y = bottomLeftChunkPos.y + y; // If no loaded neither loading if (!CurrentChunks.ContainsKey(chunkPos) && !ChunksLoaded.ContainsKey(chunkPos) && !ChunksLoading.ContainsKey(chunkPos) && !ChunksForMap.ContainsKey(chunkPos) && !ChunksLoadingForMap.ContainsKey(chunkPos)) { LoadChunk(chunkPos, ParallelChunkLoading, true); } } } }
public virtual Cell GetCell(Vector2Int terrainPos, bool isForMap = false) { Cell cell = null; Chunk chunk = GetChunk(terrainPos); // Check ChunksForMap if (chunk == null && isForMap) { Vector2Int chunkPos = TerrainPosToChunk(terrainPos); ChunksForMap.TryGetValue(chunkPos, out chunk); } // Get cell from the chunk if it exists if (chunk != null) { Vector2Int posInChunk = new Vector2Int(LC_Math.Mod(terrainPos.x, ChunkSize), LC_Math.Mod(terrainPos.y, ChunkSize)); cell = chunk.Cells[posInChunk.x, posInChunk.y]; } return(cell); }
/// <summary> /// Destroys all the chunks of the terrain, including all GameObjects and ParallelTasks. /// </summary> public virtual void DestroyTerrain() { IsGenerated = false; foreach (Transform child in transform) { Destroy(child.gameObject); } if (CurrentChunks != null) { foreach (KeyValuePair <Vector2Int, Chunk> entry in CurrentChunks) { entry.Value.Destroy(); } CurrentChunks.Clear(); } if (ChunksForMap != null) { foreach (KeyValuePair <Vector2Int, Chunk> entry in ChunksForMap) { entry.Value.Destroy(); } ChunksForMap.Clear(); } lock ( ChunksLoadingLock ) { if (ChunksLoading != null) { foreach (KeyValuePair <Vector2Int, Chunk> entry in ChunksLoading) { entry.Value.Destroy(); } ChunksLoading.Clear(); } if (ChunksLoaded != null) { foreach (KeyValuePair <Vector2Int, Chunk> entry in ChunksLoaded) { entry.Value.Destroy(); } ChunksLoaded.Clear(); } if (ChunksLoadingForMap != null) { foreach (KeyValuePair <Vector2Int, Chunk> entry in ChunksLoadingForMap) { entry.Value.Destroy(); } ChunksLoadingForMap.Clear(); } } }