Esempio n. 1
0
        private TerrainChunk GetOrCreateChunkByKey(ChunkKey key)
        {
            if (chunks.TryGetValue(key, out var existingChunk))
            {
                return(existingChunk);
            }

            var chunk = chunkPool.GetItem(PoolCallbackCreateNewChunk, PoolCallbackResetChunk);

            chunk.position = (float3)key.origin * TerrainChunk.CHUNK_SIZE * voxelSize;
            chunk.key      = key;
            chunk.meshObject.gameObject.name    = $"Chunk {key.origin}";
            chunk.meshObject.transform.position = chunk.position;
            chunk.bounds = new Bounds {
                min = chunk.position,
                max = chunk.position + math.float3(chunk.size * chunk.voxelSize)
            };

            chunks.TryAdd(key, chunk);

            var loadedData            = ChunkSaveData.Load("world", key);
            var dataLoadedSuccesfully = false;

            if (loadedData != null)
            {
                dataLoadedSuccesfully = chunk.Load(loadedData);
            }

            if (!dataLoadedSuccesfully)
            {
                chunk.ScheduleGenerateDensitiesJob(noiseSettings);
            }

            // Fix neighbours
            foreach (var side in EnumUtil <CubeSide> .valuePairs)
            {
                var neighbourKey = new ChunkKey {
                    origin = GetNeighbourChunkOrigin(chunk, side.value)
                };
                if (!chunks.TryGetValue(neighbourKey, out var neighbour))
                {
                    continue;
                }

                chunk.SetNeighbour(neighbour, side);
            }

            return(chunk);
        }
Esempio n. 2
0
        public static ChunkSaveData Load(string fileKey, ChunkKey chunkKey)
        {
            binaryFormatter ??= new BinaryFormatter();

            var filePath = GetFilePath(fileKey, chunkKey);

            if (!File.Exists(filePath))
            {
                return(null);
            }

            using var filestream = File.Open(filePath, FileMode.Open, FileAccess.Read);

            try {
                return(binaryFormatter.Deserialize(filestream) as ChunkSaveData);
            }
            catch (Exception) {
                return(null);
            }
        }
Esempio n. 3
0
 public static string GetFilePath(string fileKey, ChunkKey chunkKey)
 {
     return(Path.Combine(dataPath, $"{formatVersion}-{fileKey}-{chunkKey.origin}.bin"));
 }