Advertisement
Guest User

Untitled

a guest
Apr 4th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8. using System.Runtime.InteropServices;
  9.  
  10. namespace IcyTD
  11. {
  12.     class Level
  13.     {
  14.     // Funktionierend
  15.     int[,] map = new int[,]
  16.     {
  17.             {0,0,1,0,0,0,0,0,},
  18.             {0,0,1,1,0,0,0,0,},
  19.             {0,0,0,1,1,0,0,0,},
  20.         {0,0,0,0,1,0,0,0,},
  21.             {0,0,0,1,1,0,0,0,},
  22.             {0,0,1,1,0,0,0,0,},
  23.             {0,0,1,0,0,0,0,0,},
  24.             {0,0,1,1,1,1,1,1,},
  25.     };
  26.    
  27.         /* Meine Methode mit Fehler:
  28.     int[,] iMap;
  29.  
  30.         public Level(int iLevel)
  31.         {
  32.             iMap = ReadLevel(iLevel);
  33.         }*/
  34.  
  35.         public List<Texture2D> tileTextures = new List<Texture2D>();
  36.  
  37.         public void AddTexture(Texture2D texture)
  38.         {
  39.             tileTextures.Add(texture);
  40.         }
  41.  
  42.         public int Width
  43.         {
  44.             get { return iMap.GetLength(1); }
  45.         }
  46.  
  47.         public int Height
  48.         {
  49.             get { return iMap.GetLength(0); }
  50.         }
  51.  
  52.         public void Draw(SpriteBatch batch)
  53.         {
  54.             for (int x = 0; x < Width; x++)
  55.             {
  56.                 for (int y = 0; y < Height; y++)
  57.                 {
  58.                     int textureIndex = iMap[y, x];
  59.  
  60.                     if (textureIndex == -1) continue;
  61.  
  62.                     Texture2D texture = tileTextures[textureIndex];
  63.  
  64.                     batch.Draw(texture, new Rectangle(x * 16, y * 16, 16, 16), Color.White);
  65.                 }
  66.             }
  67.         }
  68.  
  69.         private int[,] ReadLevel(int iLevel)
  70.         {
  71.             string iFileName = "Level\\" + iLevel.ToString() + ".txt";
  72.             string[] sTempStringArray;
  73.             string[,] sTempMap;
  74.             int[,] iTempMap;
  75.  
  76.             List<String> lTempList = new List<String>();
  77.             String lLines = String.Empty;
  78.             StreamReader lStreamReader = null;
  79.             if (File.Exists(iFileName))
  80.             {
  81.                 lStreamReader = new StreamReader(iFileName, Encoding.UTF7);
  82.                 while ((lLines = lStreamReader.ReadLine()) != null)
  83.                 {
  84.                     lTempList.Add(lLines);
  85.                 }
  86.             }
  87.             lStreamReader.Close();
  88.  
  89.             sTempMap = new string[lTempList.Count, (lTempList[0].Length - 1) / 2 + 1];
  90.             iTempMap = new int[lTempList.Count, (lTempList[0].Length - 1) / 2 + 1];
  91.  
  92.             for (int i = 0; i < lTempList.Count; i++)
  93.             {
  94.                 sTempStringArray = lTempList[i].Split(',');
  95.                 for (int j = 0; j < sTempStringArray.Length; j++)
  96.                 {
  97.                     sTempMap[i, j] = sTempStringArray[j];
  98.                 }
  99.             }
  100.  
  101.             for (int i = 0; i < lTempList.Count; i++)
  102.             {
  103.                 for (int j = 0; j < (lTempList[0].Length - 1) / 2 + 1; j++)
  104.                 {
  105.                     iTempMap[i, j] = Convert.ToInt32(sTempMap[i, j]);
  106.                 }
  107.             }
  108.            
  109.             return iTempMap;
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement