Skip to content

Commit

Permalink
Added support for shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBoneJarmer committed Nov 17, 2021
1 parent 850b876 commit 004dedf
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/TiledCS.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>2.4.2</Version>
<Version>2.5.0</Version>
<Authors>Ruben Labruyere</Authors>
<Company>Ruben Labruyere</Company>
<PackageId>TiledCS</PackageId>
Expand Down
30 changes: 30 additions & 0 deletions src/TiledMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,9 @@ private TiledObject[] ParseObjects(XmlNodeList nodeList)
foreach (XmlNode node in nodeList)
{
var nodesProperty = node.SelectNodes("properties/property");
var nodePolygon = node.SelectSingleNode("polygon");
var nodePoint = node.SelectSingleNode("point");
var nodeEllipse = node.SelectSingleNode("ellipse");

var obj = new TiledObject();
obj.id = int.Parse(node.Attributes["id"].Value);
Expand All @@ -456,6 +459,33 @@ private TiledObject[] ParseObjects(XmlNodeList nodeList)
obj.properties = ParseProperties(nodesProperty);
}

if (nodePolygon != null)
{
var points = nodePolygon.Attributes["points"].Value;
var vertices = points.Split(' ');

var polygon = new TiledPolygon();
polygon.points = new float[vertices.Length * 2];

for (var i = 0; i < vertices.Length; i++)
{
polygon.points[(i * 2) + 0] = float.Parse(vertices[i].Split(',')[0], CultureInfo.InvariantCulture);
polygon.points[(i * 2) + 1] = float.Parse(vertices[i].Split(',')[1], CultureInfo.InvariantCulture);
}

obj.polygon = polygon;
}

if (nodeEllipse != null)
{
obj.ellipse = new TiledEllipse();
}

if (nodePoint != null)
{
obj.point = new TiledPoint();
}

if (node.Attributes["width"] != null)
{
obj.width = float.Parse(node.Attributes["width"].Value, CultureInfo.InvariantCulture);
Expand Down
39 changes: 39 additions & 0 deletions src/TiledModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,45 @@ public class TiledObject
/// An array of properties. Is null if none were defined.
/// </summary>
public TiledProperty[] properties;
/// <summary>
/// If an object was set to a polygon shape, this property will be set and can be used to access the polygon's data
/// </summary>
public TiledPolygon polygon;
/// <summary>
/// If an object was set to a point shape, this property will be set
/// </summary>
public TiledPoint point;
/// <summary>
/// If an object was set to an ellipse shape, this property will be set
/// </summary>
public TiledEllipse ellipse;
}

/// <summary>
/// Represents a polygon shape
/// </summary>
public class TiledPolygon
{
/// <summary>
/// The array of vertices where each two elements represent an x and y position. Like 'x,y,x,y,x,y,x,y'.
/// </summary>
public float[] points;
}

/// <summary>
/// Represents a point shape
/// </summary>
public class TiledPoint
{

}

/// <summary>
/// Represents an ellipse shape
/// </summary>
public class TiledEllipse
{

}

/// <summary>
Expand Down

0 comments on commit 004dedf

Please sign in to comment.