/// <summary> /// Constructor initializes tile array and tile textures. /// </summary> /// <param name="tile1Texture">Texture for floor tile.</param> /// <param name="tile2Texture">Texture for blocked/wall tile.</param> public Map(Texture2D tile1Texture, Texture2D tile2Texture) { tiles = new Tile[gridSize, gridSize]; for (int i = 0; i < gridSize; i++) for (int j = 0; j < gridSize; j++) tiles[i,j] = new Tile(); pathfinder = null; this.tile1Texture = tile1Texture; this.tile2Texture = tile2Texture; tileDimension = tile1Texture.Width; }
/// <summary> /// Loads a map from a given text file. /// </summary> /// <param name="path">File path of the map file to load.</param> public void Loadmap(string path) { string[] sections = path.Split('/'); name = sections[sections.Length-1]; name.TrimEnd(new char[] { '.', 'm', 'a', 'p' }); List<string> lines = new List<string>(); using (StreamReader reader = new StreamReader(path)) { // Read in the size of the map string line = reader.ReadLine(); gridSize = line.Length; tiles = new Tile[gridSize, gridSize]; for (int i = 0; i < gridSize; i++) for (int j = 0; j < gridSize; j++) tiles[i, j] = new Tile(); Debug.Assert(line.Length == gridSize, String.Format("loaded map string line width must be {0}.", gridSize)); while (line != null) { lines.Add(line); line = reader.ReadLine(); } } Debug.Assert(lines.Count == gridSize, String.Format("loaded map string must have {0} lines.",gridSize)); // Loop over every tile position, for (int y = 0; y < gridSize; ++y) { for (int x = 0; x < gridSize; ++x) { // to load each tile. char tileType = lines[y][x]; if (tileType == '.') tiles[x, y] = new Tile(false); else tiles[x, y] = new Tile(true); } } }