Esempio n. 1
0
        ///<summary>
        ///
        ///<summary>
        private void FindChunksToLoad()
        {
            // Get the position of this gameobject to generate around
            ChunkPos playerChunkPos = ChunkPos.FromWorldPos(transform.position);

            if (buildList.Count == 0)
            {
                // Cycle through the neighbouring chunks
                for (int i = 0; i < neighbourOrder.Length; i++)
                {
                    ChunkPos chunkPos = new ChunkPos(
                        neighbourOrder[i].x + playerChunkPos.x,
                        playerChunkPos.y,
                        neighbourOrder[i].z + playerChunkPos.z
                        );

                    Chunk chunk = plane.GetChunk(chunkPos.x, chunkPos.y, chunkPos.z);
                    // If the chunk already exists and it's already rendered or in queue - continue
                    if (chunk != null && (chunk.rendered || updateList.Contains(chunkPos)))
                    {
                        continue;
                    }
                    // Render a column of chunks, 4 below and 4 above.
                    for (int y = -4; y < 4; y++)
                    {
                        buildList.Add(new ChunkPos(chunkPos.x, chunkPos.y + y, chunkPos.z));
                    }
                    return;
                }
            }
        }
Esempio n. 2
0
        private void GarbageCollectChunks()
        {
            if (garbageCollectionTimer == garbageCollectionPeriod)
            {
                List <ChunkPos> toDelete       = new List <ChunkPos>();
                ChunkPos        playerChunkPos = ChunkPos.FromWorldPos(transform.position);
                foreach (var chunk in plane.chunks)
                {
                    int dx = Mathf.Abs(playerChunkPos.x - chunk.Value.chunkPos.x);
                    int dy = Mathf.Abs(playerChunkPos.y - chunk.Value.chunkPos.y);
                    int dz = Mathf.Abs(playerChunkPos.z - chunk.Value.chunkPos.z);
                    if (dx >= garbageCollectionDistance ||
                        dy >= garbageCollectionDistance ||
                        dz >= garbageCollectionDistance)
                    {
                        toDelete.Add(chunk.Key);
                    }
                }

                foreach (var chunkPos in toDelete)
                {
                    plane.DestroyChunk(chunkPos.x, chunkPos.y, chunkPos.z);
                }
                garbageCollectionTimer = 0;
            }
            else
            {
                garbageCollectionTimer++;
            }
        }