Advertisement
7heSama

v1.0 Map Loader

Mar 26th, 2017
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace Foregunners
  5. {
  6.     public static class Loader
  7.     {
  8.         public static void SaveMap(string path, Tile[,,] tiles)
  9.         {
  10.             FileStream stream = new FileStream(path, FileMode.Create);
  11.             BinaryWriter writer = new BinaryWriter(stream);
  12.  
  13.             // All binary files used in Foregunners will begin
  14.             // with three characters denoting their type
  15.             char[] signature = "MAP".ToCharArray();
  16.             writer.Write(signature);
  17.  
  18.             // Write the version number to allow conditional
  19.             // flow loading of older maps if encoding changes
  20.             byte version = 1;
  21.             writer.Write(version);
  22.  
  23.             // Tiles will be written as a 1d array, so
  24.             // note the dimensions to reconstruct the level  
  25.             byte[] dimensions = new byte[3]
  26.             {
  27.                 (byte)tiles.GetLength(0),
  28.                 (byte)tiles.GetLength(1),
  29.                 (byte)tiles.GetLength(2)
  30.             };
  31.             writer.Write(dimensions);
  32.  
  33.             // write all tiles
  34.             for (int z = 0; z < dimensions[2]; z++)
  35.                 for (int y = 0; y < dimensions[1]; y++)
  36.                     for (int x = 0; x < dimensions[0]; x++)
  37.                     {
  38.                         writer.Write(tiles[x, y, z].Vertices);
  39.                         writer.Write(tiles[x, y, z].ByteStyle);
  40.                     }
  41.            
  42.             // fin
  43.             writer.Close();
  44.         }
  45.  
  46.         public static Tile[,,] LoadMap(string path)
  47.         {
  48.             // Open the specified file
  49.             FileStream stream = new FileStream(path, FileMode.Open);
  50.             BinaryReader reader = new BinaryReader(stream);
  51.  
  52.             // Check the three-character ID tag
  53.             string signature = new string (reader.ReadChars(3));
  54.             Console.WriteLine(signature);
  55.            
  56.             if (signature != "MAP")
  57.                 throw new InvalidDataException("Non-map header read!");
  58.  
  59.             // Check the version, only using v1 so far
  60.             byte version = reader.ReadByte();
  61.  
  62.             if (version == 1)
  63.             {
  64.                 byte width = reader.ReadByte();
  65.                 byte height = reader.ReadByte();
  66.                 byte depth = reader.ReadByte();
  67.                
  68.                 Tile[,,] tiles = new Tile[width, height, depth];
  69.  
  70.                 for (int z = 0; z < depth; z++)
  71.                     for (int y = 0; y < height; y++)
  72.                         for (int x = 0; x < width; x++)
  73.                         {
  74.                             byte coll = reader.ReadByte();
  75.                             byte style = reader.ReadByte();
  76.                             tiles[x, y, z] = new Tile(coll, style, x, y, z);
  77.                         }
  78.  
  79.                 return tiles;
  80.             }
  81.             else
  82.                 throw new Exception("Unsupported version number");
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement