diff --git a/TiledCSPlus.Test/TiledMap.cs b/TiledCSPlus.Test/TiledMap.cs index 86eff87..66e505c 100644 --- a/TiledCSPlus.Test/TiledMap.cs +++ b/TiledCSPlus.Test/TiledMap.cs @@ -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] @@ -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); + } } \ No newline at end of file diff --git a/TiledCSPlus.Test/assets/TiledCSPlus_test.tiled-project b/TiledCSPlus.Test/assets/TiledCSPlus_test.tiled-project index 953ae34..192bf2b 100644 --- a/TiledCSPlus.Test/assets/TiledCSPlus_test.tiled-project +++ b/TiledCSPlus.Test/assets/TiledCSPlus_test.tiled-project @@ -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" + ] + } + ] +} diff --git a/TiledCSPlus.Test/assets/TiledCSPlus_test.tiled-session b/TiledCSPlus.Test/assets/TiledCSPlus_test.tiled-session index 82198ca..eb14fde 100644 --- a/TiledCSPlus.Test/assets/TiledCSPlus_test.tiled-session +++ b/TiledCSPlus.Test/assets/TiledCSPlus_test.tiled-session @@ -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": { @@ -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" diff --git a/TiledCSPlus.Test/assets/tilemap1.10.tmx b/TiledCSPlus.Test/assets/tilemap1.10.tmx index e1f718f..0761aa7 100644 --- a/TiledCSPlus.Test/assets/tilemap1.10.tmx +++ b/TiledCSPlus.Test/assets/tilemap1.10.tmx @@ -1,5 +1,14 @@ - + + + + + + + + + + @@ -11,6 +20,12 @@ + + + + + + diff --git a/TiledCSPlus.Test/assets/tilemap1.9.tmx b/TiledCSPlus.Test/assets/tilemap1.9.tmx index fd91369..7ba46b6 100644 --- a/TiledCSPlus.Test/assets/tilemap1.9.tmx +++ b/TiledCSPlus.Test/assets/tilemap1.9.tmx @@ -1,5 +1,5 @@ - + @@ -11,6 +11,12 @@ + + + + + + diff --git a/TiledCSPlus/TiledMap.cs b/TiledCSPlus/TiledMap.cs index c6c976a..690c96c 100644 --- a/TiledCSPlus/TiledMap.cs +++ b/TiledCSPlus/TiledMap.cs @@ -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"]; @@ -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(); diff --git a/TiledCSPlus/TiledModels.cs b/TiledCSPlus/TiledModels.cs index 5779bc5..12d9332 100644 --- a/TiledCSPlus/TiledModels.cs +++ b/TiledCSPlus/TiledModels.cs @@ -226,6 +226,11 @@ public class TiledObject /// public TiledPolygon Polygon { get; internal set; } + /// + /// If an object was set to a polyline shape, this property will be set and can be used to access the polyline's data + /// + public TiledPolyline Polyline { get; internal set; } + /// /// If an object was set to a point shape, this property will be set /// @@ -248,6 +253,17 @@ public class TiledPolygon public Vector2[] Points { get; internal set; } } + /// + /// Represents a poly line shape + /// + public class TiledPolyline + { + /// + /// The array of vertices where each two elements represent an x and y position. Like 'x,y,x,y,x,y,x,y'. + /// + public Vector2[] Points { get; internal set; } + } + /// /// Represents a point shape /// diff --git a/TiledCSPlus/TiledTileset.cs b/TiledCSPlus/TiledTileset.cs index 0c81032..eef180d 100644 --- a/TiledCSPlus/TiledTileset.cs +++ b/TiledCSPlus/TiledTileset.cs @@ -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"); @@ -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();