private void ParseMapXml(XDocument doc) { Logger.WriteVerbose("Parsing map root ..."); XElement xElement = doc.Element("map"); try { Orientation = TmxHelper.GetAttributeAsEnum <MapOrientation>(xElement, "orientation"); StaggerAxis = TmxHelper.GetAttributeAsEnum(xElement, "staggeraxis", MapStaggerAxis.Y); StaggerIndex = TmxHelper.GetAttributeAsEnum(xElement, "staggerindex", MapStaggerIndex.Odd); HexSideLength = TmxHelper.GetAttributeAsInt(xElement, "hexsidelength", 0); DrawOrderHorizontal = (TmxHelper.GetAttributeAsString(xElement, "renderorder", "right-down").Contains("right") ? 1 : (-1)); DrawOrderVertical = (TmxHelper.GetAttributeAsString(xElement, "renderorder", "right-down").Contains("down") ? 1 : (-1)); Width = TmxHelper.GetAttributeAsInt(xElement, "width"); Height = TmxHelper.GetAttributeAsInt(xElement, "height"); TileWidth = TmxHelper.GetAttributeAsInt(xElement, "tilewidth"); TileHeight = TmxHelper.GetAttributeAsInt(xElement, "tileheight"); BackgroundColor = TmxHelper.GetAttributeAsColor(xElement, "backgroundcolor", new Color32(128, 128, 128, 255)); } catch (Exception inner) { TmxException.FromAttributeException(inner, xElement); } Properties = TmxProperties.FromXml(xElement); IsResource = Properties.GetPropertyValueAsBoolean("unity:resource", false); IsResource = (IsResource || !string.IsNullOrEmpty(Properties.GetPropertyValueAsString("unity:resourcePath", null))); ExplicitSortingLayerName = Properties.GetPropertyValueAsString("unity:sortingLayerName", ""); ParseAllTilesets(doc); ParseAllTemplates(doc); LayerNodes = TmxLayerNode.ListFromXml(xElement, null, this); MapSizeInPixels = CalculateMapSizeInPixels(); TmxDisplayOrderVisitor visitor = new TmxDisplayOrderVisitor(); Visit(visitor); }
protected void FromXmlInternal(XElement xml) { // Get common elements amoung layer nodes from xml this.Name = TmxHelper.GetAttributeAsString(xml, "name", ""); this.Visible = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1; this.Opacity = TmxHelper.GetAttributeAsFloat(xml, "opacity", 1); // Get the offset PointF offset = new PointF(0, 0); offset.X = TmxHelper.GetAttributeAsFloat(xml, "offsetx", 0); offset.Y = TmxHelper.GetAttributeAsFloat(xml, "offsety", 0); this.Offset = offset; // Get all the properties this.Properties = TmxProperties.FromXml(xml); // Set the "ignore" setting on this object group this.Ignore = this.Properties.GetPropertyValueAsEnum <IgnoreSettings>("unity:ignore", IgnoreSettings.False); // Explicit sorting properties this.ExplicitSortingLayerName = this.Properties.GetPropertyValueAsString("unity:sortingLayerName", ""); if (this.Properties.PropertyMap.ContainsKey("unity:sortingOrder")) { this.ExplicitSortingOrder = this.Properties.GetPropertyValueAsInt("unity:sortingOrder"); } // Are we using a unity:layer override? this.UnityLayerOverrideName = this.Properties.GetPropertyValueAsString("unity:layer", ""); // Add all our children this.LayerNodes = TmxLayerNode.ListFromXml(xml, this, this.TmxMap); }
public static TmxObjectGroup FromXml(XElement xml, TmxMap tmxMap) { Debug.Assert(xml.Name == "objectgroup"); TmxObjectGroup tmxObjectGroup = new TmxObjectGroup(); // Order within Xml file is import for layer types tmxObjectGroup.XmlElementIndex = xml.NodesBeforeSelf().Count(); tmxObjectGroup.Name = TmxHelper.GetAttributeAsString(xml, "name", ""); tmxObjectGroup.Visible = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1; tmxObjectGroup.Color = TmxHelper.GetAttributeAsColor(xml, "color", Color.FromArgb(128, 128, 128)); tmxObjectGroup.Properties = TmxProperties.FromXml(xml); PointF offset = new PointF(0, 0); offset.X = TmxHelper.GetAttributeAsFloat(xml, "offsetx", 0); offset.Y = TmxHelper.GetAttributeAsFloat(xml, "offsety", 0); tmxObjectGroup.Offset = offset; // Get all the objects Program.WriteLine("Parsing objects in object group '{0}'", tmxObjectGroup.Name); var objects = from obj in xml.Elements("object") select TmxObject.FromXml(obj, tmxObjectGroup, tmxMap); tmxObjectGroup.Objects = objects.ToList(); // Are we using a unity:layer override? tmxObjectGroup.UnityLayerOverrideName = tmxObjectGroup.Properties.GetPropertyValueAsString("unity:layer", ""); return(tmxObjectGroup); }
private void ParseMapXml(XDocument doc) { Program.WriteLine("Parsing map root ..."); //Program.WriteVerbose(doc.ToString()); // Some TMX files are far too big (cause out of memory exception) so don't do this XElement map = doc.Element("map"); try { this.Orientation = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation"); this.StaggerAxis = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y); this.StaggerIndex = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd); this.HexSideLength = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0); this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1; this.DrawOrderVertical = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1; this.Width = TmxHelper.GetAttributeAsInt(map, "width"); this.Height = TmxHelper.GetAttributeAsInt(map, "height"); this.TileWidth = TmxHelper.GetAttributeAsInt(map, "tilewidth"); this.TileHeight = TmxHelper.GetAttributeAsInt(map, "tileheight"); this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128)); } catch (Exception e) { TmxException.FromAttributeException(e, map); } // Collect our map properties this.Properties = TmxProperties.FromXml(map); ParseAllTilesets(doc); ParseAllLayers(doc); ParseAllObjectGroups(doc); // Once everything is loaded, take a moment to do additional plumbing ParseCompleted(); }
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 ParseMap(XDocument doc) { Program.WriteLine("Parsing map root ..."); Program.WriteVerbose(doc.ToString()); XElement map = doc.Element("map"); try { this.Orientation = TmxHelper.GetAttributeAsString(map, "orientation"); this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1; this.DrawOrderVertical = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1; this.Width = TmxHelper.GetAttributeAsInt(map, "width"); this.Height = TmxHelper.GetAttributeAsInt(map, "height"); this.TileWidth = TmxHelper.GetAttributeAsInt(map, "tilewidth"); this.TileHeight = TmxHelper.GetAttributeAsInt(map, "tileheight"); this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128)); } catch (Exception e) { TmxException.FromAttributeException(e, map); } // We only support orthogonal maps if (this.Orientation != "orthogonal") { TmxException.ThrowFormat("Only orthogonal maps are supported. This map is set to \"{0}\"", this.Orientation); } // Collect our map properties this.Properties = TmxProperties.FromXml(map); ParseAllTilesets(doc); ParseAllLayers(doc); ParseAllObjectGroups(doc); }
public static TmxObject FromXml(XElement xml, TmxObjectGroup tmxObjectGroup, TmxMap tmxMap) { Logger.WriteVerbose("Parsing object ..."); uint attributeAsUInt = TmxHelper.GetAttributeAsUInt(xml, "tid", 0u); if (attributeAsUInt != 0 && tmxMap.Templates.TryGetValue(attributeAsUInt, out TgxTemplate value)) { xml = value.Templatize(xml); tmxMap = value.TemplateGroupMap; } TmxObject tmxObject = null; if (xml.Element("ellipse") != null) { tmxObject = new TmxObjectEllipse(); } else if (xml.Element("polygon") != null) { tmxObject = new TmxObjectPolygon(); } else if (xml.Element("polyline") != null) { tmxObject = new TmxObjectPolyline(); } else if (xml.Attribute("gid") != null) { uint attributeAsUInt2 = TmxHelper.GetAttributeAsUInt(xml, "gid"); attributeAsUInt2 = TmxMath.GetTileIdWithoutFlags(attributeAsUInt2); if (tmxMap.Tiles.ContainsKey(attributeAsUInt2)) { tmxObject = new TmxObjectTile(); } else { Logger.WriteWarning("Tile Id {0} not found in tilesets. Using a rectangle instead.\n{1}", attributeAsUInt2, xml.ToString()); tmxObject = new TmxObjectRectangle(); } } else { tmxObject = new TmxObjectRectangle(); } tmxObject.Id = TmxHelper.GetAttributeAsInt(xml, "id", 0); tmxObject.Name = TmxHelper.GetAttributeAsString(xml, "name", ""); tmxObject.Type = TmxHelper.GetAttributeAsString(xml, "type", ""); tmxObject.Visible = (TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1); tmxObject.ParentObjectGroup = tmxObjectGroup; float attributeAsFloat = TmxHelper.GetAttributeAsFloat(xml, "x"); float attributeAsFloat2 = TmxHelper.GetAttributeAsFloat(xml, "y"); float attributeAsFloat3 = TmxHelper.GetAttributeAsFloat(xml, "width", 0f); float attributeAsFloat4 = TmxHelper.GetAttributeAsFloat(xml, "height", 0f); float attributeAsFloat5 = TmxHelper.GetAttributeAsFloat(xml, "rotation", 0f); tmxObject.Position = new PointF(attributeAsFloat, attributeAsFloat2); tmxObject.Size = new SizeF(attributeAsFloat3, attributeAsFloat4); tmxObject.Rotation = attributeAsFloat5; tmxObject.Properties = TmxProperties.FromXml(xml); tmxObject.InternalFromXml(xml, tmxMap); return(tmxObject); }
private void ParseMap(XDocument doc) { Program.WriteLine("Parsing map root ..."); Program.WriteVerbose(doc.ToString()); XElement map = doc.Element("map"); try { this.Orientation = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation"); this.StaggerAxis = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y); this.StaggerIndex = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd); this.HexSideLength = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0); this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1; this.DrawOrderVertical = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1; this.Width = TmxHelper.GetAttributeAsInt(map, "width"); this.Height = TmxHelper.GetAttributeAsInt(map, "height"); this.TileWidth = TmxHelper.GetAttributeAsInt(map, "tilewidth"); this.TileHeight = TmxHelper.GetAttributeAsInt(map, "tileheight"); this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128)); } catch (Exception e) { TmxException.FromAttributeException(e, map); } // Collect our map properties this.Properties = TmxProperties.FromXml(map); ParseAllTilesets(doc); ParseAllLayers(doc); ParseAllObjectGroups(doc); }
public static TmxObjectGroup FromXml(XElement xml, TmxMap tmxMap) { Debug.Assert(xml.Name == "objectgroup"); TmxObjectGroup tmxObjectGroup = new TmxObjectGroup(); tmxObjectGroup.Name = TmxHelper.GetAttributeAsString(xml, "name", ""); tmxObjectGroup.Visible = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1; tmxObjectGroup.Color = TmxHelper.GetAttributeAsColor(xml, "color", Color.FromArgb(128, 128, 128)); tmxObjectGroup.Properties = TmxProperties.FromXml(xml); PointF offset = new PointF(0, 0); offset.X = TmxHelper.GetAttributeAsFloat(xml, "offsetx", 0); offset.Y = TmxHelper.GetAttributeAsFloat(xml, "offsety", 0); tmxObjectGroup.Offset = offset; // Get all the objects Program.WriteLine("Parsing objects in object group '{0}'", tmxObjectGroup.Name); var objects = from obj in xml.Elements("object") select TmxObject.FromXml(obj, tmxMap); tmxObjectGroup.Objects = objects.ToList(); return(tmxObjectGroup); }
public static TmxObject FromXml(XElement xml, TmxObjectGroup tmxObjectGroup, TmxMap tmxMap) { Program.WriteLine("Parsing object ..."); Program.WriteVerbose(xml.ToString()); // What kind of TmxObject are we creating? TmxObject tmxObject = null; if (xml.Element("ellipse") != null) { tmxObject = new TmxObjectEllipse(); } else if (xml.Element("polygon") != null) { tmxObject = new TmxObjectPolygon(); } else if (xml.Element("polyline") != null) { tmxObject = new TmxObjectPolyline(); } else if (xml.Attribute("gid") != null) { tmxObject = new TmxObjectTile(); } else { // Just a rectangle tmxObject = new TmxObjectRectangle(); } // Data found on every object type tmxObject.Name = TmxHelper.GetAttributeAsString(xml, "name", ""); tmxObject.Type = TmxHelper.GetAttributeAsString(xml, "type", ""); tmxObject.Visible = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1; tmxObject.ParentObjectGroup = tmxObjectGroup; float x = TmxHelper.GetAttributeAsFloat(xml, "x"); float y = TmxHelper.GetAttributeAsFloat(xml, "y"); float w = TmxHelper.GetAttributeAsFloat(xml, "width", 0); float h = TmxHelper.GetAttributeAsFloat(xml, "height", 0); float r = TmxHelper.GetAttributeAsFloat(xml, "rotation", 0); tmxObject.Position = new System.Drawing.PointF(x, y); tmxObject.Size = new System.Drawing.SizeF(w, h); tmxObject.Rotation = r; tmxObject.Properties = TmxProperties.FromXml(xml); tmxObject.InternalFromXml(xml, tmxMap); return(tmxObject); }
private void ParseMapXml(XDocument doc) { Logger.WriteVerbose("Parsing map root ..."); XElement map = doc.Element("map"); try { this.Orientation = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation"); this.StaggerAxis = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y); this.StaggerIndex = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd); this.HexSideLength = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0); this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1; this.DrawOrderVertical = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1; this.Width = TmxHelper.GetAttributeAsInt(map, "width"); this.Height = TmxHelper.GetAttributeAsInt(map, "height"); this.TileWidth = TmxHelper.GetAttributeAsInt(map, "tilewidth"); this.TileHeight = TmxHelper.GetAttributeAsInt(map, "tileheight"); this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128)); } catch (Exception e) { TmxException.FromAttributeException(e, map); } // Collect our map properties this.Properties = TmxProperties.FromXml(map); // Check properties for us being a resource this.IsResource = this.Properties.GetPropertyValueAsBoolean("unity:resource", false); this.IsResource = this.IsResource || !String.IsNullOrEmpty(this.Properties.GetPropertyValueAsString("unity:resourcePath", null)); ParseAllTilesets(doc); // Get all our child layer nodes this.LayerNodes = TmxLayerNode.ListFromXml(map, null, this); // Calcuate the size of the map. Isometric and hex maps make this more complicated than a simple width and height thing. this.MapSizeInPixels = CalculateMapSizeInPixels(); // Visit each node in the map to assign display order TmxDisplayOrderVisitor visitor = new TmxDisplayOrderVisitor(); this.Visit(visitor); }
public void ParseTileXml(XElement elem, TmxMap tmxMap, uint firstId) { Logger.WriteVerbose("Parse tile data (gid = {0}, id {1}) ...", GlobalId, LocalId); Properties = TmxProperties.FromXml(elem); XElement xElement = elem.Element("objectgroup"); if (xElement != null) { ObjectGroup = TmxObjectGroup.FromXml(xElement, null, tmxMap); FixTileColliderObjects(tmxMap); } XElement xElement2 = elem.Element("animation"); if (xElement2 != null) { Animation = TmxAnimation.FromXml(xElement2, firstId); } }
public static TmxLayer FromXml(XElement elem, int layerIndex) { Program.WriteVerbose(elem.ToString()); TmxLayer tmxLayer = new TmxLayer(); // Have to decorate layer names in order to force them into being unique // Also, can't have whitespace in the name because Unity will add underscores tmxLayer.DefaultName = TmxHelper.GetAttributeAsString(elem, "name"); tmxLayer.UniqueName = String.Format("{0}_{1}", tmxLayer.DefaultName, layerIndex.ToString("D2")).Replace(" ", "_"); tmxLayer.Visible = TmxHelper.GetAttributeAsInt(elem, "visible", 1) == 1; tmxLayer.Width = TmxHelper.GetAttributeAsInt(elem, "width"); tmxLayer.Height = TmxHelper.GetAttributeAsInt(elem, "height"); tmxLayer.Properties = TmxProperties.FromXml(elem); tmxLayer.ParseData(elem.Element("data")); return(tmxLayer); }
protected void FromXmlInternal(XElement xml) { Name = TmxHelper.GetAttributeAsString(xml, "name", ""); Visible = (TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1); Opacity = TmxHelper.GetAttributeAsFloat(xml, "opacity", 1f); PointF offset = new PointF(0f, 0f); offset.X = TmxHelper.GetAttributeAsFloat(xml, "offsetx", 0f); offset.Y = TmxHelper.GetAttributeAsFloat(xml, "offsety", 0f); Offset = offset; Properties = TmxProperties.FromXml(xml); Ignore = Properties.GetPropertyValueAsEnum("unity:ignore", IgnoreSettings.False); ExplicitSortingLayerName = Properties.GetPropertyValueAsString("unity:sortingLayerName", ""); if (Properties.PropertyMap.ContainsKey("unity:sortingOrder")) { ExplicitSortingOrder = Properties.GetPropertyValueAsInt("unity:sortingOrder"); } UnityLayerOverrideName = Properties.GetPropertyValueAsString("unity:layer", ""); LayerNodes = ListFromXml(xml, this, ParentMap); }
public void ParseTileXml(XElement elem, TmxMap tmxMap, uint firstId) { Logger.WriteVerbose("Parse tile data (gid = {0}, id {1}) ...", this.GlobalId, this.LocalId); this.Properties = TmxProperties.FromXml(elem); // Do we have an object group for this tile? XElement elemObjectGroup = elem.Element("objectgroup"); if (elemObjectGroup != null) { this.ObjectGroup = TmxObjectGroup.FromXml(elemObjectGroup, null, tmxMap); FixTileColliderObjects(tmxMap); } // Is this an animated tile? XElement elemAnimation = elem.Element("animation"); if (elemAnimation != null) { this.Animation = TmxAnimation.FromXml(elemAnimation, firstId); } }
private void ParseMapXml(XDocument doc) { Logger.WriteLine("Parsing map root ..."); XElement map = doc.Element("map"); try { this.Orientation = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation"); this.StaggerAxis = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y); this.StaggerIndex = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd); this.HexSideLength = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0); this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1; this.DrawOrderVertical = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1; this.Width = TmxHelper.GetAttributeAsInt(map, "width"); this.Height = TmxHelper.GetAttributeAsInt(map, "height"); this.TileWidth = TmxHelper.GetAttributeAsInt(map, "tilewidth"); this.TileHeight = TmxHelper.GetAttributeAsInt(map, "tileheight"); this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128)); } catch (Exception e) { TmxException.FromAttributeException(e, map); } // Collect our map properties this.Properties = TmxProperties.FromXml(map); // Check properties for us being a resource this.IsResource = this.Properties.GetPropertyValueAsBoolean("unity:resource", false); this.IsResource = this.IsResource || !String.IsNullOrEmpty(this.Properties.GetPropertyValueAsString("unity:resourcePath", null)); ParseAllTilesets(doc); ParseAllLayers(doc); ParseAllObjectGroups(doc); // Once everything is loaded, take a moment to do additional plumbing ParseCompleted(); }
public static TmxObjectGroup FromXml(XElement xml, TmxMap tmxMap) { Debug.Assert(xml.Name == "objectgroup"); TmxObjectGroup tmxObjectGroup = new TmxObjectGroup(tmxMap); // Order within Xml file is import for layer types tmxObjectGroup.XmlElementIndex = xml.NodesBeforeSelf().Count(); tmxObjectGroup.Name = TmxHelper.GetAttributeAsString(xml, "name", ""); tmxObjectGroup.Visible = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1; tmxObjectGroup.Opacity = TmxHelper.GetAttributeAsFloat(xml, "opacity", 1); tmxObjectGroup.Color = TmxHelper.GetAttributeAsColor(xml, "color", Color.FromArgb(128, 128, 128)); tmxObjectGroup.Properties = TmxProperties.FromXml(xml); // Set the "ignore" setting on this object group tmxObjectGroup.Ignore = tmxObjectGroup.Properties.GetPropertyValueAsEnum <IgnoreSettings>("unity:ignore", IgnoreSettings.False); PointF offset = new PointF(0, 0); offset.X = TmxHelper.GetAttributeAsFloat(xml, "offsetx", 0); offset.Y = TmxHelper.GetAttributeAsFloat(xml, "offsety", 0); tmxObjectGroup.Offset = offset; // Get all the objects Logger.WriteLine("Parsing objects in object group '{0}'", tmxObjectGroup.Name); var objects = from obj in xml.Elements("object") select TmxObject.FromXml(obj, tmxObjectGroup, tmxMap); // The objects are ordered "visually" by Y position tmxObjectGroup.Objects = objects.OrderBy(o => TmxMath.ObjectPointFToMapSpace(tmxMap, o.Position).Y).ToList(); // Are we using a unity:layer override? tmxObjectGroup.UnityLayerOverrideName = tmxObjectGroup.Properties.GetPropertyValueAsString("unity:layer", ""); return(tmxObjectGroup); }
public static TmxLayer FromXml(XElement elem, TmxMap tmxMap) { TmxLayer tmxLayer = new TmxLayer(tmxMap); // Order within Xml file is import for layer types tmxLayer.XmlElementIndex = elem.NodesBeforeSelf().Count(); // Have to decorate layer names in order to force them into being unique // Also, can't have whitespace in the name because Unity will add underscores tmxLayer.Name = TmxHelper.GetAttributeAsString(elem, "name"); tmxLayer.Visible = TmxHelper.GetAttributeAsInt(elem, "visible", 1) == 1; tmxLayer.Opacity = TmxHelper.GetAttributeAsFloat(elem, "opacity", 1); PointF offset = new PointF(0, 0); offset.X = TmxHelper.GetAttributeAsFloat(elem, "offsetx", 0); offset.Y = TmxHelper.GetAttributeAsFloat(elem, "offsety", 0); tmxLayer.Offset = offset; // Set our properties tmxLayer.Properties = TmxProperties.FromXml(elem); // Set the "ignore" setting on this layer tmxLayer.Ignore = tmxLayer.Properties.GetPropertyValueAsEnum <IgnoreSettings>("unity:ignore", IgnoreSettings.False); // We can build a layer from a "tile layer" (default) or an "image layer" if (elem.Name == "layer") { tmxLayer.Width = TmxHelper.GetAttributeAsInt(elem, "width"); tmxLayer.Height = TmxHelper.GetAttributeAsInt(elem, "height"); tmxLayer.ParseData(elem.Element("data")); } else if (elem.Name == "imagelayer") { XElement xmlImage = elem.Element("image"); if (xmlImage == null) { Logger.WriteWarning("Image Layer '{0}' is being ignored since it has no image.", tmxLayer.Name); tmxLayer.Ignore = IgnoreSettings.True; return(tmxLayer); } // An image layer is sort of like an tile layer but with just one tile tmxLayer.Width = 1; tmxLayer.Height = 1; // Find the "tile" that matches our image string imagePath = TmxHelper.GetAttributeAsFullPath(elem.Element("image"), "source"); TmxTile tile = tmxMap.Tiles.First(t => t.Value.TmxImage.AbsolutePath == imagePath).Value; tmxLayer.TileIds = new uint[1] { tile.GlobalId }; // The image layer needs to be tranlated in an interesting way when expressed as a tile layer PointF translated = tmxLayer.Offset; // Make up for height of a regular tile in the map translated.Y -= (float)tmxMap.TileHeight; // Make up for the height of this image translated.Y += (float)tile.TmxImage.Size.Height; // Correct for any orientation effects on the map (like isometric) // (We essentially undo the translation via orientation here) PointF orientation = TmxMath.TileCornerInScreenCoordinates(tmxMap, 0, 0); translated.X -= orientation.X; translated.Y -= orientation.Y; // Translate by the x and y coordiantes translated.X += TmxHelper.GetAttributeAsFloat(elem, "x", 0); translated.Y += TmxHelper.GetAttributeAsFloat(elem, "y", 0); tmxLayer.Offset = translated; } // Sometimes TMX files have "dead" tiles in them (tiles that were removed but are still referenced) // Remove these tiles from the layer by replacing them with zero for (int t = 0; t < tmxLayer.TileIds.Length; ++t) { uint tileId = tmxLayer.TileIds[t]; tileId = TmxMath.GetTileIdWithoutFlags(tileId); if (!tmxMap.Tiles.ContainsKey(tileId)) { tmxLayer.TileIds[t] = 0; } } // Each layer will be broken down into "meshes" which are collections of tiles matching the same texture or animation tmxLayer.Meshes = TmxMesh.ListFromTmxLayer(tmxLayer); // Each layer may contain different collision types which are themselves put into "Collison Layers" to be processed later tmxLayer.UnityLayerOverrideName = tmxLayer.Properties.GetPropertyValueAsString("unity:layer", ""); tmxLayer.BuildCollisionLayers(); return(tmxLayer); }
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); } } }
// 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); } } }
public static TmxObject FromXml(XElement xml, TmxObjectGroup tmxObjectGroup, TmxMap tmxMap) { Logger.WriteLine("Parsing object ..."); // What kind of TmxObject are we creating? TmxObject tmxObject = null; if (xml.Element("ellipse") != null) { tmxObject = new TmxObjectEllipse(); } else if (xml.Element("polygon") != null) { tmxObject = new TmxObjectPolygon(); } else if (xml.Element("polyline") != null) { tmxObject = new TmxObjectPolyline(); } else if (xml.Attribute("gid") != null) { uint gid = TmxHelper.GetAttributeAsUInt(xml, "gid"); gid = TmxMath.GetTileIdWithoutFlags(gid); if (tmxMap.Tiles.ContainsKey(gid)) { tmxObject = new TmxObjectTile(); } else { // For some reason, the tile is not in any of our tilesets // Warn the user and use a rectangle Logger.WriteWarning("Tile Id {0} not found in tilesets. Using a rectangle instead.\n{1}", gid, xml.ToString()); tmxObject = new TmxObjectRectangle(); } } else { // Just a rectangle tmxObject = new TmxObjectRectangle(); } // Data found on every object type tmxObject.Name = TmxHelper.GetAttributeAsString(xml, "name", ""); tmxObject.Type = TmxHelper.GetAttributeAsString(xml, "type", ""); tmxObject.Visible = TmxHelper.GetAttributeAsInt(xml, "visible", 1) == 1; tmxObject.ParentObjectGroup = tmxObjectGroup; float x = TmxHelper.GetAttributeAsFloat(xml, "x"); float y = TmxHelper.GetAttributeAsFloat(xml, "y"); float w = TmxHelper.GetAttributeAsFloat(xml, "width", 0); float h = TmxHelper.GetAttributeAsFloat(xml, "height", 0); float r = TmxHelper.GetAttributeAsFloat(xml, "rotation", 0); tmxObject.Position = new System.Drawing.PointF(x, y); tmxObject.Size = new System.Drawing.SizeF(w, h); tmxObject.Rotation = r; tmxObject.Properties = TmxProperties.FromXml(xml); tmxObject.InternalFromXml(xml, tmxMap); return(tmxObject); }
public static TmxLayer FromXml(XElement elem, TmxMap tmxMap, int layerIndex) { Program.WriteVerbose(elem.ToString()); TmxLayer tmxLayer = new TmxLayer(); // Have to decorate layer names in order to force them into being unique // Also, can't have whitespace in the name because Unity will add underscores tmxLayer.DefaultName = TmxHelper.GetAttributeAsString(elem, "name"); tmxLayer.UniqueName = String.Format("{0}_{1}", tmxLayer.DefaultName, layerIndex.ToString("D2")).Replace(" ", "_"); tmxLayer.Visible = TmxHelper.GetAttributeAsInt(elem, "visible", 1) == 1; tmxLayer.Opacity = TmxHelper.GetAttributeAsFloat(elem, "opacity", 1); PointF offset = new PointF(0, 0); offset.X = TmxHelper.GetAttributeAsFloat(elem, "offsetx", 0); offset.Y = TmxHelper.GetAttributeAsFloat(elem, "offsety", 0); tmxLayer.Offset = offset; tmxLayer.Properties = TmxProperties.FromXml(elem); // Set the "ignore" setting on this layer tmxLayer.Ignore = tmxLayer.Properties.GetPropertyValueAsEnum <IgnoreSettings>("unity:ignore", IgnoreSettings.False); // We can build a layer from a "tile layer" (default) or an "image layer" if (elem.Name == "layer") { tmxLayer.Width = TmxHelper.GetAttributeAsInt(elem, "width"); tmxLayer.Height = TmxHelper.GetAttributeAsInt(elem, "height"); tmxLayer.ParseData(elem.Element("data")); } else if (elem.Name == "imagelayer") { XElement xmlImage = elem.Element("image"); if (xmlImage == null) { Program.WriteWarning("Image Layer '{0}' is being ignored since it has no image.", tmxLayer.DefaultName); tmxLayer.Ignore = IgnoreSettings.True; return(tmxLayer); } // An image layer is sort of like an tile layer but with just one tile tmxLayer.Width = 1; tmxLayer.Height = 1; // Find the "tile" that matches our image string imagePath = TmxHelper.GetAttributeAsFullPath(elem.Element("image"), "source"); TmxTile tile = tmxMap.Tiles.First(t => t.Value.TmxImage.Path == imagePath).Value; tmxLayer.TileIds = new uint[1] { tile.GlobalId }; // The image layer needs to be tranlated in an interesting way when expressed as a tile layer PointF translated = tmxLayer.Offset; // Make up for height of a regular tile in the map translated.Y -= (float)tmxMap.TileHeight; // Make up for the height of this image translated.Y += (float)tile.TmxImage.Size.Height; // Correct for any orientation effects on the map (like isometric) // (We essentially undo the translation via orientation here) PointF orientation = TmxMath.TileCornerInScreenCoordinates(tmxMap, 0, 0); translated.X -= orientation.X; translated.Y -= orientation.Y; // Translate by the x and y coordiantes translated.X += TmxHelper.GetAttributeAsFloat(elem, "x", 0); translated.Y += TmxHelper.GetAttributeAsFloat(elem, "y", 0); tmxLayer.Offset = translated; } return(tmxLayer); }