Skip to content

Commit 3c75b03

Browse files
authored
fix!: support parsing empty tile terrain values (#61)
1 parent 31f3081 commit 3c75b03

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

packages/tiled/lib/src/tileset/tile.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Tile {
2424
double probability;
2525

2626
/// List of indexes of the terrain.
27-
List<int> terrain;
27+
List<int?> terrain;
2828

2929
TiledImage? image;
3030
Layer? objectGroup;
@@ -60,7 +60,7 @@ class Tile {
6060
terrain: parser
6161
.getStringOrNull('terrain')
6262
?.split(',')
63-
.map(int.parse)
63+
.map((str) => str.isEmpty ? null : int.parse(str))
6464
.toList() ??
6565
[],
6666
image: parser.getSingleChildOrNullAs('image', TiledImage.parse),
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="32" height="32" tilewidth="16" tileheight="16" nextobjectid="1">
3+
<tileset firstgid="1" name="test tileset" tilewidth="16" tileheight="16" tilecount="256" columns="16">
4+
<image source="tileset_16x16.png" width="256" height="256"/>
5+
<terraintypes>
6+
<terrain name="test bright" tile="2"/>
7+
<terrain name="test normal" tile="50"/>
8+
<terrain name="test dark" tile="98"/>
9+
</terraintypes>
10+
<tile id="0" terrain=",,,0"/>
11+
<tile id="1" terrain="0,0,0,"/>
12+
<tile id="2" terrain="0,0,,"/>
13+
<tile id="3" terrain="0,0,,0"/>
14+
<tile id="4" terrain=",,,0"/>
15+
<tile id="5" terrain=",,0,"/>
16+
<tile id="50" terrain="1,1,,"/>
17+
<tile id="98" terrain="2,2,,"/>
18+
</tileset>
19+
</map>

packages/tiled/test/parser_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,18 @@ void main() {
373373
);
374374
});
375375
});
376+
377+
group('Parser tiles', () {
378+
test('support empty terrain values', () {
379+
final xml = File('./test/fixtures/map_with_empty_terrains.tmx')
380+
.readAsStringSync();
381+
final tiledMap = TileMapParser.parseTmx(xml);
382+
383+
final tileset = tiledMap.tilesets.first;
384+
final tile = tileset.tiles.first;
385+
expect(tile.terrain, anyElement(isNull));
386+
});
387+
});
376388
}
377389

378390
class CustomTsxProvider extends TsxProvider {

0 commit comments

Comments
 (0)