public ElementList ToElementList(string layerName) { TMXLayer thisLayer = this.Map.Layers.FirstOrDefault(s => s.Name == layerName); if (thisLayer == null) { throw new Exception("Unable to find layer '" + layerName + "'"); } ElementList newElementList = new ElementList(); MapCellList list = ParseTMXData(thisLayer.Data); int tileX = 0; int tileY = 0; foreach (MapCell cell in list) { if (cell.TileID > 0) { TMXTile tile = GetTileById(cell.TileID); string type = tile.Properties.FirstOrDefault(p => p.Name.ToLower() == "type").Value; if (type == null) { type = "0"; } string state = tile.Properties.FirstOrDefault(p => p.Name.ToLower() == "state").Value; if (state == null) { state = "0"; } Element ele = new Element() { Type = type, State = state, X = (tileX * this.Map.TileWidth), Y = (tileY * this.Map.TileHeight) }; newElementList.Add(ele); } tileX++; if (tileX > this.Map.Width) { tileX = 0; tileY++; } } return(newElementList); }
public Map ToMap(string layerName) { TMXLayer thisLayer = this.Map.Layers.FirstOrDefault(s => s.Name == layerName); if (thisLayer == null) { throw new Exception("Unable to find layer '" + layerName + "'"); } Map newMap = new Map(); newMap.Rows = this.Map.Width; newMap.Columns = this.Map.Height; newMap.Cells = ParseTMXData(thisLayer.Data); return(newMap); }
public static TMXFile Load(string fileName) { TMXFile file = new TMXFile(); using (XmlReader reader = XmlReader.Create(fileName)) { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: switch (reader.Name.ToLower()) { case "map": file.Map.TiledVersion = reader.GetAttribute("tiledversion"); switch (reader.GetAttribute("orientation")) { case "orthogonal": file.Map.Orientation = OrientationType.Orthogonal; break; case "isometric": file.Map.Orientation = OrientationType.Isometric; break; case "staggered": file.Map.Orientation = OrientationType.Staggered; break; case "hexagonal": file.Map.Orientation = OrientationType.Hexagonal; break; default: file.Map.Orientation = OrientationType.Orthogonal; break; } switch (reader.GetAttribute("renderorder")) { case "right-down": file.Map.RenderOrder = RenderOrderType.RightDown; break; case "right-up": file.Map.RenderOrder = RenderOrderType.RightUp; break; case "left-down": file.Map.RenderOrder = RenderOrderType.LeftDown; break; case "left-up": file.Map.RenderOrder = RenderOrderType.LeftUp; break; } file.Map.Width = Int32.Parse(reader.GetAttribute("width")); file.Map.Height = Int32.Parse(reader.GetAttribute("height")); file.Map.TileWidth = Int32.Parse(reader.GetAttribute("tilewidth")); file.Map.TileHeight = Int32.Parse(reader.GetAttribute("tileheight")); file.Map.NextObjectId = Int32.Parse(reader.GetAttribute("nextobjectid")); break; case "tileset": TMXTileset set = new TMXTileset(); set.FirstGid = Int32.Parse(reader.GetAttribute("firstgid")); set.Source = reader.GetAttribute("source"); if (reader.GetAttribute("name") != null) { set.Name = reader.GetAttribute("name"); } if (reader.GetAttribute("tilewidth") != null) { set.TileWidth = Int32.Parse(reader.GetAttribute("tilewidth")); } if (reader.GetAttribute("tileheight") != null) { set.TileHeight = Int32.Parse(reader.GetAttribute("tileheight")); } if (reader.GetAttribute("tilecount") != null) { set.TileCount = Int32.Parse(reader.GetAttribute("tilecount")); } if (reader.GetAttribute("columns") != null) { set.Columns = Int32.Parse(reader.GetAttribute("columns")); } if (set.Source != null) { set = LoadTileset(set, set.Source); } file.Map.Tilesets.Add(set); set = null; break; case "layer": TMXLayer layer = new TMXLayer(); layer.Name = reader.GetAttribute("name"); layer.Width = Int32.Parse(reader.GetAttribute("width")); layer.Height = Int32.Parse(reader.GetAttribute("height")); if (reader.ReadToDescendant("data")) { TMXData dat = new TMXData(); switch (reader.GetAttribute("encoding")) { case "base64": dat.Encoding = EncodingType.base64; break; case "csv": dat.Encoding = EncodingType.csv; break; } dat.Value = reader.ReadInnerXml(); layer.Data = dat; dat = null; } file.Map.Layers.Add(layer); break; } break; case XmlNodeType.Text: break; } } } return(file); }