Skip to content

Commit 4411eca

Browse files
committed
Read polygon and polyline objects from Tiled maps
The polygon field of MapObject is not public, so the map reader skipped it: every polygon object came as an empty rectangle. In the tiled demo the boat in the water spawned as a Wall without costume or hitbox and showed the missing-costume marker, and never blocked the player. The reader now fills both fields; a polyline's points are a string as Tiled writes them.
1 parent 31f0b53 commit 4411eca

5 files changed

Lines changed: 38 additions & 33 deletions

File tree

‎.changeset/tiled-polygons.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
type: patch
3+
---
4+
5+
Polygon objects of a Tiled map keep their shape: `MapObject.getShape()` and
6+
`TiledMap.spawn` used to see them as an empty rectangle, because the polygon
7+
was never read from the map file. A wall drawn as a polygon now blocks, and a
8+
spawned sprite without a costume no longer shows the missing-costume marker.
9+
Polylines are read as well.

‎src/main/java/org/openpatch/scratch/extensions/tiled/MapObject.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,13 @@ public void setClassName(String className) {
6464
public Object point;
6565

6666
/** A Polygon object representing a polygon shape. */
67+
// read from the map file although it is not public (the reader only fills
68+
// public fields by itself, so polygon objects used to come without their shape)
69+
@com.fasterxml.jackson.annotation.JsonProperty("polygon")
6770
protected Polygon polygon;
6871

6972
/** A Polyline object representing a polyline shape. */
73+
@com.fasterxml.jackson.annotation.JsonProperty("polyline")
7074
protected Polyline polyline;
7175

7276
/**

‎src/main/java/org/openpatch/scratch/extensions/tiled/Point.java‎

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
package org.openpatch.scratch.extensions.tiled;
22

3-
import java.util.List;
4-
53
/**
6-
* The Polyline class represents a polyline object in Tiled. It includes a list
7-
* of points that make
8-
* up the polyline.
4+
* The Polyline class represents a polyline object in Tiled: its points as
5+
* Tiled writes them.
96
*
107
* <p>
118
* Example usage:
129
*
1310
* <pre>{@code
1411
* Polyline p = new Polyline();
15-
* p.points = new ArrayList<Point>();
16-
* p.points.add(new Point(0, 0));
17-
* p.points.add(new Point(10, 10));
12+
* p.points = "0,0 10,10";
1813
* }</pre>
1914
*/
2015
class Polyline {
21-
/** The list of points that make up the polyline. */
22-
public List<Point> points;
16+
/** The points of the polyline as Tiled writes them: "x,y x,y ...". */
17+
public String points;
2318
}

‎src/test/java/org/openpatch/scratch/extensions/tiled/TiledFormatsTest.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
44
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertNotNull;
56
import static org.junit.jupiter.api.Assertions.assertNull;
67
import static org.junit.jupiter.api.Assertions.assertThrows;
78
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -50,6 +51,25 @@ private static byte[] gids(long... values) {
5051
return buffer.array();
5152
}
5253

54+
@Test
55+
void polygonObjectsKeepTheirShape() throws Exception {
56+
Map m = load(map("""
57+
<objectgroup id="2" name="Objects">
58+
<object id="1" type="Wall" x="10" y="20">
59+
<polygon points="0,0 30,0 30,-10"/>
60+
</object>
61+
<object id="2" type="Fence" x="0" y="0">
62+
<polyline points="0,0 5,5"/>
63+
</object>
64+
</objectgroup>
65+
"""));
66+
MapObject wall = TiledMap.objectsFromLayer(m, "Objects")[0];
67+
assertNotNull(wall.polygon, "the polygon is read");
68+
assertEquals(3, wall.polygon.getPoints().length);
69+
assertEquals(org.openpatch.scratch.Polygon.class, wall.getShape().getClass());
70+
assertNotNull(TiledMap.objectsFromLayer(m, "Objects")[1].polyline, "the polyline is read");
71+
}
72+
5373
@Test
5474
void csvWithFlipBitsLoadsAndKeepsTheFlips() throws Exception {
5575
long flipped = 0x80000000L | 3; // horizontal flip of tile 3

0 commit comments

Comments
 (0)