From 0fab444c3dd9ad8faa1e0e9e702150b950dbf30f Mon Sep 17 00:00:00 2001 From: Erick Date: Fri, 26 Jul 2024 18:36:44 -0300 Subject: [PATCH] feat: Adding group to flame fire atlas (#3245) --- .../lib/flame_fire_atlas.dart | 36 ++++- .../test/flame_fire_atlas_test.dart | 143 ++++++++++++++++++ 2 files changed, 176 insertions(+), 3 deletions(-) diff --git a/packages/flame_fire_atlas/lib/flame_fire_atlas.dart b/packages/flame_fire_atlas/lib/flame_fire_atlas.dart index a0a9edb5760..9fe443e4c8a 100644 --- a/packages/flame_fire_atlas/lib/flame_fire_atlas.dart +++ b/packages/flame_fire_atlas/lib/flame_fire_atlas.dart @@ -73,7 +73,7 @@ abstract class BaseSelection { final Selection _info; /// {@macro _base_selection} - BaseSelection(this._info); + BaseSelection(this._info, {this.group}); /// The id of the selection. String get id => _info.id; @@ -90,6 +90,12 @@ abstract class BaseSelection { /// The height of the selection. int get h => _info.h; + /// A group that this selection belongs to. + final String? group; + + /// Copies this instance with a new group. + BaseSelection copyWithGroup(String? group); + /// Returns this instance as a json. Map toJson() { final json = {} @@ -97,7 +103,8 @@ abstract class BaseSelection { ..['x'] = x ..['y'] = y ..['w'] = w - ..['h'] = h; + ..['h'] = h + ..['group'] = group; return json; } @@ -110,13 +117,15 @@ class SpriteSelection extends BaseSelection { /// {@macro _sprite_selection} SpriteSelection({ required Selection info, + super.group, }) : super(info); /// Creates a [SpriteSelection] from [json]. @override factory SpriteSelection.fromJson(Map json) { final info = Selection.fromJson(json); - return SpriteSelection(info: info); + final group = json['group'] as String?; + return SpriteSelection(info: info, group: group); } /// Returns this instance as a json. @@ -124,6 +133,12 @@ class SpriteSelection extends BaseSelection { Map toJson() { return super.toJson()..['type'] = 'sprite'; } + + /// Copies this instance with a new group. + @override + SpriteSelection copyWithGroup(String? group) { + return SpriteSelection(info: _info, group: group); + } } /// {@template _animation_selection} @@ -145,18 +160,21 @@ class AnimationSelection extends BaseSelection { required this.frameCount, required this.stepTime, required this.loop, + super.group, }) : super(info); /// Creates a [AnimationSelection] from [json]. @override factory AnimationSelection.fromJson(Map json) { final info = Selection.fromJson(json); + final group = json['group'] as String?; return AnimationSelection( info: info, frameCount: json['frameCount'] as int, stepTime: json['stepTime'] as double, loop: json['loop'] as bool, + group: group, ); } @@ -169,6 +187,18 @@ class AnimationSelection extends BaseSelection { ..['loop'] = loop ..['type'] = 'animation'; } + + /// Copies this instance with a new group. + @override + AnimationSelection copyWithGroup(String? group) { + return AnimationSelection( + info: _info, + frameCount: frameCount, + stepTime: stepTime, + loop: loop, + group: group, + ); + } } /// FireAtlas is a mapping file that can hold several [Sprite]s and diff --git a/packages/flame_fire_atlas/test/flame_fire_atlas_test.dart b/packages/flame_fire_atlas/test/flame_fire_atlas_test.dart index d29ccc289a2..0bcb76fafcc 100644 --- a/packages/flame_fire_atlas/test/flame_fire_atlas_test.dart +++ b/packages/flame_fire_atlas/test/flame_fire_atlas_test.dart @@ -170,5 +170,148 @@ void main() { verify(() => game.images.fromBase64(any(), any())).called(1); }, ); + + group('SpriteSelection', () { + test('can be created from a json', () async { + final sprite = SpriteSelection.fromJson({ + 'id': 'kinetic_bullet', + 'x': 0, + 'y': 0, + 'w': 16, + 'h': 16, + 'group': 'bullets', + }); + expect(sprite.id, 'kinetic_bullet'); + expect(sprite.x, 0); + expect(sprite.y, 0); + expect(sprite.w, 16); + expect(sprite.h, 16); + expect(sprite.group, 'bullets'); + }); + + test('serializes to json', () async { + final sprite = SpriteSelection( + info: Selection( + id: 'kinetic_bullet', + x: 0, + y: 0, + w: 16, + h: 16, + ), + group: 'bullets', + ); + final json = sprite.toJson(); + expect(json['id'], 'kinetic_bullet'); + expect(json['x'], 0); + expect(json['y'], 0); + expect(json['w'], 16); + expect(json['h'], 16); + expect(json['group'], 'bullets'); + }); + + group('copyWithGroup', () { + test('creates a copy with a new group', () async { + final sprite = SpriteSelection( + info: Selection( + id: 'kinetic_bullet', + x: 0, + y: 0, + w: 16, + h: 16, + ), + group: 'bullets', + ); + + final copy = sprite.copyWithGroup('new_group'); + expect(copy.group, 'new_group'); + expect(copy.id, 'kinetic_bullet'); + expect(copy.x, 0); + expect(copy.y, 0); + expect(copy.w, 16); + expect(copy.h, 16); + }); + }); + }); + + group('AnimationSelection', () { + test('can be created from a json', () async { + final animation = AnimationSelection.fromJson({ + 'id': 'bomb_ptero', + 'frameCount': 1, + 'stepTime': .2, + 'loop': true, + 'x': 0, + 'y': 0, + 'w': 16, + 'h': 16, + 'group': 'enemies', + }); + + expect(animation.id, 'bomb_ptero'); + expect(animation.frameCount, 1); + expect(animation.stepTime, .2); + expect(animation.loop, isTrue); + expect(animation.x, 0); + expect(animation.y, 0); + expect(animation.w, 16); + expect(animation.h, 16); + expect(animation.group, 'enemies'); + }); + + test('serializes to json', () async { + final animation = AnimationSelection( + info: Selection( + id: 'bomb_ptero', + x: 0, + y: 0, + w: 16, + h: 16, + ), + frameCount: 1, + stepTime: .2, + loop: true, + group: 'enemies', + ); + final json = animation.toJson(); + expect(json['id'], 'bomb_ptero'); + expect(json['frameCount'], 1); + expect(json['stepTime'], .2); + expect(json['loop'], isTrue); + expect(json['x'], 0); + expect(json['y'], 0); + expect(json['w'], 16); + expect(json['h'], 16); + expect(json['group'], 'enemies'); + }); + + group('copyWithGroup', () { + test('creates a copy with a new group', () async { + final animation = AnimationSelection( + info: Selection( + id: 'bomb_ptero', + x: 0, + y: 0, + w: 16, + h: 16, + ), + frameCount: 1, + stepTime: .2, + loop: true, + group: 'enemies', + ); + + final copy = animation.copyWithGroup('new_group'); + expect(copy.group, 'new_group'); + expect(copy.id, 'bomb_ptero'); + expect(copy.frameCount, 1); + expect(copy.stepTime, .2); + expect(copy.loop, isTrue); + expect(copy.x, 0); + expect(copy.y, 0); + expect(copy.w, 16); + expect(copy.h, 16); + }); + }); + }); }); }