public void Draw(SpriteBatch spriteBatch, Tileset tileset) { for (int i = 0; i < _height; i++) { for (int j = 0; j < _width; j++) { if (data[i, j] != -1) { NewTile tile = tileset.tiles[data[i, j]]; tile.Draw(spriteBatch, new Vector2(tileset._tileWidth * j, tileset._tileHeight * i)); } } } }
public Tileset(string file_name) { tiles = new Dictionary <int, NewTile>(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(file_name); // <tileset> XmlNode Root = xmlDoc.DocumentElement.SelectSingleNode("/tileset"); _name = Root.Attributes["name"].InnerText; _tileWidth = int.Parse(Root.Attributes["tilewidth"].InnerText); _tileHeight = int.Parse(Root.Attributes["tileheight"].InnerText); _tileCount = int.Parse(Root.Attributes["tilecount"].InnerText); _columns = int.Parse(Root.Attributes["columns"].InnerText); // </tileset> // <image> XmlNode image = xmlDoc.DocumentElement.SelectSingleNode("/tileset/image"); string image_name = image.Attributes["source"].InnerText; string imagePath = Path.GetDirectoryName(file_name) + Path.DirectorySeparatorChar + image_name; int imageWidth = int.Parse(image.Attributes["width"].InnerText); int imageHeight = int.Parse(image.Attributes["height"].InnerText); // </image> Image im = Image.FromFile(imagePath); List <Bitmap> tileImages = GridCrop(im, imageHeight, imageWidth); for (int ID = 0; ID < _tileCount; ID++) { NewTile tile = new NewTile( ID, tileImages[ID] ); tiles.Add(ID, tile); } foreach (XmlNode tileNode in xmlDoc.DocumentElement.SelectNodes("/tileset/tile")) { int tileID = int.Parse(tileNode.Attributes["id"].InnerText); NewTile tile = new NewTile( tileID, tileImages[tileID] ); if (tileNode["objectgroup"] != null) { foreach (XmlNode objectNode in tileNode["objectgroup"].SelectNodes("object")) { Polygon collision = GetCollision(objectNode); if (objectNode["properties"] != null) { foreach (XmlNode property in objectNode["properties"].SelectNodes("property")) { if (property.Attributes["name"].InnerText == "drawOrderGuide" && property.Attributes["value"].InnerText == "true") { collision.drawOrderGuide = true; } } } tile.collisions.Add(collision); } } tiles[tileID] = tile; } }