From 005508e2f1ad584ab0e84344461d281e20eaffe2 Mon Sep 17 00:00:00 2001 From: Andre Popovitch Date: Sun, 23 Oct 2022 18:46:55 -0500 Subject: [PATCH] Add `play_unless_already_playing` method to AnimationPlayer --- crates/bevy_animation/src/lib.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/bevy_animation/src/lib.rs b/crates/bevy_animation/src/lib.rs index 8d7cfa9925557..924652c4a9dd4 100644 --- a/crates/bevy_animation/src/lib.rs +++ b/crates/bevy_animation/src/lib.rs @@ -115,7 +115,7 @@ impl Default for AnimationPlayer { impl AnimationPlayer { /// Start playing an animation, resetting state of the player - pub fn play(&mut self, handle: Handle) -> &mut Self { + pub fn start(&mut self, handle: Handle) -> &mut Self { *self = Self { animation_clip: handle, ..Default::default() @@ -123,6 +123,14 @@ impl AnimationPlayer { self } + /// Like `start`, but does nothing if the requested animation is already playing. + pub fn play(&mut self, handle: Handle) -> &mut Self { + if self.animation_clip != handle || self.is_paused() { + self.play(handle); + } + self + } + /// Set the animation to repeat pub fn repeat(&mut self) -> &mut Self { self.repeat = true;