示例#1
0
        private void LoadRegions()
        {
            var regionFiles = _regionsDirectory.GetFiles();

            foreach (var fileInfo in regionFiles)
            {
                var regionFile = new RegionFile(fileInfo);
                _regionFiles.Add(new Vector2Int(regionFile.X, regionFile.Z), regionFile);
            }

            Debug.Log($"[GameFile] - LoadRegions()\nRegion Files Count: {regionFiles.Length}");
        }
示例#2
0
        public void Save(Chunk chunk)
        {
            var        regionPosition = RegionFile.CalculateRegion(chunk.Position.x, chunk.Position.y);
            RegionFile regionFile;

            if (_regionFiles.TryGetValue(regionPosition, out regionFile))
            {
                regionFile.Save(chunk);
            }
            else
            {
                regionFile = new RegionFile(_regionsDirectory, regionPosition.x, regionPosition.y, chunk);
                _regionFiles.Add(regionPosition, regionFile);
            }
        }
示例#3
0
        public List <Chunk> LoadChunks(IEnumerable <Vector2Int> chunkPositions)
        {
            List <Chunk> chunks = new List <Chunk>();

            foreach (var chunkPosition in chunkPositions)
            {
                var chunk = new Chunk(chunkPosition);
                chunks.Add(chunk);
                chunk.Initialise();

                var regionPosition = RegionFile.CalculateRegion(chunkPosition.x, chunkPosition.y);

                var regionFile = _regionFiles[regionPosition];
                var chunkData  = regionFile.GetChunkData(chunkPosition.x, chunkPosition.y);

                var chunkWorldPosition = Chunk.CalculateWorldPosition(chunkPosition);

                for (int x = 0; x < chunkData.Length; ++x)
                {
                    for (int z = 0; z < chunkData[x].Length; ++z)
                    {
                        int byteOffset = 0;

                        byte tilesetId = chunkData[x][z][byteOffset];
                        ++byteOffset;

                        float moisture = BitConverter.ToSingle(chunkData[x][z], byteOffset);
                        byteOffset += sizeof(float);

                        float elevation = BitConverter.ToSingle(chunkData[x][z], byteOffset);
                        byteOffset += sizeof(float);

                        Tile tile = new Tile(tilesetId, moisture, elevation, new Vector2Int(x, z));
                        chunk.Tiles[x, z] = tile;

                        uint entityId = BitConverter.ToUInt32(chunkData[x][z], byteOffset);

                        if (entityId == 0)
                        {
                            continue;
                        }

                        byteOffset += sizeof(uint);

                        ushort entityType = BitConverter.ToUInt16(chunkData[x][z], byteOffset);
                        byteOffset += sizeof(ushort);

                        EntityCollection.Instance.LoadEntity(
                            entityType,
                            entityId,
                            tile,
                            new Vector2Int(x, z) + chunkWorldPosition,
                            chunkData[x][z],
                            byteOffset);

                        tile.PlaceEntity(entityId);
                    }
                }
            }

            return(chunks);
        }