Skip to content

Commit

Permalink
feat: Adding group to flame fire atlas (#3245)
Browse files Browse the repository at this point in the history
  • Loading branch information
erickzanardo authored Jul 26, 2024
1 parent ac77e42 commit 0fab444
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 3 deletions.
36 changes: 33 additions & 3 deletions packages/flame_fire_atlas/lib/flame_fire_atlas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -90,14 +90,21 @@ 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<String, dynamic> toJson() {
final json = <String, dynamic>{}
..['id'] = id
..['x'] = x
..['y'] = y
..['w'] = w
..['h'] = h;
..['h'] = h
..['group'] = group;

return json;
}
Expand All @@ -110,20 +117,28 @@ 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<String, dynamic> 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.
@override
Map<String, dynamic> 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}
Expand All @@ -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<String, dynamic> 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,
);
}

Expand All @@ -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
Expand Down
143 changes: 143 additions & 0 deletions packages/flame_fire_atlas/test/flame_fire_atlas_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});
}

0 comments on commit 0fab444

Please sign in to comment.