コード例 #1
0
    /// <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);
                }
            }
        }
    }
コード例 #2
0
    /// <summary>
    /// <para>Checks the reference current chunk, that always has to be build.</para>
    /// <para>If is loading, waits for it to load and then build it.</para>
    /// <para>If it isn't loaded, loads it.</para>
    /// </summary>
    protected virtual void CheckReferenceCurrentChunk()
    {
        Monitor.Enter(ChunksLoadingLock);

        if (!CurrentChunks.ContainsKey(ReferenceChunkPos) && !ChunksLoaded.ContainsKey(ReferenceChunkPos))
        {
            bool isLoading = ChunksLoading.TryGetValue(ReferenceChunkPos, out Chunk referenceChunk);
            Monitor.Exit(ChunksLoadingLock);
            if (isLoading)
            {
                referenceChunk.ParallelTask.Wait();              // Wait parallel loading to end
                BuildParallelyLoadedChunks();                    // Built the chunk (and the others if are required)
            }
            else
            {
                LoadChunk(ReferenceChunkPos, true);                      // Ignore parallel because is impossible continue playing without this chunk
            }
        }
        else
        {
            Monitor.Exit(ChunksLoadingLock);
        }
    }
コード例 #3
0
    public virtual Chunk GetChunk(Vector2Int terrainPos)
    {
        Vector2Int chunkPos = TerrainPosToChunk(terrainPos);

        return(CurrentChunks.ContainsKey(chunkPos) ? CurrentChunks[chunkPos] : null);
    }