Skip to content

Commit dc8ec80

Browse files
committed
Use {start,play}_with_transition instead of changing {start,play} API)
1 parent 5301cc3 commit dc8ec80

File tree

5 files changed

+52
-32
lines changed

5 files changed

+52
-32
lines changed

crates/bevy_animation/src/lib.rs

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -165,45 +165,64 @@ pub struct AnimationPlayer {
165165

166166
impl AnimationPlayer {
167167
/// Start playing an animation, resetting state of the player
168-
/// If `transition_duration` is set, this will use a linear blending
169-
/// between the previous and the new animation to make a smooth transition
170-
pub fn start(
168+
/// This will use a linear blending between the previous and the new animation to make a smooth transition
169+
pub fn start(&mut self, handle: Handle<AnimationClip>) -> &mut Self {
170+
self.animation = PlayingAnimation {
171+
animation_clip: handle,
172+
..Default::default()
173+
};
174+
175+
// We want a hard transition.
176+
// In case any previous transitions are still playing, stop them
177+
self.transitions.clear();
178+
179+
self
180+
}
181+
182+
/// Start playing an animation, resetting state of the player
183+
/// This will use a linear blending between the previous and the new animation to make a smooth transition
184+
pub fn start_with_transition(
171185
&mut self,
172186
handle: Handle<AnimationClip>,
173-
transition_duration: Option<Duration>,
187+
transition_duration: Duration,
174188
) -> &mut Self {
175189
let mut animation = PlayingAnimation {
176190
animation_clip: handle,
177191
..Default::default()
178192
};
179193
std::mem::swap(&mut animation, &mut self.animation);
180-
if let Some(transition_duration) = transition_duration {
181-
// Add the current transition. If other transitions are still ongoing,
182-
// this will keep those transitions running and cause a transition between
183-
// the output of that previous transition to the new animation.
184-
self.transitions.push(AnimationTransition {
185-
current_weight: 1.0,
186-
weight_decline_per_sec: 1.0 / transition_duration.as_secs_f32(),
187-
animation,
188-
});
189-
} else {
190-
// We want a hard transition.
191-
// In case any previous transitions are still playing, stop them
192-
self.transitions.clear();
193-
}
194+
195+
// Add the current transition. If other transitions are still ongoing,
196+
// this will keep those transitions running and cause a transition between
197+
// the output of that previous transition to the new animation.
198+
self.transitions.push(AnimationTransition {
199+
current_weight: 1.0,
200+
weight_decline_per_sec: 1.0 / transition_duration.as_secs_f32(),
201+
animation,
202+
});
203+
194204
self
195205
}
196206

197207
/// Start playing an animation, resetting state of the player, unless the requested animation is already playing.
198208
/// If `transition_duration` is set, this will use a linear blending
199209
/// between the previous and the new animation to make a smooth transition
200-
pub fn play(
210+
pub fn play(&mut self, handle: Handle<AnimationClip>) -> &mut Self {
211+
if self.animation.animation_clip != handle || self.is_paused() {
212+
self.start(handle);
213+
}
214+
self
215+
}
216+
217+
/// Start playing an animation, resetting state of the player, unless the requested animation is already playing.
218+
/// This will use a linear blending between the previous and the new animation to make a smooth transition
219+
pub fn play_with_transition(
201220
&mut self,
202221
handle: Handle<AnimationClip>,
203-
transition_duration: Option<Duration>,
222+
transition_duration: Duration,
204223
) -> &mut Self {
205224
if self.animation.animation_clip != handle || self.is_paused() {
206-
self.start(handle, transition_duration);
225+
self.start_with_transition(handle, transition_duration);
207226
}
208227
self
209228
}

examples/animation/animated_fox.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn setup_scene_once_loaded(
7979
) {
8080
if !*done {
8181
if let Ok(mut player) = player.get_single_mut() {
82-
player.play(animations.0[0].clone_weak(), None).repeat();
82+
player.play(animations.0[0].clone_weak()).repeat();
8383
*done = true;
8484
}
8585
}
@@ -123,9 +123,9 @@ fn keyboard_animation_control(
123123
if keyboard_input.just_pressed(KeyCode::Return) {
124124
*current_animation = (*current_animation + 1) % animations.0.len();
125125
player
126-
.play(
126+
.play_with_transition(
127127
animations.0[*current_animation].clone_weak(),
128-
Some(Duration::from_millis(250)),
128+
Duration::from_millis(250),
129129
)
130130
.repeat();
131131
}

examples/animation/animated_transform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ fn setup(
111111

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

116116
// Create the scene that will be animated
117117
// First entity is the planet

examples/stress_tests/many_foxes.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! animation to stress test skinned meshes.
33
44
use std::f32::consts::PI;
5+
use std::time::Duration;
56

67
use bevy::{
78
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
@@ -190,7 +191,7 @@ fn setup_scene_once_loaded(
190191
) {
191192
if !*done && player.iter().len() == foxes.count {
192193
for mut player in &mut player {
193-
player.play(animations.0[0].clone_weak(), None).repeat();
194+
player.play(animations.0[0].clone_weak()).repeat();
194195
}
195196
*done = true;
196197
}
@@ -266,7 +267,10 @@ fn keyboard_animation_control(
266267

267268
if keyboard_input.just_pressed(KeyCode::Return) {
268269
player
269-
.play(animations.0[*current_animation].clone_weak(), None)
270+
.play_with_transition(
271+
animations.0[*current_animation].clone_weak(),
272+
Duration::from_millis(250),
273+
)
270274
.repeat();
271275
}
272276
}

examples/tools/scene_viewer.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ fn start_animation(
201201
if !*done {
202202
if let Ok(mut player) = player.get_single_mut() {
203203
if let Some(animation) = scene_handle.animations.first() {
204-
player.play(animation.clone_weak(), None).repeat();
204+
player.play(animation.clone_weak()).repeat();
205205
*done = true;
206206
}
207207
}
@@ -233,10 +233,7 @@ fn keyboard_animation_control(
233233
// change the animation the frame after return was pressed
234234
*current_animation = (*current_animation + 1) % scene_handle.animations.len();
235235
player
236-
.play(
237-
scene_handle.animations[*current_animation].clone_weak(),
238-
None,
239-
)
236+
.play(scene_handle.animations[*current_animation].clone_weak())
240237
.repeat();
241238
*changing = false;
242239
}

0 commit comments

Comments
 (0)