Skip to content

Commit

Permalink
feat: Add overriding of Images and Bundle in all classes (#2806)
Browse files Browse the repository at this point in the history
Some classes in flame tiled weren't using the optional Images/AssetsBundle class, this address that.
  • Loading branch information
erickzanardo committed Oct 9, 2023
1 parent 2dfe0e5 commit 2df90c9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
4 changes: 3 additions & 1 deletion packages/flame_tiled/lib/src/renderable_tile_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ class RenderableTiledMap {
Images? images,
AssetBundle? bundle,
}) async {
final contents = await Flame.bundle.loadString('$prefix$fileName');
final contents =
await (bundle ?? Flame.bundle).loadString('$prefix$fileName');
return fromString(
contents,
destTileSize,
Expand Down Expand Up @@ -291,6 +292,7 @@ class RenderableTiledMap {
map,
maxX: atlasMaxX,
maxY: atlasMaxY,
images: images,
),
ignoreFlip: ignoreFlip,
images: images,
Expand Down
12 changes: 8 additions & 4 deletions packages/flame_tiled/lib/src/tile_atlas.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:ui';

import 'package:flame/cache.dart';
import 'package:flame/flame.dart';
import 'package:flame/image_composition.dart';
import 'package:flame/sprite.dart';
Expand Down Expand Up @@ -85,6 +86,7 @@ class TiledAtlas {
TiledMap map, {
double? maxX,
double? maxY,
Images? images,
}) async {
final imageList = _onlyTileImages(map).toList();

Expand All @@ -105,7 +107,7 @@ class TiledAtlas {
if (imageList.length == 1) {
// The map contains one image, so its either an atlas already, or a
// really boring map.
final image = (await Flame.images.load(key)).clone();
final image = (await (images ?? Flame.images).load(key)).clone();

// There could be a special case that a concurrent call to this method
// passes the check `if (atlasMap.containsKey(key))` due to the async call
Expand Down Expand Up @@ -139,12 +141,14 @@ class TiledAtlas {

// parallelize the download of images.
await Future.wait([
...imageList.map((tiledImage) => Flame.images.load(tiledImage.source!)),
...imageList.map(
(tiledImage) => (images ?? Flame.images).load(tiledImage.source!),
),
]);

final emptyPaint = Paint();
for (final tiledImage in imageList) {
final image = await Flame.images.load(tiledImage.source!);
final image = await (images ?? Flame.images).load(tiledImage.source!);
final rect = bin.pack(image.width.toDouble(), image.height.toDouble());

pictureRect = pictureRect.expandToInclude(rect);
Expand All @@ -159,7 +163,7 @@ class TiledAtlas {
pictureRect.width.toInt(),
pictureRect.height.toInt(),
);
Flame.images.add(key, image);
(images ?? Flame.images).add(key, image);
return atlasMap[key] = TiledAtlas._(
atlas: image,
offsets: offsetMap,
Expand Down

0 comments on commit 2df90c9

Please sign in to comment.