xemerau

Loading Map Issues

Jun 20th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.25 KB | None | 0 0
  1.  //Load Map
  2.         public void LoadMap(string path)
  3.         {
  4.             string fileContents = string.Empty;
  5.             try
  6.             {
  7.                 using (StreamReader sr = new StreamReader(File.OpenRead(path)))
  8.                 {
  9.                     fileContents = sr.ReadToEnd().ToString();
  10.                     Console.WriteLine(fileContents);          //file loaded correctly as the console output was correct
  11.                 }
  12.             }
  13.             catch (Exception e)
  14.             {
  15.                 Console.WriteLine("There was a problem loading the map");
  16.                 Console.WriteLine(e.Message);
  17.             }
  18.  
  19.             //Splitting the file contents into parts to add to the array
  20.             string[] parts = fileContents.Split(new string[] { "\r\n" }, StringSplitOptions.None);
  21.             for (int i = 0; i < parts.Length; i++)
  22.             {
  23.                 if (i == 0)
  24.                 {
  25.                     //Set Width
  26.                     tilesWide = Int16.Parse(parts[i]);
  27.                 }
  28.  
  29.                 else if (i == 1)
  30.                 {
  31.                     //Set Height
  32.                     tilesHigh = Int16.Parse(parts[i]);
  33.  
  34.                     //Declaring the identifier array using width and height
  35.                     TileMapIdentifiers = new int[tilesWide, tilesHigh];
  36.                 }
  37.  
  38.                 if (i > 1)
  39.                 {
  40.                     //Loop through values and assign as needed
  41.                     string[] tiles = parts[i].Split(new string[] { "," }, StringSplitOptions.None);
  42.                    
  43.                     for (int j = 0; j < tiles.Length; j++)
  44.                     {
  45.                         TileMapIdentifiers[i - 2, j] = Int16.Parse(tiles[j]);
  46.                     }
  47.                 }
  48.             }
  49.             //Declaring the tile array using the width and height
  50.             TileMapArray = new Tile[tilesWide, tilesHigh];
  51.  
  52.  
  53.             //Using the identifiers to declare new tiles
  54.             for(int i = 0; i < tilesWide; i++)
  55.             {
  56.                 for (int j = 0; j < tilesHigh; j++)
  57.                 {
  58.                     TileMapArray[i,j] = new Tile(new Vector2(i*64, j*64), tileTexture, TileMapIdentifiers[i,j]);
  59.                 }
  60.             }
  61.         }
Advertisement
Add Comment
Please, Sign In to add comment