Skip to content

Commit 9a6ca27

Browse files
authored
feat: Add getLayerByName to SpriteFusionTilemapData (#3755)
Introduced a `getLayerByName` method to retrieve a layer by its name in `SpriteFusionTilemapData`.
1 parent 3853567 commit 9a6ca27

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

packages/flame_sprite_fusion/lib/src/sprite_fusion_tilemap_data.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:collection/collection.dart';
12
import 'package:flame_sprite_fusion/flame_sprite_fusion.dart';
23

34
/// A class that holds the data of the tilemap from a sprite fusion.
@@ -36,4 +37,12 @@ class SpriteFusionTilemapData {
3637
.toList(growable: false),
3738
);
3839
}
40+
41+
/// Gets a layer by its [name].
42+
///
43+
/// Returns the first layer found with the given name.
44+
/// For multiple layers with the same name, use [layers] property directly.
45+
SpriteFusionLayerData? getLayerByName(String name) {
46+
return layers.firstWhereOrNull((layer) => layer.name == name);
47+
}
3948
}

packages/flame_sprite_fusion/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ environment:
1616
flutter: ">=3.32.0"
1717

1818
dependencies:
19+
collection: ^1.19.1
1920
flame: ^1.33.0
2021
flutter:
2122
sdk: flutter

packages/flame_sprite_fusion/test/sprite_fusion_data_test.dart

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,90 @@ void main() {
234234
expect(tileData.tileSize, map['tileSize']);
235235
expect(tileData.layers.length, 2);
236236
});
237+
238+
test('getLayerByName returns the correct layer', () {
239+
final tilemapData = SpriteFusionTilemapData(
240+
mapWidth: 10,
241+
mapHeight: 10,
242+
tileSize: 32,
243+
layers: [
244+
SpriteFusionLayerData(
245+
name: 'background',
246+
tiles: [],
247+
collider: false,
248+
),
249+
SpriteFusionLayerData(
250+
name: 'foreground',
251+
tiles: [],
252+
collider: true,
253+
),
254+
],
255+
);
256+
257+
final layer = tilemapData.getLayerByName('foreground');
258+
259+
expect(layer, isNotNull);
260+
expect(layer?.name, 'foreground');
261+
expect(layer?.collider, true);
262+
});
263+
264+
test('getLayerByName returns null for non-existent layer', () {
265+
final tilemapData = SpriteFusionTilemapData(
266+
mapWidth: 10,
267+
mapHeight: 10,
268+
tileSize: 32,
269+
layers: [
270+
SpriteFusionLayerData(
271+
name: 'background',
272+
tiles: [],
273+
collider: false,
274+
),
275+
],
276+
);
277+
278+
final layer = tilemapData.getLayerByName('nonExistent');
279+
280+
expect(layer, isNull);
281+
});
282+
283+
test('getLayerByName returns first layer when multiple have same name', () {
284+
final tilemapData = SpriteFusionTilemapData(
285+
mapWidth: 10,
286+
mapHeight: 10,
287+
tileSize: 32,
288+
layers: [
289+
SpriteFusionLayerData(
290+
name: 'duplicate',
291+
tiles: [SpriteFusionTileData(id: 1, x: 0, y: 0)],
292+
collider: false,
293+
),
294+
SpriteFusionLayerData(
295+
name: 'duplicate',
296+
tiles: [SpriteFusionTileData(id: 2, x: 1, y: 1)],
297+
collider: true,
298+
),
299+
],
300+
);
301+
302+
final layer = tilemapData.getLayerByName('duplicate');
303+
304+
expect(layer, isNotNull);
305+
expect(layer?.name, 'duplicate');
306+
expect(layer?.collider, false);
307+
expect(layer?.tiles.first.id, 1);
308+
});
309+
310+
test('getLayerByName returns null for empty layers list', () {
311+
final tilemapData = SpriteFusionTilemapData(
312+
mapWidth: 10,
313+
mapHeight: 10,
314+
tileSize: 32,
315+
layers: [],
316+
);
317+
318+
final layer = tilemapData.getLayerByName('anyLayer');
319+
320+
expect(layer, isNull);
321+
});
237322
});
238323
}

0 commit comments

Comments
 (0)