Beispiel #1
0
        public void Serialize(WorldData world)
        {
            WorldHeader header = world.GetHeader();

            string dir = Path.Combine(Config.WorldSaveDirectory, header.name);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            fileStream = new FileStream(Path.Combine(dir, header.name + fileExtension), FileMode.Create, FileAccess.Write);
            writer     = new BinaryWriter(fileStream);
            int[] sectionPointers = new int[numSections];

            sectionPointers[0] = BlockOutSectionPointer(world);
            sectionPointers[1] = SerializeHeader(world);
            sectionPointers[2] = SerializeChunks(world);
            SerializeSectionPointers(world, sectionPointers);

            writer.Close();
            fileStream.Close();
        }
Beispiel #2
0
        private int SerializeChunks(WorldData world)
        {
            byte b;                   //represents 8 booleans compacted into a single byte

            byte[]         tileData;  //byte array to hold tile data
            PersistentTile tile;      //Cached version of the current tile
            int            byteIndex; //Current index into the tileData array

            var chunks = world.GetChunks();

            writer.Write(chunks.Length);

            for (int i = 0; i < chunks.Length; i++)
            {
                var tiles = chunks[i].GetTiles();
                for (int j = 0; j < tiles.Length; j++)
                {
                    b                   = 0;
                    byteIndex           = 1;
                    tile                = tiles[j];
                    tileData            = new byte[bytesPerTile];
                    tileData[byteIndex] = (byte)tile.GroundTileId;
                    byteIndex++;
                    if (tile.ObjectTileId > 0)
                    {
                        b |= (byte)Masks.WallPresent;
                        tileData[byteIndex] = (byte)tile.ObjectTileId;
                        byteIndex++;
                    }
                    tileData[0] = b;
                    writer.Write(tileData, 0, byteIndex);
                }
            }


            return((int)writer.BaseStream.Position);
        }
Beispiel #3
0
 public void GenerateEmptyWorld(WorldHeader header)
 {
     // used for client in networked games. Generates an empty world based on the given WorldHeader
     worldData = new WorldData(header);
 }
Beispiel #4
0
 public void LoadWorld(string worldName)
 {
     worldData = serializer.Deserialize(worldName);
 }