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: Adding autoResetTicker option to SpriteAnimationGroupComponent #2899

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ class SpriteAnimationGroupComponent<T> 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<T, SpriteAnimation>? animations,
T? current,
bool? autoResize,
this.playing = true,
this.removeOnFinish = const {},
this.autoResetTicker = true,
Paint? paint,
super.position,
super.size,
Expand Down Expand Up @@ -83,6 +88,7 @@ class SpriteAnimationGroupComponent<T> extends PositionComponent
bool? autoResize,
bool playing = true,
Map<T, bool> removeOnFinish = const {},
bool autoResetTicker = true,
Paint? paint,
Vector2? position,
Vector2? size,
Expand All @@ -104,6 +110,7 @@ class SpriteAnimationGroupComponent<T> extends PositionComponent
current: current,
autoResize: autoResize,
removeOnFinish: removeOnFinish,
autoResetTicker: autoResetTicker,
playing: playing,
paint: paint,
position: position,
Expand All @@ -125,8 +132,13 @@ class SpriteAnimationGroupComponent<T> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,64 @@ Future<void> 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 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, 0);

component.current = _AnimationState.running;
expect(component.animationTicker!.currentIndex, 0);
});

test('resets the ticker when enabled', () {
final component = SpriteAnimationGroupComponent<_AnimationState>(
animations: {
_AnimationState.idle: animation1,
_AnimationState.running: animation2,
},
autoResetTicker: false,
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);
});
});
}