pub fn custom_ease_system<T: CustomComponentEase + Component>(
    mut commands: Commands,
    time: Res<Time>,
    mut query: Query<(Entity, &mut T)>,
    mut easing_query: Query<&mut EasingComponent<T>>,
    mut chain_query: Query<&mut EasingChainComponent<T>>,
) where
    T: interpolation::Lerp<Scalar = f32> + Default,
{
    for (entity, mut object) in query.iter_mut() {
        if let Ok(ref mut easing) = easing_query.get_mut(entity) {
            if easing.state == EasingState::Play {
                easing.timer.tick(time.delta());
            }
            if easing.paused {
                if easing.timer.just_finished() {
                    match easing.easing_type {
                        EasingType::Once { duration } => {
                            easing.timer.set_duration(duration);
                        }
                        EasingType::Loop { duration, .. } => {
                            easing.timer.set_duration(duration);
                        }
                        EasingType::PingPong { duration, .. } => {
                            easing.timer.set_duration(duration);
                        }
                    }
                    easing.timer.reset();
                    easing.paused = false;
                }
            } else {
                if easing.timer.duration().as_secs_f32() != 0. {
                    let progress = if easing.direction.is_positive() {
                        easing.timer.percent()
                    } else {
                        easing.timer.percent_left()
                    };
                    let factor = progress.compute(easing.ease_function);
                    if let Some(ref start) = easing.start {
                        *object = interpolation::lerp(&start.0, &easing.end.0, &factor);
                    } else {
                        *object = interpolation::lerp(&T::default(), &easing.end.0, &factor);
                    }
                }
                if easing.timer.finished() {
                    match easing.easing_type {
                        EasingType::Once { .. } => {
                            commands.entity(entity).remove::<EasingComponent<T>>();
                        }
                        EasingType::Loop { pause, .. } => {
                            if let Some(pause) = pause {
                                easing.timer.set_duration(pause);
                                easing.paused = true;
                            }
                            easing.timer.reset();
                        }
                        EasingType::PingPong { pause, .. } => {
                            if let Some(pause) = pause {
                                easing.timer.set_duration(pause);
                                easing.paused = true;
                            }
                            easing.timer.reset();
                            easing.direction *= -1;
                        }
                    }
                }
            }
        } else if let Ok(ref mut easing_chain) = chain_query.get_mut(entity) {
            let next = easing_chain.0.pop();
            if let Some(mut next) = next {
                if next.start.is_none() {
                    next.start = Some(EaseValue(std::mem::take(&mut object)));
                }
                if let Some(ref start) = next.start {
                    *object = interpolation::lerp(&start.0, &next.end.0, &0.);
                } else {
                    *object = interpolation::lerp(&T::default(), &next.end.0, &0.);
                }

                commands.entity(entity).insert(next);
            } else {
                commands.entity(entity).remove::<EasingChainComponent<T>>();
            }
        }
    }
}
/// Ease system for custom component. Add this system to your application with your component as a type parameter.
Ease system for custom component. Add this system to your application with your component as a type parameter.
Ease system for custom component . Add this system to your application with your component as a type parameter .
pub fn custom_ease_system<T: CustomComponentEase + Component>(
    mut commands: Commands,
    time: Res<Time>,
    mut query: Query<(Entity, &mut T)>,
    mut easing_query: Query<&mut EasingComponent<T>>,
    mut chain_query: Query<&mut EasingChainComponent<T>>,
) where
    T: interpolation::Lerp<Scalar = f32> + Default,
{
    for (entity, mut object) in query.iter_mut() {
        if let Ok(ref mut easing) = easing_query.get_mut(entity) {
            if easing.state == EasingState::Play {
                easing.timer.tick(time.delta());
            }
            if easing.paused {
                if easing.timer.just_finished() {
                    match easing.easing_type {
                        EasingType::Once { duration } => {
                            easing.timer.set_duration(duration);
                        }
                        EasingType::Loop { duration, .. } => {
                            easing.timer.set_duration(duration);
                        }
                        EasingType::PingPong { duration, .. } => {
                            easing.timer.set_duration(duration);
                        }
                    }
                    easing.timer.reset();
                    easing.paused = false;
                }
            } else {
                if easing.timer.duration().as_secs_f32() != 0. {
                    let progress = if easing.direction.is_positive() {
                        easing.timer.percent()
                    } else {
                        easing.timer.percent_left()
                    };
                    let factor = progress.compute(easing.ease_function);
                    if let Some(ref start) = easing.start {
                        *object = interpolation::lerp(&start.0, &easing.end.0, &factor);
                    } else {
                        *object = interpolation::lerp(&T::default(), &easing.end.0, &factor);
                    }
                }
                if easing.timer.finished() {
                    match easing.easing_type {
                        EasingType::Once { .. } => {
                            commands.entity(entity).remove::<EasingComponent<T>>();
                        }
                        EasingType::Loop { pause, .. } => {
                            if let Some(pause) = pause {
                                easing.timer.set_duration(pause);
                                easing.paused = true;
                            }
                            easing.timer.reset();
                        }
                        EasingType::PingPong { pause, .. } => {
                            if let Some(pause) = pause {
                                easing.timer.set_duration(pause);
                                easing.paused = true;
                            }
                            easing.timer.reset();
                            easing.direction *= -1;
                        }
                    }
                }
            }
        } else if let Ok(ref mut easing_chain) = chain_query.get_mut(entity) {
            let next = easing_chain.0.pop();
            if let Some(mut next) = next {
                if next.start.is_none() {
                    next.start = Some(EaseValue(std::mem::take(&mut object)));
                }
                if let Some(ref start) = next.start {
                    *object = interpolation::lerp(&start.0, &next.end.0, &0.);
                } else {
                    *object = interpolation::lerp(&T::default(), &next.end.0, &0.);
                }
                commands.entity(entity).insert(next);
            } else {
                commands.entity(entity).remove::<EasingChainComponent<T>>();
            }
        }
    }
}
pub fn custom_ease_system < T : CustomComponentEase + Component > ( mut commands : Commands , time : Res < Time > , mut query : Query < ( Entity , & mut T ) > , mut easing_query : Query < & mut EasingComponent < T > > , mut chain_query : Query < & mut EasingChainComponent < T > > , ) where T : interpolation :: Lerp < Scalar = f32 > + Default , { for ( entity , mut object ) in query . iter_mut ( ) { if let Ok ( ref mut easing ) = easing_query . get_mut ( entity ) { if easing . state == EasingState :: Play { easing . timer . tick ( time . delta ( ) ) ; } if easing . paused { if easing . timer . just_finished ( ) { match easing . easing_type { EasingType :: Once { duration } => { easing . timer . set_duration ( duration ) ; } EasingType :: Loop { duration , .. } => { easing . timer . set_duration ( duration ) ; } EasingType :: PingPong { duration , .. } => { easing . timer . set_duration ( duration ) ; } } easing . timer . reset ( ) ; easing . paused = false ; } } else { if easing . timer . duration ( ) . as_secs_f32 ( ) != 0. { let progress = if easing . direction . is_positive ( ) { easing . timer . percent ( ) } else { easing . timer . percent_left ( ) } ; let factor = progress . compute ( easing . ease_function ) ; if let Some ( ref start ) = easing . start { * object = interpolation :: lerp ( & start . 0 , & easing . end . 0 , & factor ) ; } else { * object = interpolation :: lerp ( & T :: default ( ) , & easing . end . 0 , & factor ) ; } } if easing . timer . finished ( ) { match easing . easing_type { EasingType :: Once { .. } => { commands . entity ( entity ) . remove :: < EasingComponent < T > > ( ) ; } EasingType :: Loop { pause , .. } => { if let Some ( pause ) = pause { easing . timer . set_duration ( pause ) ; easing . paused = true ; } easing . timer . reset ( ) ; } EasingType :: PingPong { pause , .. } => { if let Some ( pause ) = pause { easing . timer . set_duration ( pause ) ; easing . paused = true ; } easing . timer . reset ( ) ; easing . direction *= - 1 ; } } } } } else if let Ok ( ref mut easing_chain ) = chain_query . get_mut ( entity ) { let next = easing_chain . 0 . pop ( ) ; if let Some ( mut next ) = next { if next . start . is_none ( ) { next . start = Some ( EaseValue ( std :: mem :: take ( & mut object ) ) ) ; } if let Some ( ref start ) = next . start { * object = interpolation :: lerp ( & start . 0 , & next . end . 0 , & 0. ) ; } else { * object = interpolation :: lerp ( & T :: default ( ) , & next . end . 0 , & 0. ) ; } commands . entity ( entity ) . insert ( next ) ; } else { commands . entity ( entity ) . remove :: < EasingChainComponent < T > > ( ) ; } } } }
Ease system for custom component.
Ease system for custom component .
commands
Commands
time
Res<Time>
query
Query<(Entity, &mut T)>
easing_query
Query<&mut EasingComponent<T>>
chain_query
Query<&mut EasingChainComponent<T>>