public LayerLoader(string filename, TileList tileList) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filename); foreach (XmlNode tileMapNode in xmlDoc.DocumentElement.ChildNodes) // tilesetFile nodes { //TODO Get tileWidth and tileHeight also mapWidth = int.Parse(tileMapNode.Attributes["rows"].Value); mapHeight = int.Parse(tileMapNode.Attributes["columns"].Value); foreach (XmlNode layerNode in tileMapNode.ChildNodes) { MapLayer layer = new MapLayer(mapWidth, mapHeight); //Get data node inside layernode XmlNode dataNode = layerNode.FirstChild; string data = dataNode.InnerXml; //Parse all extra marks from data data = data.Replace("\r\n\t\t\t\t", ""); data = data.Replace("\n\t\t\t", ""); //Parse all , makrs also int counter = 0; // counter for data for (int y = 0; y < mapWidth; y++) { for (int x = 0; x < mapHeight; x++) { // Get data id // int id = int.Parse(data[counter].ToString()); int id = RemoveInt(ref data); if (id != 0) { layer.SetTile(x, y, tileList.GetTile(id)); } counter++; } } layers.Add(layer); } } }
// loads the tiles from xml-file generated by TileMapEditor TODO: Content pipeline public TileLoader(string filename, IEContentManager content) { this.content = content; list = new TileList(); input = new TInput(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filename); //TODO: foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes) // tilesetFile nodes { //TODO Make tilesets if (node.Name == "tiles") // if node is <tiles> { foreach (XmlNode imageTileNode in node.ChildNodes) // <imageTiles> { int id = Int32.Parse(imageTileNode.Attributes["id"].Value); int width = Int32.Parse(imageTileNode.Attributes["width"].Value); int height = Int32.Parse(imageTileNode.Attributes["height"].Value); foreach (XmlNode imageTileNodes in imageTileNode.ChildNodes) { if (imageTileNodes.Name == "image") { string file = imageTileNodes.Attributes["file"].Value; string[] contentName = file.Split('.'); try { file = contentName[0]; } catch { throw new Exception("Invalid picture name at " + file); } texture = content.Load<Texture2D>("Content/images/" + contentName[0]); tile = new Tile(texture,id); list.Add(tile); } //TODO Optimize tilesets if (imageTileNodes.Name == "tilesetImage") { string file = imageTileNodes.Attributes["file"].Value; string[] contentName = file.Split('.'); try { file = contentName[0]; } catch { throw new Exception("Invalid picture name at " + file); } //Loads the whole texture. for optimizing just load the tileset //Once then use the same texture in all tiles texture = content.Load<Texture2D>("Content/images/" + contentName[0]); int x = Int32.Parse(imageTileNodes.Attributes["posX"].Value); int y = Int32.Parse(imageTileNodes.Attributes["posY"].Value); Rectangle tilesetPart = new Rectangle(x, y, width, height); tile = new Tile(texture, id, tilesetPart); tile.IsPartOfTileset = true; list.Add(tile); } } //TODO: Check for bugs //Load nulltiles with collision if (imageTileNode.Name == "nullTile") { foreach (XmlNode imageTileNodes in imageTileNode.ChildNodes) { if (imageTileNodes.Name == "collision") { string collision = imageTileNodes.Attributes["area"].Value; if (collision != "none") { tile = new Tile(CollisionType.Unpassable); } else { tile = new Tile(CollisionType.Passable); } tile.TileID = id; list.Add(tile); } } } } } } }