Skip to content

Commit

Permalink
Use {start,play}_with_transition instead of changing {start,play} API)
Browse files Browse the repository at this point in the history
  • Loading branch information
smessmer committed Jan 8, 2023
1 parent dd1c92d commit aa307ec
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
61 changes: 40 additions & 21 deletions crates/bevy_animation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,45 +165,64 @@ pub struct AnimationPlayer {

impl AnimationPlayer {
/// Start playing an animation, resetting state of the player
/// If `transition_duration` is set, this will use a linear blending
/// between the previous and the new animation to make a smooth transition
pub fn start(
/// This will use a linear blending between the previous and the new animation to make a smooth transition
pub fn start(&mut self, handle: Handle<AnimationClip>) -> &mut Self {
self.animation = PlayingAnimation {
animation_clip: handle,
..Default::default()
};

// We want a hard transition.
// In case any previous transitions are still playing, stop them
self.transitions.clear();

self
}

/// Start playing an animation, resetting state of the player
/// This will use a linear blending between the previous and the new animation to make a smooth transition
pub fn start_with_transition(
&mut self,
handle: Handle<AnimationClip>,
transition_duration: Option<Duration>,
transition_duration: Duration,
) -> &mut Self {
let mut animation = PlayingAnimation {
animation_clip: handle,
..Default::default()
};
std::mem::swap(&mut animation, &mut self.animation);
if let Some(transition_duration) = transition_duration {
// Add the current transition. If other transitions are still ongoing,
// this will keep those transitions running and cause a transition between
// the output of that previous transition to the new animation.
self.transitions.push(AnimationTransition {
current_weight: 1.0,
weight_decline_per_sec: 1.0 / transition_duration.as_secs_f32(),
animation,
});
} else {
// We want a hard transition.
// In case any previous transitions are still playing, stop them
self.transitions.clear();
}

// Add the current transition. If other transitions are still ongoing,
// this will keep those transitions running and cause a transition between
// the output of that previous transition to the new animation.
self.transitions.push(AnimationTransition {
current_weight: 1.0,
weight_decline_per_sec: 1.0 / transition_duration.as_secs_f32(),
animation,
});

self
}

/// Start playing an animation, resetting state of the player, unless the requested animation is already playing.
/// If `transition_duration` is set, this will use a linear blending
/// between the previous and the new animation to make a smooth transition
pub fn play(
pub fn play(&mut self, handle: Handle<AnimationClip>) -> &mut Self {
if self.animation.animation_clip != handle || self.is_paused() {
self.start(handle);
}
self
}

/// Start playing an animation, resetting state of the player, unless the requested animation is already playing.
/// This will use a linear blending between the previous and the new animation to make a smooth transition
pub fn play_with_transition(
&mut self,
handle: Handle<AnimationClip>,
transition_duration: Option<Duration>,
transition_duration: Duration,
) -> &mut Self {
if self.animation.animation_clip != handle || self.is_paused() {
self.start(handle, transition_duration);
self.start_with_transition(handle, transition_duration);
}
self
}
Expand Down
6 changes: 3 additions & 3 deletions examples/animation/animated_fox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn setup_scene_once_loaded(
) {
if !*done {
if let Ok(mut player) = player.get_single_mut() {
player.play(animations.0[0].clone_weak(), None).repeat();
player.play(animations.0[0].clone_weak()).repeat();
*done = true;
}
}
Expand Down Expand Up @@ -123,9 +123,9 @@ fn keyboard_animation_control(
if keyboard_input.just_pressed(KeyCode::Return) {
*current_animation = (*current_animation + 1) % animations.0.len();
player
.play(
.play_with_transition(
animations.0[*current_animation].clone_weak(),
Some(Duration::from_millis(250)),
Duration::from_millis(250),
)
.repeat();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/animation/animated_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fn setup(

// Create the animation player, and set it to repeat
let mut player = AnimationPlayer::default();
player.play(animations.add(animation), None).repeat();
player.play(animations.add(animation)).repeat();

// Create the scene that will be animated
// First entity is the planet
Expand Down
8 changes: 6 additions & 2 deletions examples/stress_tests/many_foxes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! animation to stress test skinned meshes.

use std::f32::consts::PI;
use std::time::Duration;

use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
Expand Down Expand Up @@ -190,7 +191,7 @@ fn setup_scene_once_loaded(
) {
if !*done && player.iter().len() == foxes.count {
for mut player in &mut player {
player.play(animations.0[0].clone_weak(), None).repeat();
player.play(animations.0[0].clone_weak()).repeat();
}
*done = true;
}
Expand Down Expand Up @@ -266,7 +267,10 @@ fn keyboard_animation_control(

if keyboard_input.just_pressed(KeyCode::Return) {
player
.play(animations.0[*current_animation].clone_weak(), None)
.play_with_transition(
animations.0[*current_animation].clone_weak(),
Duration::from_millis(250),
)
.repeat();
}
}
Expand Down

0 comments on commit aa307ec

Please sign in to comment.