Skip to content

Commit

Permalink
fix: Compute scale in TileLayers based on native map tile size rather…
Browse files Browse the repository at this point in the history
… than image sizes to support oversized/undersized tiles. (#2634)

Compute isometric tile scale based on the map's tile size rather than the tile image's default size. This fixes an issue where scale was incorrect when those two sizes differed.
  • Loading branch information
chippydip authored Aug 7, 2023
1 parent a35d3a1 commit 1c4d6cd
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class HexagonalTileLayer extends FlameTileLayer {
}

final flips = SimpleFlips.fromFlips(tileGid.flips);
final scale = size.x / src.width;
final scale = size.x / map.tileWidth;
final anchorX = src.width - halfMapTile.x;
final anchorY = src.height - halfMapTile.y;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class IsometricTileLayer extends FlameTileLayer {
.translate(spriteOffset.dx, spriteOffset.dy),
);
final flips = SimpleFlips.fromFlips(tileGid.flips);
final scale = size.x / src.width;
final scale = size.x / map.tileWidth;
final anchorX = src.width - halfMapTile.x;
final anchorY = src.height - halfMapTile.y;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class OrthogonalTileLayer extends FlameTileLayer {
);

final flips = SimpleFlips.fromFlips(tileGid.flips);
final scale = size.x / src.width;
final scale = size.x / map.tileWidth;
final anchorX = src.width - halfMapTile.x;
final anchorY = src.height - halfMapTile.y;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class StaggeredTileLayer extends FlameTileLayer {
}

final flips = SimpleFlips.fromFlips(tileGid.flips);
final scale = size.x / src.width;
final scale = size.x / map.tileWidth;
final anchorX = src.width - halfMapTile.x;
final anchorY = src.height - halfMapTile.y;

Expand Down
11 changes: 11 additions & 0 deletions packages/flame_tiled/test/assets/oversized_tiles_hexagonal.tmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.1" orientation="hexagonal" renderorder="right-down" width="4" height="4" tilewidth="16" tileheight="16" infinite="0" hexsidelength="8" staggeraxis="y" staggerindex="odd" nextlayerid="2" nextobjectid="1">
<tileset firstgid="1" name="0x72_DungeonTilesetII_v1.4" tilewidth="32" tileheight="32" tilecount="256" columns="16">
<image source="0x72_DungeonTilesetII_v1.4.png" width="512" height="512"/>
</tileset>
<layer id="1" name="Tile Layer 1" width="4" height="4">
<data encoding="base64" compression="zlib">
eJxjYEAFrlA6lQE7KIbSJUC8AIgBKkgCMg==
</data>
</layer>
</map>
11 changes: 11 additions & 0 deletions packages/flame_tiled/test/assets/oversized_tiles_isometric.tmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.1" orientation="isometric" renderorder="right-down" width="4" height="4" tilewidth="16" tileheight="16" infinite="0" nextlayerid="2" nextobjectid="1">
<tileset firstgid="1" name="0x72_DungeonTilesetII_v1.4" tilewidth="32" tileheight="32" tilecount="256" columns="16">
<image source="0x72_DungeonTilesetII_v1.4.png" width="512" height="512"/>
</tileset>
<layer id="1" name="Tile Layer 1" width="4" height="4">
<data encoding="base64" compression="zlib">
eJxjYMAOiqB0EpQOhtKFaOoALSgBmQ==
</data>
</layer>
</map>
11 changes: 11 additions & 0 deletions packages/flame_tiled/test/assets/oversized_tiles_orthogonal.tmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.1" orientation="orthogonal" renderorder="right-down" width="4" height="4" tilewidth="16" tileheight="16" infinite="0" nextlayerid="2" nextobjectid="1">
<tileset firstgid="1" name="0x72_DungeonTilesetII_v1.4" tilewidth="32" tileheight="32" tilecount="256" columns="16">
<image source="0x72_DungeonTilesetII_v1.4.png" width="512" height="512"/>
</tileset>
<layer id="1" name="Tile Layer 1" width="4" height="4">
<data encoding="base64" compression="zlib">
eJxjYEAFxVC6hAE7KITSRVAaADKQAcs=
</data>
</layer>
</map>
11 changes: 11 additions & 0 deletions packages/flame_tiled/test/assets/oversized_tiles_staggered.tmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.1" orientation="staggered" renderorder="right-down" width="4" height="4" tilewidth="16" tileheight="16" infinite="0" staggeraxis="y" staggerindex="odd" nextlayerid="2" nextobjectid="1">
<tileset firstgid="1" name="0x72_DungeonTilesetII_v1.4" tilewidth="32" tileheight="32" tilecount="256" columns="16">
<image source="0x72_DungeonTilesetII_v1.4.png" width="512" height="512"/>
</tileset>
<layer id="1" name="Tile Layer 1" width="4" height="4">
<data encoding="base64" compression="zlib">
eJxjYMANDgBxEppYMZQuAeI+IAYANIgCmA==
</data>
</layer>
</map>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions packages/flame_tiled/test/tiled_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -866,4 +866,39 @@ void main() {
});
}
});

group('oversized tiles', () {
late TiledComponent component;
final size = Vector2(16, 16);

for (final mapType in [
'orthogonal',
'isometric',
'hexagonal',
'staggered'
]) {
group(mapType, () {
setUp(() async {
Flame.bundle = TestAssetBundle(
imageNames: [
'0x72_DungeonTilesetII_v1.4.png',
],
stringNames: ['oversized_tiles_$mapType.tmx'],
);
component = await TiledComponent.load(
'oversized_tiles_$mapType.tmx',
size,
);
});

test('renders ($mapType)', () async {
final pngData = await renderMapToPng(component);
await expectLater(
pngData,
matchesGoldenFile('goldens/oversized_tiles_$mapType.png'),
);
});
});
}
});
}

0 comments on commit 1c4d6cd

Please sign in to comment.