From 14aa98448b45459763f2070c9ca15d32c347b979 Mon Sep 17 00:00:00 2001 From: Munsterlander Date: Sat, 19 Aug 2023 10:29:41 -0700 Subject: [PATCH 1/2] Initial Commit --- .../lib/src/sprite_animation_ticker.dart | 18 +++++++++++++++-- .../test/sprite_animation_ticker_test.dart | 20 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/packages/flame/lib/src/sprite_animation_ticker.dart b/packages/flame/lib/src/sprite_animation_ticker.dart index a6f3c2754f3..289cb7d5214 100644 --- a/packages/flame/lib/src/sprite_animation_ticker.dart +++ b/packages/flame/lib/src/sprite_animation_ticker.dart @@ -47,6 +47,16 @@ class SpriteAnimationTicker { /// still image). bool get isSingleFrame => spriteAnimation.frames.length == 1; + bool _paused = false; + + /// Returns current value of paused flag. + bool get isPaused => _paused; + + /// Sets the given value of paused flag. + set paused(bool value) { + _paused = value; + } + /// A future that will complete when the animation completes. /// /// An animation is considered to be completed if it reaches its [isLastFrame] @@ -68,6 +78,7 @@ class SpriteAnimationTicker { currentIndex = 0; _done = false; _started = false; + _paused = false; // Reset completeCompleter if it's already completed if (completeCompleter?.isCompleted ?? false) { @@ -111,9 +122,12 @@ class SpriteAnimationTicker { /// calls to [onStart]. bool _started = false; - /// Updates this animation, ticking the lifeTime by an amount [dt] - /// (in seconds). + /// Updates this animation, if not paused, ticking the lifeTime by an amount + /// [dt] (in seconds). void update(double dt) { + if (_paused) { + return; + } clock += dt; elapsed += dt; if (_done) { diff --git a/packages/flame/test/sprite_animation_ticker_test.dart b/packages/flame/test/sprite_animation_ticker_test.dart index ab1c1fe8a12..2fab9d91399 100644 --- a/packages/flame/test/sprite_animation_ticker_test.dart +++ b/packages/flame/test/sprite_animation_ticker_test.dart @@ -162,5 +162,25 @@ void main() { animationTicker.completed; expect(animationTicker.completeCompleter!.isCompleted, false); }); + + test('paused pauses ticket', () async { + final sprite = MockSprite(); + final animationTicker = SpriteAnimation.spriteList( + [sprite, sprite], + stepTime: 1, + loop: false, + ).createTicker(); + + expect(animationTicker.isPaused, false); + expect(animationTicker.currentIndex, 0); + animationTicker.update(1); + expect(animationTicker.currentIndex, 1); + animationTicker.paused = true; + expect(animationTicker.isPaused, true); + expect(animationTicker.currentIndex, 1); + animationTicker.reset(); + expect(animationTicker.currentIndex, 0); + expect(animationTicker.isPaused, false); + }); }); } From 21ad213538fc590f6064d81db14cc9db483c90a8 Mon Sep 17 00:00:00 2001 From: Munsterlander Date: Sat, 19 Aug 2023 12:52:47 -0700 Subject: [PATCH 2/2] Added missing update --- packages/flame/test/sprite_animation_ticker_test.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/flame/test/sprite_animation_ticker_test.dart b/packages/flame/test/sprite_animation_ticker_test.dart index 2fab9d91399..e12c798547a 100644 --- a/packages/flame/test/sprite_animation_ticker_test.dart +++ b/packages/flame/test/sprite_animation_ticker_test.dart @@ -177,6 +177,7 @@ void main() { expect(animationTicker.currentIndex, 1); animationTicker.paused = true; expect(animationTicker.isPaused, true); + animationTicker.update(1); expect(animationTicker.currentIndex, 1); animationTicker.reset(); expect(animationTicker.currentIndex, 0);