Skip to content

Commit

Permalink
fix!: support parsing empty tile terrain values (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
devoncarew authored Oct 31, 2022
1 parent 31f3081 commit 3c75b03
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/tiled/lib/src/tileset/tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Tile {
double probability;

/// List of indexes of the terrain.
List<int> terrain;
List<int?> terrain;

TiledImage? image;
Layer? objectGroup;
Expand Down Expand Up @@ -60,7 +60,7 @@ class Tile {
terrain: parser
.getStringOrNull('terrain')
?.split(',')
.map(int.parse)
.map((str) => str.isEmpty ? null : int.parse(str))
.toList() ??
[],
image: parser.getSingleChildOrNullAs('image', TiledImage.parse),
Expand Down
19 changes: 19 additions & 0 deletions packages/tiled/test/fixtures/map_with_empty_terrains.tmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="32" height="32" tilewidth="16" tileheight="16" nextobjectid="1">
<tileset firstgid="1" name="test tileset" tilewidth="16" tileheight="16" tilecount="256" columns="16">
<image source="tileset_16x16.png" width="256" height="256"/>
<terraintypes>
<terrain name="test bright" tile="2"/>
<terrain name="test normal" tile="50"/>
<terrain name="test dark" tile="98"/>
</terraintypes>
<tile id="0" terrain=",,,0"/>
<tile id="1" terrain="0,0,0,"/>
<tile id="2" terrain="0,0,,"/>
<tile id="3" terrain="0,0,,0"/>
<tile id="4" terrain=",,,0"/>
<tile id="5" terrain=",,0,"/>
<tile id="50" terrain="1,1,,"/>
<tile id="98" terrain="2,2,,"/>
</tileset>
</map>
12 changes: 12 additions & 0 deletions packages/tiled/test/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,18 @@ void main() {
);
});
});

group('Parser tiles', () {
test('support empty terrain values', () {
final xml = File('./test/fixtures/map_with_empty_terrains.tmx')
.readAsStringSync();
final tiledMap = TileMapParser.parseTmx(xml);

final tileset = tiledMap.tilesets.first;
final tile = tileset.tiles.first;
expect(tile.terrain, anyElement(isNull));
});
});
}

class CustomTsxProvider extends TsxProvider {
Expand Down

0 comments on commit 3c75b03

Please sign in to comment.