public static ShapeCollectionSave ToShapeCollectionSave(this TiledMapSave tiledMapSave, string layerName) { MapLayer mapLayer = null; if (!string.IsNullOrEmpty(layerName)) { mapLayer = tiledMapSave.Layers.FirstOrDefault(l => l.Name.Equals(layerName)); } var shapes = new ShapeCollectionSave(); if ((mapLayer != null && !mapLayer.IsVisible && mapLayer.VisibleBehavior == TMXGlueLib.TiledMapSave.LayerVisibleBehavior.Skip) || tiledMapSave.objectgroup == null || tiledMapSave.objectgroup.Count == 0) { return shapes; } foreach (mapObjectgroup group in tiledMapSave.objectgroup) { if (group.@object != null && !string.IsNullOrEmpty(group.Name) && (string.IsNullOrEmpty(layerName) || group.Name.Equals(layerName))) { foreach (mapObjectgroupObject @object in group.@object) { ///November 8th, 2015 ///Jesse Crafts-Finch ///If a polygon has a gid, and therefore an image associate with it, it will be turned into a spritesave, not a polygon. if (@object.gid != null) { continue; } if (@object.polygon != null) { foreach (mapObjectgroupObjectPolygon polygon in @object.polygon) { // TODO: Make this a rectangle object PolygonSave p = tiledMapSave.ConvertTmxObjectToFrbPolygonSave(@object.Name, @object.x, @object.y, @object.Rotation, polygon.points, true); if (p != null) { shapes.PolygonSaves.Add(p); } } } if (@object.polyline != null) { foreach (mapObjectgroupObjectPolyline polyline in @object.polyline) { PolygonSave p = tiledMapSave.ConvertTmxObjectToFrbPolygonSave(@object.Name, @object.x, @object.y, @object.Rotation, polyline.points, false); if (p != null) { shapes.PolygonSaves.Add(p); } } } if (@object.polygon == null && @object.polyline == null) { PolygonSave p = tiledMapSave.ConvertTmxObjectToFrbPolygonSave(@object.Name, @object.x, @object.y, @object.width, @object.height, @object.Rotation, @object.ellipse); if (p != null) { shapes.PolygonSaves.Add(p); } } } } } return shapes; }
private static PolygonSave ConvertTmxObjectToFrbPolygonSave(this TiledMapSave tiledMapSave, string name, double x, double y, double w, double h, double rotation, mapObjectgroupObjectEllipse ellipse) { var pointsSb = new StringBuilder(); if (ellipse == null) { pointsSb.Append("0,0"); pointsSb.AppendFormat(" {0},{1}", w, 0); pointsSb.AppendFormat(" {0},{1}", w, h); pointsSb.AppendFormat(" {0},{1}", 0, h); } else { const double a = .5; const double b = .5; // x = a cos t // y = b cos t var first = true; string firstPoint = ""; for (var angle = 0; angle <= 360; angle += 18) { var radians = MathHelper.ToRadians(angle); var newx = a*Math.Cos(radians)*w+w/2; var newy = b*Math.Sin(radians)*h+h/2; if (first) { firstPoint = string.Format("{0},{1}", newx, newy); } pointsSb.AppendFormat("{2}{0},{1}", newx, newy, first ? "" : " "); first = false; } pointsSb.AppendFormat(" {0}", firstPoint); } return tiledMapSave.ConvertTmxObjectToFrbPolygonSave(name, x, y, rotation, pointsSb.ToString(), true); }
public static ShapeCollectionSave ToShapeCollectionSave(this TiledMapSave tiledMapSave, string layerName) { MapLayer mapLayer = null; if (!string.IsNullOrEmpty(layerName)) { mapLayer = tiledMapSave.Layers.FirstOrDefault(l => l.Name.Equals(layerName)); } var shapes = new ShapeCollectionSave(); if ((mapLayer != null && !mapLayer.IsVisible && mapLayer.VisibleBehavior == TMXGlueLib.TiledMapSave.LayerVisibleBehavior.Skip) || tiledMapSave.objectgroup == null || tiledMapSave.objectgroup.Count == 0) { return shapes; } foreach (mapObjectgroup group in tiledMapSave.objectgroup) { if (group.@object != null && !string.IsNullOrEmpty(group.name) && (string.IsNullOrEmpty(layerName) || group.name.Equals(layerName))) { foreach (mapObjectgroupObject @object in group.@object) { if (@object.polygon != null) { foreach (mapObjectgroupObjectPolygon polygon in @object.polygon) { // TODO: Make this a rectangle object PolygonSave p = tiledMapSave.ConvertTmxObjectToFrbPolygonSave(@object.Name, @object.x, @object.y, @object.Rotation, polygon.points, true); if (p != null) { shapes.PolygonSaves.Add(p); } } } if (@object.polyline != null) { foreach (mapObjectgroupObjectPolyline polyline in @object.polyline) { PolygonSave p = tiledMapSave.ConvertTmxObjectToFrbPolygonSave(@object.Name, @object.x, @object.y, @object.Rotation, polyline.points, false); if (p != null) { shapes.PolygonSaves.Add(p); } } } if (@object.polygon == null && @object.polyline == null) { PolygonSave p = tiledMapSave.ConvertTmxObjectToFrbPolygonSave(@object.Name, @object.x, @object.y, @object.width, @object.height, @object.Rotation, @object.ellipse); if (p != null) { shapes.PolygonSaves.Add(p); } } } } } return shapes; }