Ejemplo n.º 1
0
        private void HandleRemovalOfChunk(TerrainChunk chunk, float3 playerPosition)
        {
            if (chunk.hasBeenMarkedForRemoval)
            {
                return;
            }

            var isTooFarAway = math.distance(playerPosition, chunk.position) > chunkDrawDistance * 1.5 * TerrainChunk.CHUNK_SIZE;
            var isEmpty      = !chunk.hasSignChangeOnAnySide && !ShouldKeepEmptyChunks;

            if (!isTooFarAway && !isEmpty)
            {
                return;
            }

            // Tell the neighbours this chunk no longer exists
            foreach (var(sideIndex, side) in EnumUtil <CubeSide> .valuePairs)
            {
                if (chunk.hasNeighbourOnSide[sideIndex])
                {
                    var neighbourKey     = chunk.neighbourKeys[sideIndex];
                    var neighbour        = chunks[neighbourKey];
                    var flippedSideIndex = (int)side.Flip();

                    chunk.ClearNeighbour(sideIndex);
                    neighbour.ClearNeighbour(flippedSideIndex);
                }
            }

            if (chunk.hasSignChangeOnAnySide)
            {
                chunksWithSignChange--;
            }

            chunk.hasBeenMarkedForRemoval = true;
            chunk.CompleteJobs();
            chunk.meshObject.gameObject.SetActive(false);

            if (chunk.hasBeenDeformed)
            {
                var saveData = new ChunkSaveData {
                    fileKey  = "world",
                    chunkKey = chunk.key,
                };

                chunk.Save(saveData);
                ChunkSaveData.Save(saveData);
            }

            chunks.TryRemove(chunk.key, out _);
            chunkPool.AddItem(chunk);
        }