File size: 1,592 Bytes
48ca417 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
use bevy::prelude::Resource;
use bevy::utils::Instant;
use std::f32::consts::PI;
use std::fmt::Debug;
use std::sync::atomic::{ AtomicBool, Ordering };
use std::sync::Arc;
#[derive(Debug, Clone, Resource)]
pub(crate) struct EasedChange<T: Debug + Clone> {
pub(crate) from: T,
pub(crate) to: T,
pub(crate) duration: f32,
pub(crate) done: Arc<AtomicBool>,
pub(crate) start_time: Instant,
}
impl<T: Debug + Clone> EasedChange<T> {
pub(crate) fn new(from: T, to: T, duration: f32) -> Self {
Self {
from,
to,
duration,
done: Arc::new(AtomicBool::new(false)),
start_time: Instant::now(),
}
}
pub(crate) fn input(&self) -> f32 {
(self.start_time.elapsed().as_secs_f32() / self.duration).min(1.0)
}
pub(crate) fn is_done(&self) -> bool {
self.input() >= 0.99
}
pub(crate) fn set_done(&mut self) {
self.done.store(true, Ordering::Relaxed);
}
/// Source: <https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Animated/Easing.js#L165>
pub(crate) fn elastic(&self, bounciness: f32) -> f32 {
let p = bounciness * PI;
let t = self.input();
1.0 - ((t * PI) / 2.0).cos().powi(3) * (t * p).cos()
}
pub(crate) fn smooth_start(&self) -> f32 {
let x = self.input();
x.powf(1.5)
}
pub(crate) fn smooth_end(&self) -> f32 {
let x = self.input();
let n = 1.0 - x;
1.0 - n * n
}
}
|