private void ParseTilesetFromImageLayer(XElement elemImageLayer) { string tilesetName = TmxHelper.GetAttributeAsString(elemImageLayer, "name"); XElement xmlImage = elemImageLayer.Element("image"); if (xmlImage == null) { Logger.WriteWarning("Image Layer '{0}' has no image assigned.", tilesetName); return; } TmxProperties properties = TmxProperties.FromXml(elemImageLayer); string prefix = properties.GetPropertyValueAsString("unity:namePrefix", ""); string postfix = properties.GetPropertyValueAsString("unity:namePostfix", ""); TmxImage tmxImage = TmxImage.FromXml(xmlImage, prefix, postfix); // The "firstId" is is always one more than all the tiles that we've already parsed (which may be zero) uint firstId = 1; if (this.Tiles.Count > 0) { firstId = this.Tiles.Max(t => t.Key) + 1; } uint localId = 1; uint globalId = firstId + localId; TmxTile tile = new TmxTile(this, globalId, localId, tilesetName, tmxImage); tile.SetTileSize(tmxImage.Size.Width, tmxImage.Size.Height); tile.SetLocationOnSource(0, 0); this.Tiles[tile.GlobalId] = tile; }
public static TsxTileset FromImageLayerXml(XElement xml, TmxMap tmxMap) { TsxTileset tsxTileset = new TsxTileset(tmxMap); tsxTileset.Name = TmxHelper.GetAttributeAsString(xml, "name"); XElement xElement = xml.Element("image"); if (xElement == null) { Logger.WriteWarning("Image Layer '{0}' has no image assigned.", tsxTileset.Name); return(null); } TmxProperties tmxProperties = TmxProperties.FromXml(xml); string propertyValueAsString = tmxProperties.GetPropertyValueAsString("unity:namePrefix", ""); string propertyValueAsString2 = tmxProperties.GetPropertyValueAsString("unity:namePostfix", ""); TmxImage tmxImage = TmxImage.FromXml(xElement, propertyValueAsString, propertyValueAsString2); uint num = 1u; if (tmxMap.Tiles.Count > 0) { num = tmxMap.Tiles.Max((KeyValuePair <uint, TmxTile> t) => t.Key) + 1; } uint num2 = 1u; uint globalId = num + num2; TmxTile tmxTile = new TmxTile(tmxMap, globalId, num2, tsxTileset.Name, tmxImage); tmxTile.SetTileSize(tmxImage.Size.Width, tmxImage.Size.Height); tmxTile.SetLocationOnSource(0, 0); tsxTileset.Tiles.Add(tmxTile); tsxTileset.Tiles.ForEach(delegate(TmxTile t) { tmxMap.Tiles.Add(t.GlobalId, t); }); return(tsxTileset); }
private void ParseTilesetFromImageLayer(XElement elemImageLayer) { string tilesetName = TmxHelper.GetAttributeAsString(elemImageLayer, "name"); XElement xmlImage = elemImageLayer.Element("image"); if (xmlImage == null) { Program.WriteWarning("Image Layer '{0}' has no image assigned.", tilesetName); return; } TmxImage tmxImage = TmxImage.FromXml(xmlImage); RegisterImagePath(tmxImage.Path); uint firstId = this.Tiles.Max(t => t.Key) + 1; uint localId = 1; uint globalId = firstId + localId; TmxTile tile = new TmxTile(globalId, localId, tilesetName, tmxImage); tile.SetTileSize(tmxImage.Size.Width, tmxImage.Size.Height); tile.SetLocationOnSource(0, 0); this.Tiles[tile.GlobalId] = tile; }
// This method is called eventually for external tilesets too // Only the gid attribute has been consumed at this point for the tileset private void ParseInternalTileset(XElement elemTileset, uint firstId) { string tilesetName = TmxHelper.GetAttributeAsString(elemTileset, "name"); Logger.WriteVerbose("Parse internal tileset '{0}' (gid = {1}) ...", tilesetName, firstId); int tileWidth = TmxHelper.GetAttributeAsInt(elemTileset, "tilewidth"); int tileHeight = TmxHelper.GetAttributeAsInt(elemTileset, "tileheight"); int spacing = TmxHelper.GetAttributeAsInt(elemTileset, "spacing", 0); int margin = TmxHelper.GetAttributeAsInt(elemTileset, "margin", 0); PointF tileOffset = PointF.Empty; XElement xmlTileOffset = elemTileset.Element("tileoffset"); if (xmlTileOffset != null) { tileOffset.X = TmxHelper.GetAttributeAsInt(xmlTileOffset, "x"); tileOffset.Y = TmxHelper.GetAttributeAsInt(xmlTileOffset, "y"); } IList <TmxTile> tilesToAdd = new List <TmxTile>(); // Get proerties on tileset TmxProperties properties = TmxProperties.FromXml(elemTileset); string prefix = properties.GetPropertyValueAsString("unity:namePrefix", ""); string postfix = properties.GetPropertyValueAsString("unity:namePostfix", ""); // Tilesets may have an image for all tiles within it, or it may have an image per tile if (elemTileset.Element("image") != null) { TmxImage tmxImage = TmxImage.FromXml(elemTileset.Element("image"), prefix, postfix); // Create all the tiles // This is a bit complicated because of spacing and margin // (Margin is ignored from Width and Height) for (int end_y = margin + tileHeight; end_y <= tmxImage.Size.Height; end_y += spacing + tileHeight) { for (int end_x = margin + tileWidth; end_x <= tmxImage.Size.Width; end_x += spacing + tileWidth) { uint localId = (uint)tilesToAdd.Count(); uint globalId = firstId + localId; TmxTile tile = new TmxTile(this, globalId, localId, tilesetName, tmxImage); tile.Offset = tileOffset; tile.SetTileSize(tileWidth, tileHeight); tile.SetLocationOnSource(end_x - tileWidth, end_y - tileHeight); tilesToAdd.Add(tile); } } } else { // Each tile will have it's own image foreach (var t in elemTileset.Elements("tile")) { TmxImage tmxImage = TmxImage.FromXml(t.Element("image"), prefix, postfix); uint localId = (uint)tilesToAdd.Count(); // Local Id can be overridden by the tile element // This is because tiles can be removed from the tileset, so we won'd always have a zero-based index localId = TmxHelper.GetAttributeAsUInt(t, "id", localId); uint globalId = firstId + localId; TmxTile tile = new TmxTile(this, globalId, localId, tilesetName, tmxImage); tile.Offset = tileOffset; tile.SetTileSize(tmxImage.Size.Width, tmxImage.Size.Height); tile.SetLocationOnSource(0, 0); tilesToAdd.Add(tile); } } StringBuilder builder = new StringBuilder(); foreach (TmxTile tile in tilesToAdd) { builder.AppendFormat("{0}", tile.ToString()); if (tile != tilesToAdd.Last()) { builder.Append("\n"); } this.Tiles[tile.GlobalId] = tile; } Logger.WriteVerbose("Added {0} tiles", tilesToAdd.Count); // Add any extra data to tiles foreach (var elemTile in elemTileset.Elements("tile")) { int localTileId = TmxHelper.GetAttributeAsInt(elemTile, "id"); var tiles = from t in this.Tiles where t.Value.GlobalId == localTileId + firstId select t.Value; // Note that some old tile data may be sticking around if (tiles.Count() == 0) { Logger.WriteWarning("Tile '{0}' in tileset '{1}' does not exist but there is tile data for it.\n{2}", localTileId, tilesetName, elemTile.ToString()); } else { tiles.First().ParseTileXml(elemTile, this, firstId); } } }
// This method is called eventually for external tilesets too // Only the gid attribute has been consumed at this point for the tileset private void ParseInternalTileset(XElement elemTileset, uint firstId) { string tilesetName = TmxHelper.GetAttributeAsString(elemTileset, "name"); Program.WriteLine("Parse internal tileset '{0}' (gid = {1}) ...", tilesetName, firstId); Program.WriteVerbose(elemTileset.ToString()); int tileWidth = TmxHelper.GetAttributeAsInt(elemTileset, "tilewidth"); int tileHeight = TmxHelper.GetAttributeAsInt(elemTileset, "tileheight"); int spacing = TmxHelper.GetAttributeAsInt(elemTileset, "spacing", 0); int margin = TmxHelper.GetAttributeAsInt(elemTileset, "margin", 0); IList <TmxTile> tilesToAdd = new List <TmxTile>(); // Tilesets may have an image for all tiles within it, or it may have an image per tile if (elemTileset.Element("image") != null) { TmxImage tmxImage = TmxImage.FromXml(elemTileset.Element("image")); RegisterImagePath(tmxImage.Path); // Create all the tiles // This is a bit complicated because of spacing and margin // (Margin is ignored from Width and Height) for (int end_y = margin + tileHeight; end_y <= tmxImage.Size.Height; end_y += spacing + tileHeight) { for (int end_x = margin + tileWidth; end_x <= tmxImage.Size.Width; end_x += spacing + tileWidth) { uint localId = (uint)tilesToAdd.Count(); uint globalId = firstId + localId; TmxTile tile = new TmxTile(globalId, localId, tilesetName, tmxImage); tile.SetTileSize(tileWidth, tileHeight); tile.SetLocationOnSource(end_x - tileWidth, end_y - tileHeight); tilesToAdd.Add(tile); } } } else { // Each tile will have it's own image foreach (var t in elemTileset.Elements("tile")) { TmxImage tmxImage = TmxImage.FromXml(t.Element("image")); RegisterImagePath(tmxImage.Path); uint localId = (uint)tilesToAdd.Count(); uint globalId = firstId + localId; TmxTile tile = new TmxTile(globalId, localId, tilesetName, tmxImage); tile.SetTileSize(tmxImage.Size.Width, tmxImage.Size.Height); tile.SetLocationOnSource(0, 0); tilesToAdd.Add(tile); } } StringBuilder builder = new StringBuilder(); foreach (TmxTile tile in tilesToAdd) { builder.AppendFormat("{0}", tile.ToString()); if (tile != tilesToAdd.Last()) { builder.Append("\n"); } this.Tiles[tile.GlobalId] = tile; } Program.WriteLine("Added {0} tiles", tilesToAdd.Count); Program.WriteVerbose(builder.ToString()); // Add any extra data to tiles foreach (var elemTile in elemTileset.Elements("tile")) { int localTileId = TmxHelper.GetAttributeAsInt(elemTile, "id"); var tiles = from t in this.Tiles where t.Value.GlobalId == localTileId + firstId select t.Value; tiles.First().ParseXml(elemTile, this, firstId); } }
private static void ParseTilesetXml(XElement xml, TsxTileset tileset) { tileset.Name = TmxHelper.GetAttributeAsString(xml, "name"); Logger.WriteVerbose("Parse internal tileset '{0}' (gid = {1}) ...", tileset.Name, tileset.FirstGlobalId); tileset.TileWidth = TmxHelper.GetAttributeAsInt(xml, "tilewidth"); tileset.TileHeight = TmxHelper.GetAttributeAsInt(xml, "tileheight"); tileset.Spacing = TmxHelper.GetAttributeAsInt(xml, "spacing", 0); tileset.Margin = TmxHelper.GetAttributeAsInt(xml, "margin", 0); PointF empty = PointF.Empty; XElement xElement = xml.Element("tileoffset"); if (xElement != null) { empty.X = (float)TmxHelper.GetAttributeAsInt(xElement, "x"); empty.Y = (float)TmxHelper.GetAttributeAsInt(xElement, "y"); } tileset.TileOffset = empty; List <TmxTile> list = new List <TmxTile>(); TmxProperties tmxProperties = TmxProperties.FromXml(xml); string propertyValueAsString = tmxProperties.GetPropertyValueAsString("unity:namePrefix", ""); string propertyValueAsString2 = tmxProperties.GetPropertyValueAsString("unity:namePostfix", ""); if (xml.Element("image") != null) { TmxImage tmxImage = TmxImage.FromXml(xml.Element("image"), propertyValueAsString, propertyValueAsString2); for (int i = tileset.Margin + tileset.TileHeight; i <= tmxImage.Size.Height; i += tileset.Spacing + tileset.TileHeight) { for (int j = tileset.Margin + tileset.TileWidth; j <= tmxImage.Size.Width; j += tileset.Spacing + tileset.TileWidth) { uint num = (uint)list.Count(); uint globalId = tileset.FirstGlobalId + num; TmxTile tmxTile = new TmxTile(tileset.TmxMap, globalId, num, tileset.Name, tmxImage); tmxTile.Offset = empty; tmxTile.SetTileSize(tileset.TileWidth, tileset.TileHeight); tmxTile.SetLocationOnSource(j - tileset.TileWidth, i - tileset.TileHeight); list.Add(tmxTile); } } } else { foreach (XElement item in xml.Elements("tile")) { TmxImage tmxImage2 = TmxImage.FromXml(item.Element("image"), propertyValueAsString, propertyValueAsString2); uint defaultValue = (uint)list.Count(); defaultValue = TmxHelper.GetAttributeAsUInt(item, "id", defaultValue); uint globalId2 = tileset.FirstGlobalId + defaultValue; TmxTile tmxTile2 = new TmxTile(tileset.TmxMap, globalId2, defaultValue, tileset.Name, tmxImage2); tmxTile2.Offset = empty; tmxTile2.SetTileSize(tmxImage2.Size.Width, tmxImage2.Size.Height); tmxTile2.SetLocationOnSource(0, 0); list.Add(tmxTile2); } } tileset.Tiles.AddRange(list); Logger.WriteVerbose("Added {0} tiles", list.Count); foreach (XElement item2 in xml.Elements("tile")) { int localTileId = TmxHelper.GetAttributeAsInt(item2, "id"); IEnumerable <TmxTile> source = from t in tileset.Tiles where t.GlobalId == localTileId + tileset.FirstGlobalId select t; if (source.Count() == 0) { Logger.WriteWarning("Tile '{0}' in tileset '{1}' does not exist but there is tile data for it.\n{2}", localTileId, tileset.Name, item2.ToString()); } else { source.First().ParseTileXml(item2, tileset.TmxMap, tileset.FirstGlobalId); } } }