예제 #1
0
    public void LoadCycle()
    {
        int chunkSize = WorldData.CHUNK_SIZE;

        HashSet <Chunk> chunksToLoad = new HashSet <Chunk>();

        Vector3 playerPosition = PlayerInfo.getPosition();
        int     px             = Mathf.FloorToInt(playerPosition.x / chunkSize);
        int     py             = Mathf.FloorToInt(playerPosition.z / chunkSize);

        for (int x = -RENDER_DISTANCE; x < RENDER_DISTANCE + 1; x++)
        {
            for (int y = -RENDER_DISTANCE; y < RENDER_DISTANCE + 1; y++)
            {
                int chunkX = px + x;
                int chunkY = py + y;
                // If the chunk does not exist yet, generate it
                if (!WorldData.ContainsChunk(chunkX, chunkY))
                {
                    Tile[,] tiles = WorldGen.GenerateChunk(chunkX, chunkY);
                    WorldData.GenerateChunk(chunkX, chunkY, tiles);
                    InstantiateChunk(chunkX, chunkY);
                }
                chunksToLoad.Add(WorldData.chunks[chunkX, chunkY]);
            }
        }

        // Only hide the chunks that were loaded but shouldn't be loaded anymore
        loadedChunks.ExceptWith(chunksToLoad);
        HideChunks(loadedChunks);

        // Show all chunks in render distance
        loadedChunks.Clear();
        ShowChunks(chunksToLoad);
    }
예제 #2
0
 public void HideChunk(Chunk chunk)
 {
     //Debug.Log("Contains chunk: " + WorldData.ContainsChunk(chunk) + ", Reference: " + chunk.reference);
     if (WorldData.ContainsChunk(chunk) && chunk.reference != null)
     {
         chunk.reference.gameObject.SetActive(false);
     }
 }
예제 #3
0
 public void ShowChunk(Chunk chunk)
 {
     if (WorldData.ContainsChunk(chunk) && chunk.reference != null)
     {
         chunk.reference.gameObject.SetActive(true);
         loadedChunks.Add(chunk);
     }
 }