Guest User

Untitled

a guest
Apr 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.46 KB | None | 0 0
  1. class LevelLoader
  2.     {
  3.         Texture2D tileSheet; //The sprite sheet that holds all the tiles used to make the levels
  4.  
  5.         //Rectangles for all different tiles so it makes it easier to switch them
  6.         Rectangle skyTile; //Tile for the sky
  7.         Rectangle grassTile; //Tile for the grass
  8.         Rectangle stoneTile; //Tile for the stone
  9.  
  10.         Camera camera = new Camera();
  11.         Player player;
  12.         int cameraX;
  13.  
  14.         int[] currentLevel; //This array will be used as an array to hold the current level that is played
  15.  
  16.         //The level array of level one
  17.         int[] level1 = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  18.                          1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  19.                          1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  20.                          1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  21.                          1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  22.                          1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  23.                          1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  24.                          1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  25.                          1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 1, 1, 1,
  26.                          1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  27.                          1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1,
  28.                          1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  29.                          1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  30.                          1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  31.                          2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
  32.  
  33.         public LevelLoader()
  34.         {
  35.             //The skyTile that will be used to draw the sky
  36.             skyTile.X = 0; //The x cordinate to draw the tile from
  37.             skyTile.Y = 0; //The y cordinate to draw the tile from
  38.             skyTile.Width = 32; //Width of the tile
  39.             skyTile.Height = 32; //Height of the tile
  40.  
  41.             //The grassTile that will be used to draw the grass
  42.             grassTile.X = 0; //The x cordinate to draw the tile from
  43.             grassTile.Y = 32; //The y cordinate to draw the tile from
  44.             grassTile.Width = 32; //Width of the tile
  45.             grassTile.Height = 32; //Height of the tile
  46.  
  47.             //The stoneTile that will be used to draw stones
  48.             stoneTile.X = 0; //The x cordinate to draw the tile from
  49.             stoneTile.Y = 64; //The y cordinate to draw the tile from
  50.             stoneTile.Width = 32; //Width of the tile
  51.             stoneTile.Height = 32; //Height of the tile
  52.         }
  53.  
  54.         public void LoadContent(ContentManager Content, string filename)
  55.         {
  56.             //Loading the tile sheet that will be used for all the level sprites
  57.             tileSheet = Content.Load<Texture2D>(filename);
  58.         }
  59.  
  60.        
  61.         public void setLevel(int level)
  62.         {
  63.             //See what level that should be set
  64.             switch (level)
  65.             {
  66.                 case 1:
  67.                     currentLevel = level1;
  68.                     break;
  69.             }
  70.         }
  71.  
  72.  
  73.         public void Draw(SpriteBatch spriteBatch)
  74.         {
  75.            
  76.  
  77.             //Its here i need the value from Player class!
  78.  
  79.             int currentX = cameraX; //The current x cordiate to draw the tile too
  80.             int currentY = 0; //The current y cordiate to draw the tile too
  81.             for (int i = 0; i < currentLevel.Length; i++)
  82.             {
  83.                 /*
  84.                  * A switch statement to see if the current level1[i] is either a grass, sky or stone tile
  85.                  */
  86.                 switch(currentLevel[i])
  87.                 {
  88.                     case 1:
  89.                         //Draw a sky tile
  90.                         spriteBatch.Draw(tileSheet, new Rectangle(currentX, currentY, 32, 32), skyTile, Color.White);
  91.                         break;
  92.  
  93.                     case 2:
  94.                         //Draw a grass tile
  95.                         spriteBatch.Draw(tileSheet, new Rectangle(currentX, currentY, 32, 32), grassTile, Color.White);
  96.                         break;
  97.  
  98.                     case 3:
  99.                         //Draw a stone tile
  100.                         spriteBatch.Draw(tileSheet, new Rectangle(currentX, currentY, 32, 32), stoneTile, Color.White);
  101.                         break;
  102.                 }
  103.  
  104.                 //Add 32 to the current cordinate so we don't draw to the same spot all the time
  105.                 currentX = currentX + 32;
  106.                 if (currentX >= 800) //If we hit the width of the map we want:
  107.                 {
  108.                     //Start drawing from X cordinate zero
  109.                     cameraX = camera.getCameraX();
  110.                     currentX = cameraX;
  111.                     //And Y cordinate + 32
  112.                     currentY = currentY + 32;
  113.                     //This will make so we draw one line and when we hit a full line on the map then we draw next line and soo on.
  114.                 }
  115.             }
  116.         }
  117.     }
  118. }
Add Comment
Please, Sign In to add comment