public static TileLayer FromFile(ContentManager content, string file) { bool readingLayout = false; bool readingTextures = false; List<String> textureNames = new List<string>(); List<List<int>> tempLayout = new List<List<int>>(); using (StreamReader reader = new StreamReader(file)) { while (!reader.EndOfStream) { string line = reader.ReadLine().Trim(); if (line.Contains("[Textures]")) { readingTextures = true; readingLayout = false; } else if (line.Contains("[Layout]")) { readingTextures = false; readingLayout = true; } else if (readingLayout) { List<int> row = new List<int>(); string[] cells = line.Split(' '); foreach (string c in cells) { if (!string.IsNullOrEmpty(c)) row.Add(int.Parse(c)); } tempLayout.Add(row); } else if (readingTextures) { textureNames.Add(line); } } } int width = tempLayout[0].Count; int height = tempLayout.Count; TileLayer tileLayer = new TileLayer(width,height); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { tileLayer.setCellIndex(x,y, tempLayout[y][x]); } } tileLayer.loadTextures(content, textureNames.ToArray()); return tileLayer; }
public static TileLayer FromFile(ContentManager content, string file) { bool readingLayout = false; bool readingTextures = false; List <String> textureNames = new List <string>(); List <List <int> > tempLayout = new List <List <int> >(); using (StreamReader reader = new StreamReader(file)) { while (!reader.EndOfStream) { string line = reader.ReadLine().Trim(); if (line.Contains("[Textures]")) { readingTextures = true; readingLayout = false; } else if (line.Contains("[Layout]")) { readingTextures = false; readingLayout = true; } else if (readingLayout) { List <int> row = new List <int>(); string[] cells = line.Split(' '); foreach (string c in cells) { if (!string.IsNullOrEmpty(c)) { row.Add(int.Parse(c)); } } tempLayout.Add(row); } else if (readingTextures) { textureNames.Add(line); } } } int width = tempLayout[0].Count; int height = tempLayout.Count; TileLayer tileLayer = new TileLayer(width, height); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { tileLayer.setCellIndex(x, y, tempLayout[y][x]); } } tileLayer.loadTextures(content, textureNames.ToArray()); return(tileLayer); }