Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
added polyline support CarimA@fd2bb2d, resolves #3
Browse files Browse the repository at this point in the history
  • Loading branch information
nolemretaW committed Apr 30, 2024
1 parent ebc0566 commit 3a2d9a5
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 22 deletions.
25 changes: 24 additions & 1 deletion TiledCSPlus.Test/TiledMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public void Layers()
{
TiledMap?.Layers[0].TintColor.ShouldBe(Color.FromArgb(252, 255, 254, 253));
TiledMap?.Layers[2].Name.ShouldBe("Image Layer 1");
TiledMap?.Layers[1].Objects[0].Class.ShouldBe("test110");
}

[Test]
Expand All @@ -58,4 +57,28 @@ public void Compression()
TiledMap19?.TileLayerFormat.ShouldBe(TiledTileLayerFormat.GzipBase64);
TiledMap?.TileLayerFormat.ShouldBe(TiledTileLayerFormat.ZstdBase64);
}

[Test]
public void Objects()
{
TiledMap?.Layers[1].Objects[0].Class.ShouldBe("test110");

TiledMap?.Layers[1].Objects[1].Polygon.Points[0].X.ShouldBe(0);
TiledMap?.Layers[1].Objects[1].Polygon.Points[0].Y.ShouldBe(0);

TiledMap?.Layers[1].Objects[1].Polygon.Points[1].X.ShouldBe(49.5785f);
TiledMap?.Layers[1].Objects[1].Polygon.Points[1].Y.ShouldBe(-0.364548f);

TiledMap?.Layers[1].Objects[1].Polygon.Points[2].X.ShouldBe(48.8494f);
TiledMap?.Layers[1].Objects[1].Polygon.Points[2].Y.ShouldBe(25.1538f);

TiledMap?.Layers[1].Objects[1].Polygon.Points[3].X.ShouldBe(4.37457f);
TiledMap?.Layers[1].Objects[1].Polygon.Points[3].Y.ShouldBe(21.5083f);

TiledMap?.Layers[1].Objects[2].Polyline.Points[0].X.ShouldBe(0);
TiledMap?.Layers[1].Objects[2].Polyline.Points[0].Y.ShouldBe(0);

TiledMap?.Layers[1].Objects[2].Polyline.Points[1].X.ShouldBe(47.7558f);
TiledMap?.Layers[1].Objects[2].Polyline.Points[1].Y.ShouldBe(3.64548f);
}
}
72 changes: 58 additions & 14 deletions TiledCSPlus.Test/assets/TiledCSPlus_test.tiled-project
Original file line number Diff line number Diff line change
@@ -1,14 +1,58 @@
{
"automappingRulesFile": "",
"commands": [
],
"compatibilityVersion": 1100,
"extensionsPath": "extensions",
"folders": [
"."
],
"properties": [
],
"propertyTypes": [
]
}
{
"automappingRulesFile": "",
"commands": [
],
"compatibilityVersion": 1100,
"extensionsPath": "extensions",
"folders": [
"."
],
"properties": [
],
"propertyTypes": [
{
"id": 2,
"name": "cc",
"storageType": "string",
"type": "enum",
"values": [
"a",
"b",
"c",
"d",
"cc_5"
],
"valuesAsFlags": true
},
{
"color": "#ffa0a0a4",
"drawFill": true,
"id": 1,
"members": [
{
"name": "a",
"type": "string",
"value": ""
},
{
"name": "b",
"type": "string",
"value": ""
}
],
"name": "Class0",
"type": "class",
"useAs": [
"property",
"map",
"layer",
"object",
"tile",
"tileset",
"wangcolor",
"wangset",
"project"
]
}
]
}
23 changes: 18 additions & 5 deletions TiledCSPlus.Test/assets/TiledCSPlus_test.tiled-session
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,32 @@
},
"tilemap1.10.tmx": {
"scale": 2.743125,
"selectedLayer": 0,
"selectedLayer": 1,
"viewCenter": {
"x": 79.83595352016405,
"y": 79.65367965367966
"x": 434.5408976987925,
"y": 280.8840282524493
}
},
"tilemap1.10.tmx#tileset-embedded110": {
"scaleInDock": 1
},
"tilemap1.8.tmx": {
"scale": 7.2378125,
"selectedLayer": 1,
"viewCenter": {
"x": 124.62328914986398,
"y": 127.17931004706188
}
},
"tilemap1.8.tmx#tileset-embedded19": {
"scaleInDock": 1
},
"tilemap1.9.tmx": {
"scale": 3.645625,
"selectedLayer": 1,
"viewCenter": {
"x": 295.1482941882394,
"y": 181.86182067546721
"x": 80.09600548602779,
"y": 80.23315618035318
}
},
"tilemap1.9.tmx#tileset-embedded": {
Expand Down Expand Up @@ -95,10 +106,12 @@
"tilemap1.10.tmx"
],
"project": "TiledCSPlus_test.tiled-project",
"property.type": "cc",
"recentFiles": [
"tilemap1.10.tmx",
"tileset.tsx",
"tilemap1.9.tmx",
"tilemap1.8.tmx",
"D:/Projects/csharp/MelonEngine/MelonEngine.Common/data/levels/map01.tmx",
"D:/Projects/csharp/TiledCSPlus/TiledCSPlus.Example/assets/tilemap.tmx",
"tilemap.tmx"
Expand Down
17 changes: 16 additions & 1 deletion TiledCSPlus.Test/assets/tilemap1.10.tmx
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="16" tileheight="16" infinite="0" backgroundcolor="#04010203" nextlayerid="5" nextobjectid="2">
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="16" tileheight="16" infinite="0" backgroundcolor="#04010203" nextlayerid="5" nextobjectid="4">
<properties>
<property name="a" type="class" propertytype="Class0">
<properties>
<property name="a" value="1"/>
<property name="b" value="2"/>
</properties>
</property>
<property name="b" propertytype="cc" value="a,c,cc_5"/>
</properties>
<tileset firstgid="1" source="tileset.tsx"/>
<tileset firstgid="2" name="tileset-embedded110" tilewidth="16" tileheight="16" tilecount="1" columns="1">
<image source="tileset.png" width="16" height="16"/>
Expand All @@ -11,6 +20,12 @@
</layer>
<objectgroup id="2" name="Object Layer 1">
<object id="1" type="test110" x="14.8123" y="15.0866" width="129.196" height="129.196"/>
<object id="2" x="59.4213" y="7.29095">
<polygon points="0,0 49.5785,-0.364548 48.8494,25.1538 4.37457,21.5083"/>
</object>
<object id="3" x="50.3076" y="148.371">
<polyline points="0,0 47.7558,3.64548"/>
</object>
</objectgroup>
<imagelayer id="3" name="Image Layer 1"/>
<group id="4" name="Group Layer 1"/>
Expand Down
8 changes: 7 additions & 1 deletion TiledCSPlus.Test/assets/tilemap1.9.tmx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.9" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="16" tileheight="16" infinite="0" backgroundcolor="#04010203" nextlayerid="5" nextobjectid="2">
<map version="1.9" tiledversion="1.9.2" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="16" tileheight="16" infinite="0" backgroundcolor="#04010203" nextlayerid="5" nextobjectid="4">
<tileset firstgid="1" source="tileset.tsx"/>
<tileset firstgid="2" name="tileset-embedded19" tilewidth="16" tileheight="16" tilecount="1" columns="1">
<image source="tileset.png" width="16" height="16"/>
Expand All @@ -11,6 +11,12 @@
</layer>
<objectgroup id="2" name="Object Layer 1">
<object id="1" class="test19" x="14.8123" y="15.0866" width="129.196" height="129.196"/>
<object id="2" x="54.8603" y="5.48603">
<polygon points="10.4235,7.40614 34.2877,1.09721 34.0134,16.1838 0.548603,16.4581"/>
</object>
<object id="3" x="23.3156" y="153.609">
<polyline points="0,0 80.3703,2.46871"/>
</object>
</objectgroup>
<imagelayer id="3" name="Image Layer 1"/>
<group id="4" name="Group Layer 1"/>
Expand Down
18 changes: 18 additions & 0 deletions TiledCSPlus/TiledMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ private TiledObject[] ParseObjects(XmlNodeList nodeList)
{
var nodesProperty = node.SelectNodes("properties/property");
var nodePolygon = node.SelectSingleNode("polygon");
var nodePolyline = node.SelectSingleNode("polyline");
var nodePoint = node.SelectSingleNode("point");
var nodeEllipse = node.SelectSingleNode("ellipse");
var attrGid = node.Attributes["gid"];
Expand Down Expand Up @@ -660,6 +661,23 @@ private TiledObject[] ParseObjects(XmlNodeList nodeList)
obj.Polygon = polygon;
}

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

var polyline = new TiledPolyline() { Points = new Vector2[vertices.Length] };

for (var i = 0; i < vertices.Length; i++)
{
polyline.Points[i] =
new Vector2(float.Parse(vertices[i].Split(',')[0], CultureInfo.InvariantCulture),
float.Parse(vertices[i].Split(',')[1], CultureInfo.InvariantCulture));
}

obj.Polyline = polyline;
}

