Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: support parsing empty tile terrain values #61

Merged
merged 1 commit into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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