Skip to content

Commit 651f3d0

Browse files
Make the component types of the new animation players clonable. (#13736)
# Objective Some use cases might require holding onto the previous state of the animation player for change detection. ## Solution Added `clone` and `copy` implementation to most animation types. Added optimized `clone_from` implementations for the specific use case of holding a `PreviousAnimationPlayer` component. --------- Co-authored-by: Alice Cecile <[email protected]>
1 parent d45bcfd commit 651f3d0

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

crates/bevy_animation/src/lib.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl Hash for AnimationTargetId {
236236
/// Note that each entity can only be animated by one animation player at a
237237
/// time. However, you can change [`AnimationTarget`]'s `player` property at
238238
/// runtime to change which player is responsible for animating the entity.
239-
#[derive(Clone, Component, Reflect)]
239+
#[derive(Clone, Copy, Component, Reflect)]
240240
#[reflect(Component, MapEntities)]
241241
pub struct AnimationTarget {
242242
/// The ID of this animation target.
@@ -326,7 +326,7 @@ pub enum RepeatAnimation {
326326
/// playing, but is presently paused.
327327
///
328328
/// An stopped animation is considered no longer active.
329-
#[derive(Debug, Reflect)]
329+
#[derive(Debug, Clone, Copy, Reflect)]
330330
pub struct ActiveAnimation {
331331
/// The factor by which the weight from the [`AnimationGraph`] is multiplied.
332332
weight: f32,
@@ -515,6 +515,21 @@ pub struct AnimationPlayer {
515515
blend_weights: HashMap<AnimationNodeIndex, f32>,
516516
}
517517

518+
// This is needed since `#[derive(Clone)]` does not generate optimized `clone_from`.
519+
impl Clone for AnimationPlayer {
520+
fn clone(&self) -> Self {
521+
Self {
522+
active_animations: self.active_animations.clone(),
523+
blend_weights: self.blend_weights.clone(),
524+
}
525+
}
526+
527+
fn clone_from(&mut self, source: &Self) {
528+
self.active_animations.clone_from(&source.active_animations);
529+
self.blend_weights.clone_from(&source.blend_weights);
530+
}
531+
}
532+
518533
/// The components that we might need to read or write during animation of each
519534
/// animation target.
520535
struct AnimationTargetContext<'a> {

crates/bevy_animation/src/transition.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,23 @@ pub struct AnimationTransitions {
3333
transitions: Vec<AnimationTransition>,
3434
}
3535

36+
// This is needed since `#[derive(Clone)]` does not generate optimized `clone_from`.
37+
impl Clone for AnimationTransitions {
38+
fn clone(&self) -> Self {
39+
Self {
40+
main_animation: self.main_animation,
41+
transitions: self.transitions.clone(),
42+
}
43+
}
44+
45+
fn clone_from(&mut self, source: &Self) {
46+
self.main_animation = source.main_animation;
47+
self.transitions.clone_from(&source.transitions);
48+
}
49+
}
50+
3651
/// An animation that is being faded out as part of a transition
37-
#[derive(Debug, Reflect)]
52+
#[derive(Debug, Clone, Copy, Reflect)]
3853
pub struct AnimationTransition {
3954
/// The current weight. Starts at 1.0 and goes to 0.0 during the fade-out.
4055
current_weight: f32,

0 commit comments

Comments
 (0)