Skip to content

Commit

Permalink
feat: Exposing atlases for reading in a TiledComponent (#2865)
Browse files Browse the repository at this point in the history
Adds a `componentAtlases` in the `TiledComponent` which can be useful
for debugging purposes, where the developer will be able to inspect the
atlases being used in their game.
  • Loading branch information
erickzanardo committed Nov 23, 2023
1 parent ee11aae commit e1b4d93
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/flame_tiled/lib/src/tiled_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:collection/collection.dart';
import 'package:flame/cache.dart';
import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame_tiled/src/renderable_layers/tile_layers/tile_layer.dart';
import 'package:flame_tiled/src/renderable_tile_map.dart';
import 'package:flame_tiled/src/tile_atlas.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -191,4 +192,21 @@ class TiledComponent<T extends FlameGame> extends PositionComponent
);
}
}

/// Returns a list of all the Atlases that were created for this component.
///
/// This method is useful for debugging purposes as it allows developers to
/// check how the tilesets were packed into the atlas.
///
/// It returns a record with the Atlas key and its image.
List<(String, Image)> atlases() {
return tileMap.renderableLayers
.whereType<FlameTileLayer>()
.where((layer) => layer.tiledAtlas.atlas != null)
.map((layer) {
final image = layer.tiledAtlas.atlas;
final key = layer.tiledAtlas.key;
return (key, image!);
}).toList();
}
}
6 changes: 6 additions & 0 deletions packages/flame_tiled/test/tiled_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ void main() {
expect(tiled.tileMap.renderableLayers.length, equals(3));
});

test('component atlases returns the loaded atlases', () {
final atlases = tiled.atlases();
expect(atlases, hasLength(1));
expect(atlases.first.$1, equals('map-level1.png'));
});

test('correct loads the file, with different prefix', () async {
tiled = await TiledComponent.load(
'map_custom_path.tmx',
Expand Down

0 comments on commit e1b4d93

Please sign in to comment.