public MapLayer(int width, int height) { map = new Tile[height, width]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { map[y, x] = new Tile(0, 0, x, y); } } }
/// <summary> /// Loads the level from files created in an external tile editor. /// </summary> /// <param name="fileStream"></param> /// <param name="levelIndex"></param> public virtual void LoadLevel(int numLayers) { List<MapLayer> mapLayers = new List<MapLayer>(); // Holds the layers for the level. List<string> tilelines; // The lines of text that hold tile information. List<string> collisionlines; // The lines of text that hold collision information. List<string> spritelines; // The lines of text that hold sprite information. // Load the text file that has the assigned collisions. string collisionPath = string.Format("Content/Levels/{0}/collision.txt", levelIndex); using (Stream fileStream = TitleContainer.OpenStream(collisionPath)) { collisionlines = new List<string>(); collisionlines = readFile(fileStream); fileStream.Close(); } // Load the text file that has the assigned sprite positions. string spritePath = string.Format("Content/Levels/{0}/sprite.txt", levelIndex); using (Stream fileStream = TitleContainer.OpenStream(spritePath)) { spritelines = new List<string>(); spritelines = readFile(fileStream); fileStream.Close(); } // Loop through every single layer. for (int i = 0; i < numLayers; i++) { // Load the text file that has the assigned tile positions. string levelPath = string.Format("Content/Levels/{0}/{1}.txt", levelIndex, i); using (Stream fileStream = TitleContainer.OpenStream(levelPath)) { tilelines = new List<string>(); tilelines = readFile(fileStream); fileStream.Close(); } // The level is as wide as the number of elements in the line, or the number of commas, and as high as the number of lines. levelHeight = tilelines.Count; // Create a new layer. MapLayer layer = new MapLayer(levelWidth, levelHeight); // Loop over every tile position in the file and set each tile to the layer. for (int y = 0; y < layer.Height; y++) { string[] tileNumbers = tilelines[y].Split(','); string[] collisionNumbers = collisionlines[y].Split(','); string[] spriteNumbers = spritelines[y].Split(','); for (int x = 0; x < layer.Width; x++) { // Use the numbers from the file by converting them to integers. int tileIndex = Convert.ToInt32(tileNumbers[x]); Tile tile = new Tile(tileIndex - 1, i, x, y); layer.SetTile(x, y, tile); // Set the tile collisions and sprites only once. if (i == 0) { // Get the collision number from the file by converting it to an integer and load the collision. int collisionNum = Convert.ToInt32(collisionNumbers[x]); LoadCollisions(layer, x, y, collisionNum); // Get the sprite number from the file by converting it to an integer and load the sprite. int spriteNum = Convert.ToInt32(spriteNumbers[x]); LoadSprites(x, y, spriteNum); levelLayer = layer; } } } mapLayers.Add(layer); } // Create the tilemap with the tilesets and the layers. map = new TileMap(tilesets, mapLayers); }
public void SetTile(int x, int y, int tileIndex, int tileset) { map[y, x] = new Tile(tileIndex, tileset, x, y); }
public void SetTile(int x, int y, Tile tile) { map[y, x] = tile; }
public MapLayer(Tile[,] map) { this.map = (Tile[,])map.Clone(); }