/// <summary> /// Loads a .tmx map and converts it to a TileMap. /// </summary> /// <param name="mapName"> The name of the map to load. </param> /// <param name="game"> The game. </param> /// <returns> /// A TileMap based on the .tmx map. /// </returns> public Map LoadTmxMap(string mapName, Game1 game) { // Finds the path to the map. var filename = Path.Combine(Directories.GetMapDirection(), mapName); this.tilesetList = new List<Tileset>(); this.layerList = new List<MapLayer>(); // Creates a new XmlDocument and loads the map in as one such. var document = new XmlDocument(); document.Load(filename); XmlNode mapNode = document["map"]; // Asserts that certain things exists in the XMLdocument. Debug.Assert(mapNode != null, "mapNode != null"); Debug.Assert(mapNode.Attributes != null, "mapNode.Attributes != null"); Debug.Assert(mapNode.FirstChild.Attributes != null, "mapNode.FirstChild.Attributes != null"); // Finds the height and the width of the map. this.mapWidth = int.Parse(mapNode.Attributes["width"].Value, CultureInfo.InvariantCulture); this.mapHeight = int.Parse(mapNode.Attributes["height"].Value, CultureInfo.InvariantCulture); var xmlNodeList = document.SelectNodes("map/tileset"); if (xmlNodeList != null) { // Loops through each node that describes a tileset. foreach (XmlNode tilesetNode in xmlNodeList) { // Asserts that the tilesetNode contains attributes and that the // first child contains attributes. Debug.Assert(tilesetNode.Attributes != null, "tilesetNode.Attributes != null"); Debug.Assert(tilesetNode.FirstChild.Attributes != null, "tilesetNode.FirstChild.Attributes != null"); if (tilesetNode.FirstChild.Attributes["source"] != null) { // Loads the name, texture, height and width of the current tilesetNode. var tilesetName = tilesetNode.Attributes["name"].Value; var tilesetTexture = game.Content.Load<Texture2D>(@"Textures\TileSets\" + tilesetName); var tilesWide = int.Parse(tilesetNode.FirstChild.Attributes["width"].Value) / int.Parse(tilesetNode.Attributes["tilewidth"].Value); var tilesHigh = int.Parse(tilesetNode.FirstChild.Attributes["height"].Value) / int.Parse(tilesetNode.Attributes["tileheight"].Value); // Makes a new tileset from the above variables. var testset = new Tileset( tilesetTexture, tilesWide, tilesHigh, int.Parse(tilesetNode.Attributes["tilewidth"].Value), int.Parse(tilesetNode.Attributes["tileheight"].Value)); // Sets the Firstgid value of the tilset to the value stored in the tilesetNode. testset.SetFirstgid(int.Parse(tilesetNode.Attributes["firstgid"].Value)); // Adds the tileset to the list of tilesets that make up the map. this.tilesetList.Add(testset); } } } var selectNodes = document.SelectNodes("map/layer|map/objectgroup"); if (selectNodes != null) { // Loops through each node that describes a layer in the XMLDocument foreach (XmlNode layerNode in selectNodes) { if (layerNode.Name == "layer") { // Makes a layer from the node. this.MakeLayer(layerNode); } } } // Creates and returns a TileMap based on the list of tilesets and maplayers. return new Map(this.tilesetList, this.layerList); }
/// <summary> /// Initializes a new instance of the <see cref="Map"/> class. /// </summary> /// <param name="tileset"> The tileset to create the map from. </param> /// <param name="layer"> The layer to create the map from. </param> public Map(Tileset tileset, MapLayer layer) { this.tilesets = new List<Tileset>(); this.tilesets.Add(tileset); this.mapLayers = new List<MapLayer>(); this.mapLayers.Add(layer); mapWidth = this.mapLayers[0].Width; mapHeight = this.mapLayers[0].Height; this.dataMap = new MapData(this.MapHeight, this.MapWidth); this.dataMap.AddCollision(this.mapLayers, this.tilesets); }
/// <summary> /// Makes a layer for the map from the given input. /// </summary> /// <param name="lines"> The lines that holds the data for the map. </param> /// <param name="start"> The line to start reading from. </param> /// <param name="height"> The height of the map. </param> /// <param name="width"> The width of the map. </param> /// <param name="gameRef"> A snapshot of the game. </param> private void MakeLayer(string[] lines, int start, int height, int width, Game1 gameRef) { // Finds the name of the tileset used to make this specific layer and loads it. var tilesetName = lines[start + 2].Substring(lines[start + 2].IndexOf('=') + 1); var tilesetTexture = gameRef.Content.Load<Texture2D>(@"Textures\TileSets\" + tilesetName); var backgroundTileset = new Tileset( tilesetTexture, tilesetTexture.Width / 32, tilesetTexture.Height / 32, 32, 32); this.tilesets.Add(backgroundTileset); var index = lines[start + 1].IndexOf('=') + 1; var layerName = lines[start + 1].Substring(index, lines[start + 1].Length - index); var background = new MapLayer(layerName, width, height); // Goes through all the lines that hold tileIDs for (int i = 0; i < height; i++) { // Loads the current line and splits it into a string array. string[] stringTileIDs = lines[i + start + 4].Split(','); var tileIDs = new int[width]; // Converts each ID from a string to an int. for (int j = 0; j < width; j++) { tileIDs[j] = Convert.ToInt32(stringTileIDs[j]); } // Loops through the list of TileIDs and places new tiles in the layer, corrosponding // to the ID and position. for (int j = 0; j < width; j++) { background.SetTile(i, j, new Tile(tileIDs[j] - 1 < 0 ? 0 : tileIDs[j] - 1, this.tilesets.Count - 1)); } } this.layers.Add(background); }