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); }
static public void RotatePoints(PointF[] points, TmxObject tmxObject) { Matrix rotate = new Matrix(); rotate.RotateAt(tmxObject.Rotation, tmxObject.Position); rotate.TransformPoints(points); }
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); }
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 TmxObjectPolygon FromRectangle(TmxMap tmxMap, TmxObjectRectangle tmxRectangle) { TmxObjectPolygon tmxObjectPolygon = new TmxObjectPolygon(); TmxObject.CopyBaseProperties(tmxRectangle, tmxObjectPolygon); tmxObjectPolygon.Points = tmxRectangle.Points; return(tmxObjectPolygon); }
public void VisitObject(TmxObject obj) { // Objects only increase draw order if they are tiles if (obj is TmxObjectTile) { TmxObjectTile tile = obj as TmxObjectTile; tile.DrawOrderIndex = this.drawOrderIndex++; } }
static public void RotatePoints(PointF[] points, TmxObject tmxObject) { TranslatePoints(points, -tmxObject.Position.X, -tmxObject.Position.Y); TmxRotationMatrix rotate = new TmxRotationMatrix(-tmxObject.Rotation); rotate.TransformPoints(points); TranslatePoints(points, tmxObject.Position.X, tmxObject.Position.Y); }
private void DrawObjectCollider(Graphics g, TmxObject tmxObject, Color color) { Color brushColor = Color.FromArgb(128, color); using (Brush brush = new HatchBrush(HatchStyle.BackwardDiagonal, color, brushColor)) using (Pen pen = new Pen(color)) { pen.Alignment = PenAlignment.Inset; GraphicsState state = g.Save(); g.TranslateTransform(tmxObject.Position.X, tmxObject.Position.Y); g.RotateTransform(tmxObject.Rotation); if (tmxObject.GetType() == typeof(TmxObjectPolygon)) { DrawPolygon(g, pen, brush, tmxObject as TmxObjectPolygon); } else if (tmxObject.GetType() == typeof(TmxObjectRectangle)) { // Rectangles are polygons DrawPolygon(g, pen, brush, tmxObject as TmxObjectPolygon); } else if (tmxObject.GetType() == typeof(TmxObjectEllipse)) { DrawEllipse(g, pen, brush, tmxObject as TmxObjectEllipse); } else if (tmxObject.GetType() == typeof(TmxObjectPolyline)) { DrawPolyline(g, pen, tmxObject as TmxObjectPolyline); } else if (tmxObject.GetType() == typeof(TmxObjectTile)) { TmxObjectTile tmxObjectTile = tmxObject as TmxObjectTile; RectangleF rcTile = new RectangleF(); rcTile.X = 0; rcTile.Y = -tmxObjectTile.Tile.TileSize.Height; rcTile.Size = tmxObjectTile.Tile.TileSize; g.FillRectangle(brush, rcTile); g.DrawRectangle(pen, rcTile.X, rcTile.Y, rcTile.Width - 1, rcTile.Height - 1); } else { g.Restore(state); RectangleF bounds = tmxObject.GetWorldBounds(); g.FillRectangle(Brushes.Red, bounds.X, bounds.Y, bounds.Width, bounds.Height); g.DrawRectangle(Pens.White, bounds.X, bounds.Y, bounds.Width, bounds.Height); string message = String.Format("Unhandled object: {0}", tmxObject.GetNonEmptyName()); DrawString(g, message, bounds.X, bounds.Y); } // Restore our state g.Restore(state); } }
private void AddTmxObjectComponentsTo(GameObject gameObject, XElement goXml) { var xml = goXml.Element("TmxObjectComponent"); if (xml != null) { TmxObject tmxObject = gameObject.AddComponent <TmxObject>(); FillBaseTmxObjectProperties(tmxObject, xml); } }
protected static void CopyBaseProperties(TmxObject from, TmxObject to) { to.Name = from.Name; to.Type = from.Type; to.Visible = from.Visible; to.Position = from.Position; to.Size = from.Size; to.Rotation = from.Rotation; to.Properties = from.Properties; }
static protected void CopyBaseProperties(TmxObject from, TmxObject to) { to.Name = from.Name; to.Type = from.Type; to.Visible = from.Visible; to.Position = from.Position; to.Size = from.Size; to.Rotation = from.Rotation; to.Properties = from.Properties; }
protected static void CopyBaseProperties(TmxObject from, TmxObject to) { to.Id = from.Id; to.Name = from.Name; to.Type = from.Type; to.Visible = from.Visible; to.Position = from.Position; to.Size = from.Size; to.Rotation = from.Rotation; to.Properties = from.Properties; to.ParentObjectGroup = from.ParentObjectGroup; }
static public TmxObjectPolygon FromIsometricRectangle(TmxMap tmxMap, TmxObjectRectangle tmxRectangle) { Debug.Assert(tmxMap.Orientation == TmxMap.MapOrientation.Isometric); TmxObjectPolygon tmxPolygon = new TmxObjectPolygon(); TmxObject.CopyBaseProperties(tmxRectangle, tmxPolygon); tmxPolygon.Points = tmxRectangle.Points; return(tmxPolygon); }
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); }
public static TmxObjectGroup FromXml(XElement xml, TmxLayerNode parent, TmxMap tmxMap) { TmxObjectGroup tmxObjectGroup = new TmxObjectGroup(parent, tmxMap); tmxObjectGroup.FromXmlInternal(xml); tmxObjectGroup.Color = TmxHelper.GetAttributeAsColor(xml, "color", new Color32(128, 128, 128, 255)); Logger.WriteVerbose("Parsing objects in object group '{0}'", tmxObjectGroup.Name); IEnumerable <TmxObject> source = from obj in xml.Elements("object") select TmxObject.FromXml(obj, tmxObjectGroup, tmxMap); tmxObjectGroup.Objects = (from o in source orderby TmxMath.ObjectPointFToMapSpace(tmxMap, o.Position).Y select o).ToList(); return(tmxObjectGroup); }
private void FixTileColliderObjects(TmxMap tmxMap) { // Objects inside of tiles are colliders that will be merged with the colliders on neighboring tiles. // In order to promote this merging we have to perform the following clean up operations ... // - All rectangles objects are made into polygon objects // - All polygon objects will have their rotations burned into the polygon points (and Rotation set to zero) // - All cooridinates will be "sanitized" to make up for floating point errors due to rotation and poor placement of colliders // (The sanitation will round all numbers to the nearest 1/256th) // Replace rectangles with polygons for (int i = 0; i < this.ObjectGroup.Objects.Count; i++) { TmxObject tmxObject = this.ObjectGroup.Objects[i]; if (tmxObject is TmxObjectRectangle) { TmxObjectPolygon tmxObjectPolygon = TmxObjectPolygon.FromRectangle(tmxMap, tmxObject as TmxObjectRectangle); this.ObjectGroup.Objects[i] = tmxObjectPolygon; } } // Burn rotation into all polygon points, sanitizing the point locations as we go foreach (TmxObject tmxObject in this.ObjectGroup.Objects) { TmxHasPoints tmxHasPoints = tmxObject as TmxHasPoints; if (tmxHasPoints != null) { var pointfs = tmxHasPoints.Points.ToArray(); // Rotate our points by the rotation and position in the object TmxMath.RotatePoints(pointfs, tmxObject); // Sanitize our points to make up for floating point precision errors pointfs = pointfs.Select(TmxMath.Sanitize).ToArray(); // Set the points back into the object tmxHasPoints.Points = pointfs.ToList(); // Zero out our rotation tmxObject.BakeRotation(); } } }
public static TmxProperties GetPropertiesWithTypeDefaults(TmxHasProperties hasProperties, TmxObjectTypes objectTypes) { TmxProperties tmxProperties = new TmxProperties(); // Fill in all the default properties first // (Note: At the moment, only TmxObject has default properties it inherits from TmxObjectType) string objectTypeName = null; if (hasProperties is TmxObject) { TmxObject tmxObject = hasProperties as TmxObject; objectTypeName = tmxObject.Type; } else if (hasProperties is TmxLayer) { TmxLayer tmxLayer = hasProperties as TmxLayer; objectTypeName = tmxLayer.Name; } // If an object type has been found then copy over all the default values for properties TmxObjectType tmxObjectType = objectTypes.GetValueOrNull(objectTypeName); if (tmxObjectType != null) { foreach (TmxObjectTypeProperty tmxTypeProp in tmxObjectType.Properties.Values) { tmxProperties.PropertyMap[tmxTypeProp.Name] = new TmxProperty() { Name = tmxTypeProp.Name, Type = tmxTypeProp.Type, Value = tmxTypeProp.Default }; } } // Now add all the object properties (which may override some of the default properties) foreach (TmxProperty tmxProp in hasProperties.Properties.PropertyMap.Values) { tmxProperties.PropertyMap[tmxProp.Name] = tmxProp; } return(tmxProperties); }
public static TmxObjectGroup FromXml(XElement xml, TmxLayerNode parent, TmxMap tmxMap) { Debug.Assert(xml.Name == "objectgroup"); TmxObjectGroup tmxObjectGroup = new TmxObjectGroup(parent, tmxMap); tmxObjectGroup.FromXmlInternal(xml); // Color is specific to object group tmxObjectGroup.Color = TmxHelper.GetAttributeAsColor(xml, "color", Color.FromArgb(128, 128, 128)); // Get all the objects Logger.WriteVerbose("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.ToList(); return(tmxObjectGroup); }
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); }
private void FixTileColliderObjects(TmxMap tmxMap) { for (int i = 0; i < ObjectGroup.Objects.Count; i++) { TmxObject tmxObject = ObjectGroup.Objects[i]; if (tmxObject is TmxObjectRectangle) { TmxObjectPolygon value = TmxObjectPolygon.FromRectangle(tmxMap, tmxObject as TmxObjectRectangle); ObjectGroup.Objects[i] = value; } } foreach (TmxObject @object in ObjectGroup.Objects) { TmxHasPoints tmxHasPoints = @object as TmxHasPoints; if (tmxHasPoints != null) { PointF[] array = tmxHasPoints.Points.ToArray(); TmxMath.RotatePoints(array, @object); array = array.Select(TmxMath.Sanitize).ToArray(); tmxHasPoints.Points = array.ToList(); @object.BakeRotation(); } } }
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); }
private void DrawObjectCollider(Graphics g, TmxObject tmxObject, Color color) { Color brushColor = Color.FromArgb(128, color); using (Brush brush = new HatchBrush(HatchStyle.BackwardDiagonal, color, brushColor)) using (Pen pen = new Pen(color)) { pen.Alignment = PenAlignment.Inset; GraphicsState state = g.Save(); PointF xfPosition = TmxMath.ObjectPointFToMapSpace(this.tmxMap, tmxObject.Position); g.TranslateTransform(xfPosition.X, xfPosition.Y); g.RotateTransform(tmxObject.Rotation); if (tmxObject.GetType() == typeof(TmxObjectPolygon)) { DrawPolygon(g, pen, brush, tmxObject as TmxObjectPolygon); } else if (tmxObject.GetType() == typeof(TmxObjectRectangle)) { if (this.tmxMap.Orientation == TmxMap.MapOrientation.Isometric) { TmxObjectPolygon tmxIsometricRectangle = TmxObjectPolygon.FromIsometricRectangle(this.tmxMap, tmxObject as TmxObjectRectangle); DrawPolygon(g, pen, brush, tmxIsometricRectangle); } else { // Rectangles are polygons DrawPolygon(g, pen, brush, tmxObject as TmxObjectPolygon); } } else if (tmxObject.GetType() == typeof(TmxObjectEllipse)) { DrawEllipse(g, pen, brush, tmxObject as TmxObjectEllipse); } else if (tmxObject.GetType() == typeof(TmxObjectPolyline)) { DrawPolyline(g, pen, tmxObject as TmxObjectPolyline); } else if (tmxObject.GetType() == typeof(TmxObjectTile)) { TmxObjectTile tmxObjectTile = tmxObject as TmxObjectTile; RectangleF rcTile = new RectangleF(); rcTile.X = 0; rcTile.Y = -tmxObjectTile.Tile.TileSize.Height; rcTile.Size = tmxObjectTile.Tile.TileSize; g.FillRectangle(brush, rcTile); g.DrawRectangle(pen, rcTile.X, rcTile.Y, rcTile.Width - 1, rcTile.Height - 1); } else { g.Restore(state); RectangleF bounds = tmxObject.GetWorldBounds(); g.FillRectangle(Brushes.Red, bounds.X, bounds.Y, bounds.Width, bounds.Height); g.DrawRectangle(Pens.White, bounds.X, bounds.Y, bounds.Width, bounds.Height); string message = String.Format("Unhandled object: {0}", tmxObject.GetNonEmptyName()); DrawString(g, message, bounds.X, bounds.Y); } // Restore our state g.Restore(state); } }
public static void RotatePoints(PointF[] points, TmxObject tmxObject) { TranslatePoints(points, 0f - tmxObject.Position.X, 0f - tmxObject.Position.Y); new TmxRotationMatrix(0f - tmxObject.Rotation).TransformPoints(points); TranslatePoints(points, tmxObject.Position.X, tmxObject.Position.Y); }
private void DrawObjectCollider(Graphics g, TmxObject tmxObject, Color color) { Color brushColor = Color.FromArgb(128, color); using (Brush brush = new HatchBrush(HatchStyle.BackwardDiagonal, color, brushColor)) using (Pen pen = new Pen(color)) { pen.Alignment = PenAlignment.Inset; GraphicsState state = g.Save(); PointF xfPosition = TmxMath.ObjectPointFToMapSpace(this.tmxMap, tmxObject.Position); g.TranslateTransform(xfPosition.X, xfPosition.Y); g.RotateTransform(tmxObject.Rotation); if (tmxObject.GetType() == typeof(TmxObjectPolygon)) { DrawPolygon(g, pen, brush, tmxObject as TmxObjectPolygon); } else if (tmxObject.GetType() == typeof(TmxObjectRectangle)) { if (this.tmxMap.Orientation == TmxMap.MapOrientation.Isometric) { TmxObjectPolygon tmxIsometricRectangle = TmxObjectPolygon.FromIsometricRectangle(this.tmxMap, tmxObject as TmxObjectRectangle); DrawPolygon(g, pen, brush, tmxIsometricRectangle); } else { // Rectangles are polygons DrawPolygon(g, pen, brush, tmxObject as TmxObjectPolygon); } } else if (tmxObject.GetType() == typeof(TmxObjectEllipse)) { DrawEllipse(g, pen, brush, tmxObject as TmxObjectEllipse); } else if (tmxObject.GetType() == typeof(TmxObjectPolyline)) { DrawPolyline(g, pen, tmxObject as TmxObjectPolyline); } else if (tmxObject.GetType() == typeof(TmxObjectTile)) { GraphicsState tileState = g.Save(); TmxObjectTile tmxObjectTile = tmxObject as TmxObjectTile; // Apply scale SizeF scale = tmxObjectTile.GetTileObjectScale(); g.ScaleTransform(scale.Width, scale.Height); // Apply horizontal flip if (tmxObjectTile.FlippedHorizontal) { g.TranslateTransform(tmxObjectTile.Tile.TileSize.Width, 0); g.ScaleTransform(-1, 1); } // Apply vertical flip if (tmxObjectTile.FlippedVertical) { g.TranslateTransform(0, -tmxObjectTile.Tile.TileSize.Height); g.ScaleTransform(1, -1); } // (Note: Now we can draw the tile and collisions as normal as the transforms have been set up.) // Draw the tile Rectangle destination = new Rectangle(0, -tmxObjectTile.Tile.TileSize.Height, tmxObjectTile.Tile.TileSize.Width, tmxObjectTile.Tile.TileSize.Height); Rectangle source = new Rectangle(tmxObjectTile.Tile.LocationOnSource, tmxObjectTile.Tile.TileSize); g.DrawImage(tmxObjectTile.Tile.TmxImage.ImageBitmap, destination, source, GraphicsUnit.Pixel); // Put a black border around the tile so it sticks out a bit as an object g.DrawRectangle(Pens.Black, destination); // Draw the collisions // Make up for the fact that the bottom-left corner is the origin g.TranslateTransform(0, -tmxObjectTile.Tile.TileSize.Height); foreach (var obj in tmxObjectTile.Tile.ObjectGroup.Objects) { DrawObjectCollider(g, obj, Color.Gray); } g.Restore(tileState); } else { g.Restore(state); RectangleF bounds = tmxObject.GetWorldBounds(); g.FillRectangle(Brushes.Red, bounds.X, bounds.Y, bounds.Width, bounds.Height); g.DrawRectangle(Pens.White, bounds.X, bounds.Y, bounds.Width, bounds.Height); string message = String.Format("Unhandled object: {0}", tmxObject.GetNonEmptyName()); DrawString(g, message, bounds.X, bounds.Y); } // Restore our state g.Restore(state); } }
private void DrawObjectCollider(Graphics g, TmxObject tmxObject, Color color) { Color brushColor = Color.FromArgb(128, color); using (Brush brush = new HatchBrush(HatchStyle.BackwardDiagonal, color, brushColor)) using (Pen pen = new Pen(color)) { pen.Alignment = PenAlignment.Inset; GraphicsState state = g.Save(); PointF xfPosition = TmxMath.ObjectPointFToMapSpace(this.tmxMap, tmxObject.Position); g.TranslateTransform(xfPosition.X, xfPosition.Y); g.RotateTransform(tmxObject.Rotation); if (tmxObject.GetType() == typeof(TmxObjectPolygon)) { DrawPolygon(g, pen, brush, tmxObject as TmxObjectPolygon); } else if (tmxObject.GetType() == typeof(TmxObjectRectangle)) { if (this.tmxMap.Orientation == TmxMap.MapOrientation.Isometric) { TmxObjectPolygon tmxIsometricRectangle = TmxObjectPolygon.FromRectangle(this.tmxMap, tmxObject as TmxObjectRectangle); DrawPolygon(g, pen, brush, tmxIsometricRectangle); } else { // Rectangles are polygons DrawPolygon(g, pen, brush, tmxObject as TmxObjectPolygon); } } else if (tmxObject.GetType() == typeof(TmxObjectEllipse)) { DrawEllipse(g, pen, brush, tmxObject as TmxObjectEllipse); } else if (tmxObject.GetType() == typeof(TmxObjectPolyline)) { DrawPolyline(g, pen, tmxObject as TmxObjectPolyline); } else if (tmxObject.GetType() == typeof(TmxObjectTile)) { GraphicsState tileState = g.Save(); TmxObjectTile tmxObjectTile = tmxObject as TmxObjectTile; // Apply scale SizeF scale = tmxObjectTile.GetTileObjectScale(); g.ScaleTransform(scale.Width, scale.Height); // Apply horizontal flip if (tmxObjectTile.FlippedHorizontal) { g.TranslateTransform(tmxObjectTile.Tile.TileSize.Width, 0); g.ScaleTransform(-1, 1); } // Apply vertical flip if (tmxObjectTile.FlippedVertical) { g.TranslateTransform(0, -tmxObjectTile.Tile.TileSize.Height); g.ScaleTransform(1, -1); } // (Note: Now we can draw the tile and collisions as normal as the transforms have been set up.) // Draw the tile Rectangle destination = new Rectangle(0, -tmxObjectTile.Tile.TileSize.Height, tmxObjectTile.Tile.TileSize.Width, tmxObjectTile.Tile.TileSize.Height); Rectangle source = new Rectangle(tmxObjectTile.Tile.LocationOnSource, tmxObjectTile.Tile.TileSize); g.DrawImage(tmxObjectTile.Tile.TmxImage.ImageBitmap, destination, source, GraphicsUnit.Pixel); // Put a black border around the tile so it sticks out a bit as an object g.DrawRectangle(Pens.Black, destination); // Draw the collisions // Make up for the fact that the bottom-left corner is the origin g.TranslateTransform(0, -tmxObjectTile.Tile.TileSize.Height); foreach (var obj in tmxObjectTile.Tile.ObjectGroup.Objects) { DrawObjectCollider(g, obj, Color.Gray); } g.Restore(tileState); } else { g.Restore(state); RectangleF bounds = tmxObject.GetWorldBounds(); g.FillRectangle(Brushes.Red, bounds.X, bounds.Y, bounds.Width, bounds.Height); g.DrawRectangle(Pens.White, bounds.X, bounds.Y, bounds.Width, bounds.Height); string message = String.Format("Unhandled object: {0}", tmxObject.GetNonEmptyName()); DrawString(g, message, bounds.X, bounds.Y); } // Restore our state g.Restore(state); } }