diff --git a/src/lib.rs b/src/lib.rs index 3b4f1c9..2e068bd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -421,6 +421,14 @@ macro_rules! animator_impl { self } + /// Indicate whether the Animator component should be removed from entity + /// after reaching [`TweenState::Completed`]. + #[must_use] + pub fn with_remove_on_completed(mut self, remove: bool) -> Self { + self.remove_on_completed = remove; + self + } + /// Set the animation speed. Defaults to 1. /// /// A speed of 2 means the animation will run twice as fast while a speed of 0.1 @@ -476,6 +484,7 @@ pub struct Animator { pub state: AnimatorState, tweenable: BoxedTweenable, speed: f32, + remove_on_completed: bool, } impl std::fmt::Debug for Animator { @@ -494,9 +503,17 @@ impl Animator { state: default(), tweenable: Box::new(tween), speed: 1., + remove_on_completed: false, } } + /// Create a new animator component from a single tweenable. + /// After [`TweenState::Completed`] reached the component is removed from entity + #[must_use] + pub fn new_self_removing(tween: impl Tweenable + 'static) -> Self { + Self::new(tween).with_remove_on_completed(true) + } + animator_impl!(); } @@ -511,6 +528,7 @@ pub struct AssetAnimator { pub state: AnimatorState, tweenable: BoxedTweenable, speed: f32, + remove_on_completed: bool, } #[cfg(feature = "bevy_asset")] @@ -531,6 +549,19 @@ impl AssetAnimator { state: default(), tweenable: Box::new(tween), speed: 1., + remove_on_completed: false, + } + } + + /// Create a new animator component from a single tweenable. + /// After [`TweenState::Completed`] reached the component is removed from entity + #[must_use] + pub fn new_self_removing(tween: impl Tweenable + 'static) -> Self { + Self { + state: default(), + tweenable: Box::new(tween), + speed: 1., + remove_on_completed: true, } } diff --git a/src/plugin.rs b/src/plugin.rs index b8a0db8..de33c25 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -2,7 +2,7 @@ use bevy::prelude::*; #[cfg(feature = "bevy_asset")] use crate::{tweenable::AssetTarget, AssetAnimator}; -use crate::{tweenable::ComponentTarget, Animator, AnimatorState, TweenCompleted}; +use crate::{tweenable::ComponentTarget, Animator, AnimatorState, TweenCompleted, TweenState}; /// Plugin to add systems related to tweening of common components and assets. /// @@ -88,18 +88,22 @@ pub fn component_animator_system( time: Res