Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add isFirstFrame and onStart event to SpriteAnimation #1492

Merged
merged 43 commits into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
056c4f3
Added isFirstFrame and onStart method
munsterlander Mar 26, 2022
0548a93
Merge branch 'flame-engine:main' into main
munsterlander Mar 26, 2022
4f16b63
Add onStart test
munsterlander Mar 26, 2022
03fbeca
Fixed formatting.
munsterlander Mar 27, 2022
d36c7a3
Merge branch 'main' into main
munsterlander Mar 27, 2022
a385db9
Fixed test - bad copy / paste.
munsterlander Mar 27, 2022
8a3b407
Merge remote-tracking branch 'origin/main' into main
munsterlander Mar 27, 2022
9edc13d
Merge branch 'main' into main
munsterlander Mar 27, 2022
edc8ab9
Merge branch 'main' into main
wolfenrain Mar 30, 2022
e07e824
Merge branch 'main' into main
spydon Apr 12, 2022
8388fd7
Merge branch 'main' into main
munsterlander Apr 17, 2022
150a463
added onFrame method and updated test.
munsterlander Apr 18, 2022
4e918d4
Merge remote-tracking branch 'origin/main' into main
munsterlander Apr 18, 2022
bca4f04
Merge branch 'main' into main
munsterlander Apr 18, 2022
d7b8a02
Added flag to prevent multiple calls to onStart
munsterlander Apr 19, 2022
1cc4730
Merge remote-tracking branch 'origin/main' into main
munsterlander Apr 19, 2022
2f2bbd9
Added call to reset _started on reset();
munsterlander Apr 19, 2022
7c41bb8
Fixing > 80 chars per line for melos.
munsterlander Apr 19, 2022
055f97f
Merge branch 'main' into main
munsterlander Apr 20, 2022
4739fc4
Merge branch 'main' into main
munsterlander Apr 21, 2022
b3edaa1
Moving onStart call outside of the while loop as per @spydon recommen…
munsterlander Apr 21, 2022
44eed63
Merge remote-tracking branch 'origin/main' into main
munsterlander Apr 21, 2022
df137c9
Updated the tests
munsterlander Apr 21, 2022
27751ab
Merge branch 'main' into main
munsterlander Apr 21, 2022
92335c5
Update packages/flame/lib/src/sprite_animation.dart
munsterlander Apr 21, 2022
0536a97
Updated 2 of the 3 requested tests.
munsterlander Apr 22, 2022
248393d
Clarified test statement and added final check. (not certain about t…
munsterlander Apr 22, 2022
89c486f
formatting
munsterlander Apr 22, 2022
e330802
trailing comma missing even after flutter format
munsterlander Apr 22, 2022
28a57cf
formatting. melos....
munsterlander Apr 22, 2022
b558fd6
Added tests, provided clarification and verification that events star…
munsterlander Apr 22, 2022
f3c8030
Merge branch 'main' of https://github.com/flame-engine/flame into fla…
munsterlander Apr 22, 2022
8681262
Update packages/flame/test/sprite_animation_test.dart
munsterlander Apr 22, 2022
80cede5
Update packages/flame/test/sprite_animation_test.dart
munsterlander Apr 22, 2022
8cdcc3f
Update packages/flame/test/sprite_animation_test.dart
munsterlander Apr 22, 2022
683f269
Merge remote-tracking branch 'origin/main' into main
munsterlander Apr 22, 2022
2ecc7e1
Merge branch 'main' of https://github.com/flame-engine/flame into fla…
munsterlander Apr 22, 2022
3ef4452
formatting. lines over 80
munsterlander Apr 22, 2022
ddc95a3
formatting. yet again.
munsterlander Apr 22, 2022
5d0f93f
another 80 liner because of how flutter format . works
munsterlander Apr 22, 2022
65d60f8
Merge branch 'main' into main
munsterlander Apr 22, 2022
65c6c12
Ok, all tests passing and fixed calls so it fires at beginning of frame
munsterlander Apr 23, 2022
5479fae
Merge remote-tracking branch 'origin/main' into main
munsterlander Apr 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/flame/lib/src/sprite_animation.dart
Expand Up @@ -230,12 +230,21 @@ class SpriteAnimation {
/// to the first, or keeps returning the last when done.
bool loop = true;

/// Registered method to be triggered when the animation starts.
void Function()? onStart;

/// Registered method to be triggered when the animation frame updates.
void Function(int currentIndex)? onFrame;

/// Registered method to be triggered when the animation complete.
void Function()? onComplete;

/// The current frame that should be displayed.
SpriteAnimationFrame get currentFrame => frames[currentIndex];

/// Returns whether the animation is on the first frame.
bool get isFirstFrame => currentIndex == 0;

/// Returns whether the animation is on the last frame.
bool get isLastFrame => currentIndex == frames.length - 1;

Expand Down Expand Up @@ -265,6 +274,7 @@ class SpriteAnimation {
elapsed = 0.0;
currentIndex = 0;
_done = false;
_started = false;
}

/// Sets this animation to be on the last frame.
Expand All @@ -291,6 +301,10 @@ class SpriteAnimation {
bool _done = false;
bool done() => _done;

/// Local flag to determine if the animation has started to prevent multiple
/// calls to [onStart].
bool _started = false;
munsterlander marked this conversation as resolved.
Show resolved Hide resolved

/// Updates this animation, ticking the lifeTime by an amount [dt]
/// (in seconds).
void update(double dt) {
Expand All @@ -299,7 +313,12 @@ class SpriteAnimation {
if (_done) {
return;
}
if (!_started) {
onStart?.call();
_started = true;
munsterlander marked this conversation as resolved.
Show resolved Hide resolved
}
while (clock >= currentFrame.stepTime) {
onFrame?.call(currentIndex);
if (isLastFrame) {
if (loop) {
clock -= currentFrame.stepTime;
Expand Down
70 changes: 70 additions & 0 deletions packages/flame/test/sprite_animation_test.dart
Expand Up @@ -41,6 +41,40 @@ void main() {
);
});

test('onStart called for single-frame animation', () {
var counter = 0;
final sprite = MockSprite();
final animation =
SpriteAnimation.spriteList([sprite], stepTime: 1, loop: false)
..onStart = () => counter++;
expect(counter, 0);
animation.update(0.5);
expect(counter, 1);
animation.update(1);
expect(counter, 1);
});

test('onFrame called for a multi-frame animation', () {
var counter = 0;
var i = 0;
const animationLength = 3;
final sprite = MockSprite();
final animation = SpriteAnimation.spriteList(
[sprite, sprite, sprite],
stepTime: 1,
loop: false,
);
animation.onFrame = (index) {
counter++;
};
for (i = 0; i <= animationLength; i++) {
expect(counter, i);
animation.update(1);
}
expect(counter, 3);
expect(i, 4);
});

test('onComplete called for single-frame animation', () {
var counter = 0;
final sprite = MockSprite();
Expand All @@ -55,6 +89,42 @@ void main() {
animation.update(1);
expect(counter, 1);
});

test('test sequence of event lifecycle for an animation', () {
var animationStarted = false;
var animationRunning = false;
var animationComplete = false;
final sprite = MockSprite();
final animation =
SpriteAnimation.spriteList([sprite], stepTime: 1, loop: false);
animation.onStart = () {
expect(animationStarted, false);
expect(animationRunning, false);
expect(animationComplete, false);
animationStarted = true;
};
animation.onFrame = (index) {
if (index == 0) {
expect(animationStarted, true);
expect(animationRunning, false);
expect(animationComplete, false);
}
if (index == 1) {
expect(animationStarted, true);
expect(animationRunning, true);
expect(animationComplete, false);
}
animationRunning = true;
};
animation.onComplete = () {
expect(animationStarted, true);
expect(animationRunning, true);
expect(animationComplete, false);
animationComplete = true;
};
animation.update(1);
expect(animationComplete, true);
});
});
}

Expand Down