Skip to content

Commit

Permalink
fix: delayed animation start when switching to a new animation (#67)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonathan Cornaz <[email protected]>
  • Loading branch information
edwardvear and jcornaz authored Jul 1, 2022
1 parent b6745f8 commit 3c025c0
Showing 1 changed file with 50 additions and 11 deletions.
61 changes: 50 additions & 11 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl SpriteSheetAnimationState {
debug_assert!(animation.has_frames());

let mut frame = animation.frames[self.current_frame % animation.frames.len()];
sprite.index = frame.index;

self.elapsed_in_frame += delta;
while self.elapsed_in_frame >= frame.duration {
Expand Down Expand Up @@ -108,9 +109,6 @@ impl SpriteSheetAnimationState {
frame = animation.frames[self.current_frame];
sprite.index = frame.index;
}
if sprite.index > frame.index {
sprite.index = frame.index;
}

false
}
Expand Down Expand Up @@ -195,6 +193,11 @@ mod tests {
TextureAtlasSprite::new(1)
}

#[fixture]
fn sprite_at_third_frame() -> TextureAtlasSprite {
TextureAtlasSprite::new(2)
}

#[fixture]
fn frame_duration() -> Duration {
Duration::from_secs(1)
Expand Down Expand Up @@ -255,14 +258,27 @@ mod tests {
}

#[rstest]
fn updates_index_if_not_on_expected_index(
fn updates_index_if_less_than_expected_index(
mut state: SpriteSheetAnimationState,
mut sprite_at_second_frame: TextureAtlasSprite,
animation: SpriteSheetAnimation,
mut sprite: TextureAtlasSprite,
frame_duration: Duration,
smaller_duration: Duration,
) {
let animation = SpriteSheetAnimation::from_range(1..=3, frame_duration);
state.update(&mut sprite, &animation, smaller_duration);
assert_eq!(sprite.index, 1);
}

#[rstest]
fn updates_index_if_greater_than_expected_index(
mut state: SpriteSheetAnimationState,
mut sprite_at_third_frame: TextureAtlasSprite,
frame_duration: Duration,
smaller_duration: Duration,
) {
state.update(&mut sprite_at_second_frame, &animation, smaller_duration);
assert_eq!(sprite_at_second_frame.index, 0);
let animation = SpriteSheetAnimation::from_range(1..=3, frame_duration);
state.update(&mut sprite_at_third_frame, &animation, smaller_duration);
assert_eq!(sprite_at_third_frame.index, 1);
}

#[rstest]
Expand Down Expand Up @@ -499,15 +515,38 @@ mod tests {
mod run_once {
use super::*;

mod on_last_frame {
#[fixture]
fn animation(frame_duration: Duration) -> SpriteSheetAnimation {
SpriteSheetAnimation::from_range(0..=1, frame_duration).once()
}

mod on_first_frame {
use super::*;

#[fixture]
fn animation(frame_duration: Duration) -> SpriteSheetAnimation {
SpriteSheetAnimation::from_range(0..=1, frame_duration).once()
fn state() -> SpriteSheetAnimationState {
SpriteSheetAnimationState {
current_frame: 0,
elapsed_in_frame: Duration::from_nanos(500),
going_backward: false,
}
}

#[rstest]
fn final_index_set_if_frames_skipped_past_end(
mut state: SpriteSheetAnimationState,
mut sprite: TextureAtlasSprite,
animation: SpriteSheetAnimation,
frame_duration: Duration,
) {
state.update(&mut sprite, &animation, frame_duration * 4);
assert_eq!(sprite.index, 1);
}
}

mod on_last_frame {
use super::*;

#[fixture]
fn state() -> SpriteSheetAnimationState {
SpriteSheetAnimationState {
Expand Down

0 comments on commit 3c025c0

Please sign in to comment.