Guest User

LevelLoading Class

a guest
Apr 6th, 2012
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1. /*
  2. Example based on http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx
  3. Consider we have a level in this format, 5x5 tiles:
  4. 0,1,0,0,0
  5. 0,1,0,0,0
  6. 0,1,0,0,0
  7. 0,1,1,1,1
  8. 0,0,0,0,0
  9. */
  10. using System;
  11. using System.IO;
  12. using Microsoft.Xna.Framework
  13. using Microsoft.Xna.Framework.Graphics
  14.  
  15. class Tile
  16. {
  17.     public Vector2 Position { get; private set; }
  18.     public Texture2D Texture { get; private set; }
  19.    
  20.     public Tile(Vector2 vec, Texture2D tex)
  21.     {
  22.         position = vec;
  23.         texture = tex;
  24.     }
  25. }
  26.  
  27. class Level
  28. {
  29.     //These will be explained as they are used
  30.     int xCounter, yCounter, levelWidth, levelHeight;
  31.    
  32.     //This holds all our tiles to be drawn later
  33.     List<Tile> Tiles;
  34.    
  35.     public void Level()
  36.     {
  37.         xCounter = 0;
  38.         yCounter = 0;
  39.         levelWidth = 64;
  40.         levelHeight = 64;
  41.        
  42.         Tiles = new List<Vector2>();
  43.     }
  44.    
  45.     public void LoadLevel(ContentManager Content)
  46.     {
  47.         try
  48.         {
  49.             // Create an instance of StreamReader to read from a file.
  50.             // The using statement also closes the StreamReader.
  51.             using (StreamReader sr = new StreamReader("Level01.txt"))
  52.             {
  53.                 String line;
  54.                 // Read and display lines from the file until the end of
  55.                 // the file is reached.
  56.                 while ((line = sr.ReadLine()) != null)
  57.                 {
  58.                     string[] tempTiles = line.Split(',');   //Split the line into individual tiles seperated by commas
  59.                    
  60.                     //Now we loop through each string in the array to get the first row of tiles
  61.                     foreach(string s in tempTiles)
  62.                     {
  63.                         //if we multiple the x value by the width then we get the current tiles x position in world, same with y.
  64.                         //The Tile class also takes a Texture for drawing
  65.                        
  66.                         //if the current string reads as 0 then we expect a grass tile
  67.                         if(s == 0)
  68.                         {
  69.                             Tiles.Add(new Tile(new Vector2(x * levelWidth, y * levelHeight), Content.Load<Texture2D>("grass")));
  70.                         }
  71.                        
  72.                         //if the current string reads as 1 then we expect a dirt tile
  73.                         if(s == 1)
  74.                         {
  75.                             Tiles.Add(new Tile(new Vector2(x * levelWidth, y * levelHeight), Content.Load<Texture2D>("dirt")));
  76.                         }
  77.                         xCounter++; //Increment x so we can get the correct world position
  78.                     }
  79.                 }
  80.                 xCounter = 0;   //Reset x as we are moving to the next line
  81.                 yCounter++;     //Incremement y to get the correct world position
  82.             }
  83.             xCounter = 0;   //Reset what we've used here, just in case.
  84.             yCounter = 0;   //Reset what we've used here, just in case.
  85.         }
  86.         catch (Exception e)
  87.         {
  88.             // Let the user know what went wrong.
  89.             Console.WriteLine("The file could not be read:");
  90.             Console.WriteLine(e.Message);
  91.         }
  92.     }
  93.    
  94.     public void Draw(SpriteBatch spriteBatch)
  95.     {
  96.         spriteBatch.Begin();
  97.        
  98.         foreach(Tile t in Tiles)
  99.         {
  100.             spriteBatch.Draw(t.Texture, t.Position, Color.White);
  101.         }
  102.        
  103.         spriteBatch.End();
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment