Skip to content

Commit

Permalink
feat: Adding autoResetTicker option to SpriteAnimationGroupComponent (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtome committed Dec 6, 2023
1 parent 2e96dcd commit 001c870
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
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
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);
});
});
}

0 comments on commit 001c870

Please sign in to comment.