From c5ab6ac7a395cc08f915607c7e6d7f6d69d2c769 Mon Sep 17 00:00:00 2001 From: Kurtis Melby Date: Mon, 4 Dec 2023 23:09:07 -0500 Subject: [PATCH 1/4] feat: adding autoResetTicker option to SpriteAnimationGroupComponent --- .../sprite_animation_group_component.dart | 12 ++++ ...sprite_animation_group_component_test.dart | 60 +++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/packages/flame/lib/src/components/sprite_animation_group_component.dart b/packages/flame/lib/src/components/sprite_animation_group_component.dart index dee4c7aebfd..b529ab4a89b 100644 --- a/packages/flame/lib/src/components/sprite_animation_group_component.dart +++ b/packages/flame/lib/src/components/sprite_animation_group_component.dart @@ -29,6 +29,10 @@ class SpriteAnimationGroupComponent extends PositionComponent /// size of current animation sprite. bool _autoResize; + /// Whether the current animation's ticker should reset to the beginning + /// when it becomes current. + bool autoResetTicker; + /// Creates a component with an empty animation which can be set later SpriteAnimationGroupComponent({ Map? animations, @@ -36,6 +40,7 @@ class SpriteAnimationGroupComponent extends PositionComponent bool? autoResize, this.playing = true, this.removeOnFinish = const {}, + this.autoResetTicker = false, Paint? paint, super.position, super.size, @@ -83,6 +88,7 @@ class SpriteAnimationGroupComponent extends PositionComponent bool? autoResize, bool playing = true, Map removeOnFinish = const {}, + bool autoResetTicker = false, Paint? paint, Vector2? position, Vector2? size, @@ -104,6 +110,7 @@ class SpriteAnimationGroupComponent extends PositionComponent current: current, autoResize: autoResize, removeOnFinish: removeOnFinish, + autoResetTicker: autoResetTicker, playing: playing, paint: paint, position: position, @@ -125,8 +132,13 @@ class SpriteAnimationGroupComponent extends PositionComponent /// /// Will update [size] if [autoResize] is true. set current(T? value) { + final changed = value != current; _current = value; _resizeToSprite(); + + if (changed && autoResetTicker) { + animationTicker?.reset(); + } } /// Returns the map of animation state and their corresponding animations. diff --git a/packages/flame/test/components/sprite_animation_group_component_test.dart b/packages/flame/test/components/sprite_animation_group_component_test.dart index b025f6a2823..e01c83ac54c 100644 --- a/packages/flame/test/components/sprite_animation_group_component_test.dart +++ b/packages/flame/test/components/sprite_animation_group_component_test.dart @@ -353,4 +353,64 @@ Future main() async { expect(sizeChangeCounter, equals(4)); }); }); + + group('SpriteAnimationGroupComponent.autoResetTicker', () { + final sprite1 = Sprite(image, srcSize: Vector2.all(76)); + final sprite2 = Sprite(image, srcSize: Vector2.all(15)); + final animation1 = SpriteAnimation.spriteList( + List.filled(5, sprite1), + stepTime: 0.1, + loop: false, + ); + final animation2 = SpriteAnimation.spriteList( + List.filled(5, sprite2), + stepTime: 0.1, + loop: false, + ); + + test('does not reset ticker by default', () { + final component = SpriteAnimationGroupComponent<_AnimationState>( + animations: { + _AnimationState.idle: animation1, + _AnimationState.running: animation2, + }, + current: _AnimationState.idle, + ); + component.update(0.9); + expect(component.animationTicker!.currentIndex, 4); + + component.current = _AnimationState.running; + component.update(0.1); + expect(component.animationTicker!.currentIndex, 1); + + component.current = _AnimationState.idle; + expect(component.animationTicker!.currentIndex, 4); + + component.current = _AnimationState.running; + expect(component.animationTicker!.currentIndex, 1); + }); + + test('resets the ticker when enaled', () { + final component = SpriteAnimationGroupComponent<_AnimationState>( + animations: { + _AnimationState.idle: animation1, + _AnimationState.running: animation2, + }, + autoResetTicker: true, + current: _AnimationState.idle, + ); + component.update(0.9); + expect(component.animationTicker!.currentIndex, 4); + + component.current = _AnimationState.running; + component.update(0.1); + expect(component.animationTicker!.currentIndex, 1); + + component.current = _AnimationState.idle; + expect(component.animationTicker!.currentIndex, 0); + + component.current = _AnimationState.running; + expect(component.animationTicker!.currentIndex, 0); + }); + }); } From 598e08d7e9b8f2005138ca9c556752f2a23578d7 Mon Sep 17 00:00:00 2001 From: Kurtis Melby Date: Mon, 4 Dec 2023 23:58:11 -0500 Subject: [PATCH 2/4] fixing formatting --- .../lib/src/components/sprite_animation_group_component.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/flame/lib/src/components/sprite_animation_group_component.dart b/packages/flame/lib/src/components/sprite_animation_group_component.dart index b529ab4a89b..715a36601fa 100644 --- a/packages/flame/lib/src/components/sprite_animation_group_component.dart +++ b/packages/flame/lib/src/components/sprite_animation_group_component.dart @@ -29,7 +29,7 @@ class SpriteAnimationGroupComponent extends PositionComponent /// size of current animation sprite. bool _autoResize; - /// Whether the current animation's ticker should reset to the beginning + /// Whether the current animation's ticker should reset to the beginning /// when it becomes current. bool autoResetTicker; From eb97d1a5ffceda1877cfd1b62fb7951db10f2ce3 Mon Sep 17 00:00:00 2001 From: Kurtis Melby Date: Mon, 4 Dec 2023 23:59:01 -0500 Subject: [PATCH 3/4] fixing spelling --- .../test/components/sprite_animation_group_component_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/flame/test/components/sprite_animation_group_component_test.dart b/packages/flame/test/components/sprite_animation_group_component_test.dart index e01c83ac54c..841deca50b7 100644 --- a/packages/flame/test/components/sprite_animation_group_component_test.dart +++ b/packages/flame/test/components/sprite_animation_group_component_test.dart @@ -390,7 +390,7 @@ Future main() async { expect(component.animationTicker!.currentIndex, 1); }); - test('resets the ticker when enaled', () { + test('resets the ticker when enabled', () { final component = SpriteAnimationGroupComponent<_AnimationState>( animations: { _AnimationState.idle: animation1, From 3c3d4e8c15e59b3470b0a064e0087f153a09c62f Mon Sep 17 00:00:00 2001 From: Kurtis Melby Date: Tue, 5 Dec 2023 22:57:09 -0500 Subject: [PATCH 4/4] changing default to true --- .../components/sprite_animation_group_component.dart | 4 ++-- .../sprite_animation_group_component_test.dart | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/flame/lib/src/components/sprite_animation_group_component.dart b/packages/flame/lib/src/components/sprite_animation_group_component.dart index 715a36601fa..df9252d3a36 100644 --- a/packages/flame/lib/src/components/sprite_animation_group_component.dart +++ b/packages/flame/lib/src/components/sprite_animation_group_component.dart @@ -40,7 +40,7 @@ class SpriteAnimationGroupComponent extends PositionComponent bool? autoResize, this.playing = true, this.removeOnFinish = const {}, - this.autoResetTicker = false, + this.autoResetTicker = true, Paint? paint, super.position, super.size, @@ -88,7 +88,7 @@ class SpriteAnimationGroupComponent extends PositionComponent bool? autoResize, bool playing = true, Map removeOnFinish = const {}, - bool autoResetTicker = false, + bool autoResetTicker = true, Paint? paint, Vector2? position, Vector2? size, diff --git a/packages/flame/test/components/sprite_animation_group_component_test.dart b/packages/flame/test/components/sprite_animation_group_component_test.dart index 841deca50b7..53926b04800 100644 --- a/packages/flame/test/components/sprite_animation_group_component_test.dart +++ b/packages/flame/test/components/sprite_animation_group_component_test.dart @@ -368,7 +368,7 @@ Future main() async { loop: false, ); - test('does not reset ticker by default', () { + test('does reset ticker by default', () { final component = SpriteAnimationGroupComponent<_AnimationState>( animations: { _AnimationState.idle: animation1, @@ -384,10 +384,10 @@ Future main() async { expect(component.animationTicker!.currentIndex, 1); component.current = _AnimationState.idle; - expect(component.animationTicker!.currentIndex, 4); + expect(component.animationTicker!.currentIndex, 0); component.current = _AnimationState.running; - expect(component.animationTicker!.currentIndex, 1); + expect(component.animationTicker!.currentIndex, 0); }); test('resets the ticker when enabled', () { @@ -396,7 +396,7 @@ Future main() async { _AnimationState.idle: animation1, _AnimationState.running: animation2, }, - autoResetTicker: true, + autoResetTicker: false, current: _AnimationState.idle, ); component.update(0.9); @@ -407,10 +407,10 @@ Future main() async { expect(component.animationTicker!.currentIndex, 1); component.current = _AnimationState.idle; - expect(component.animationTicker!.currentIndex, 0); + expect(component.animationTicker!.currentIndex, 4); component.current = _AnimationState.running; - expect(component.animationTicker!.currentIndex, 0); + expect(component.animationTicker!.currentIndex, 1); }); }); }