Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace Foregunners
- {
- public static class Loader
- {
- public static void SaveMap(string path, Tile[,,] tiles)
- {
- FileStream stream = new FileStream(path, FileMode.Create);
- BinaryWriter writer = new BinaryWriter(stream);
- // All binary files used in Foregunners will begin
- // with three characters denoting their type
- char[] signature = "MAP".ToCharArray();
- writer.Write(signature);
- // Write the version number to allow conditional
- // flow loading of older maps if encoding changes
- byte version = 1;
- writer.Write(version);
- // Tiles will be written as a 1d array, so
- // note the dimensions to reconstruct the level
- byte[] dimensions = new byte[3]
- {
- (byte)tiles.GetLength(0),
- (byte)tiles.GetLength(1),
- (byte)tiles.GetLength(2)
- };
- writer.Write(dimensions);
- // write all tiles
- for (int z = 0; z < dimensions[2]; z++)
- for (int y = 0; y < dimensions[1]; y++)
- for (int x = 0; x < dimensions[0]; x++)
- {
- writer.Write(tiles[x, y, z].Vertices);
- writer.Write(tiles[x, y, z].ByteStyle);
- }
- // fin
- writer.Close();
- }
- public static Tile[,,] LoadMap(string path)
- {
- // Open the specified file
- FileStream stream = new FileStream(path, FileMode.Open);
- BinaryReader reader = new BinaryReader(stream);
- // Check the three-character ID tag
- string signature = new string (reader.ReadChars(3));
- Console.WriteLine(signature);
- if (signature != "MAP")
- throw new InvalidDataException("Non-map header read!");
- // Check the version, only using v1 so far
- byte version = reader.ReadByte();
- if (version == 1)
- {
- byte width = reader.ReadByte();
- byte height = reader.ReadByte();
- byte depth = reader.ReadByte();
- Tile[,,] tiles = new Tile[width, height, depth];
- for (int z = 0; z < depth; z++)
- for (int y = 0; y < height; y++)
- for (int x = 0; x < width; x++)
- {
- byte coll = reader.ReadByte();
- byte style = reader.ReadByte();
- tiles[x, y, z] = new Tile(coll, style, x, y, z);
- }
- return tiles;
- }
- else
- throw new Exception("Unsupported version number");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement