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;
}