Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
fix: RenderableTiledMap to use images cache
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago committed Mar 11, 2024
1 parent dd24d25 commit 1347bdc
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion packages/trashy_road/lib/src/game/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ class TrashyRoadGame extends FlameGame
FutureOr<void> onLoad() async {
await super.onLoad();

final tiledMap = _gameBloc.state.map..transformTileImagePaths();
final renderableTiledMap = await RenderableTiledMap.fromTiledMap(
_gameBloc.state.map,
tiledMap,
GameSettings.gridDimensions,
images: images,
);
final tiled = TiledComponent(renderableTiledMap);
final trashyRoadWorld =
Expand Down Expand Up @@ -152,3 +154,36 @@ class TrashyRoadGame extends FlameGame
camera.update(dt);
}
}

extension on TiledMap {
void transformTileImagePaths() {
for (final tileset in tilesets) {
for (final tile in tileset.tiles) {
final tileImage = tile.image;
if (tileImage == null) continue;
final newTileImage = tileImage.copyWith(
source: tileImage.source?.replaceFirst('..', 'assets'),
);
tile.image = newTileImage;
}
}
}
}

extension on TiledImage {
TiledImage copyWith({
String? source,
String? format,
int? width,
int? height,
String? trans,
}) {
return TiledImage(
source: source ?? this.source,
format: format ?? this.format,
width: width ?? this.width,
height: height ?? this.height,
trans: trans ?? this.trans,
);
}
}

0 comments on commit 1347bdc

Please sign in to comment.