if(nodeEllipse != null)
{
obj.Ellipse = new TiledEllipse();
Expand Down
16 changes: 16 additions & 0 deletions TiledCSPlus/TiledModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ public class TiledObject
/// </summary>
public TiledPolygon Polygon { get; internal set; }

/// <summary>
/// If an object was set to a polyline shape, this property will be set and can be used to access the polyline's data
/// </summary>
public TiledPolyline Polyline { get; internal set; }

/// <summary>
/// If an object was set to a point shape, this property will be set
/// </summary>
Expand All @@ -248,6 +253,17 @@ public class TiledPolygon
public Vector2[] Points { get; internal set; }
}

/// <summary>
/// Represents a poly line shape
/// </summary>
public class TiledPolyline
{
/// <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 Vector2[] Points { get; internal set; }
}

/// <summary>
/// Represents a point shape
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions TiledCSPlus/TiledTileset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ private TiledObject[] ParseObjects(XmlNodeList nodeList)
{
var nodesProperty = node.SelectNodes("properties/property");
var nodePolygon = node.SelectSingleNode("polygon");
var nodePolyline = node.SelectSingleNode("polyline");
var nodePoint = node.SelectSingleNode("point");
var nodeEllipse = node.SelectSingleNode("ellipse");

Expand Down Expand Up @@ -335,6 +336,23 @@ private TiledObject[] ParseObjects(XmlNodeList nodeList)
obj.Polygon = polygon;
}

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

var polyline = new TiledPolyline() { Points = new Vector2[vertices.Length] };

for (var i = 0; i < vertices.Length; i++)
{
polyline.Points[i] =
new Vector2(float.Parse(vertices[i].Split(',')[0], CultureInfo.InvariantCulture),
float.Parse(vertices[i].Split(',')[1], CultureInfo.InvariantCulture));
}

obj.Polyline = polyline;
}

if(nodeEllipse != null)
{
obj.Ellipse = new TiledEllipse();
Expand Down

0 comments on commit 3a2d9a5

Please sign in to comment.