use crate::engine::animation::base::{Animation, AudioEvent};
use std::time::Duration;
pub struct Any {
pub(crate) animations: Vec<Box<dyn Animation>>,
}
impl Any {
pub fn new(animations: Vec<Box<dyn Animation>>) -> Self {
Self { animations }
}
}
impl Animation for Any {
fn update(&mut self, dt: Duration) -> (bool, Duration) {
let mut any_finished = false;
let mut max_leftover = Duration::ZERO;
for anim in &mut self.animations {
let (finished, leftover) = anim.update(dt);
if finished {
any_finished = true;
max_leftover = max_leftover.max(leftover);
}
}
(
any_finished,
if any_finished {
max_leftover
} else {
Duration::ZERO
},
)
}
fn duration(&self) -> Duration {
self.animations
.iter()
.map(|a| a.duration())
.min()
.unwrap_or(Duration::ZERO)
}
fn set_easing(&mut self, easing: fn(f32) -> f32) {
for anim in &mut self.animations {
anim.set_easing(easing);
}
}
fn collect_audio_events(&mut self, current_time: Duration, events: &mut Vec<AudioEvent>) {
for anim in &mut self.animations {
anim.collect_audio_events(current_time, events);
}
}
}
pub fn any(animations: Vec<Box<dyn Animation>>) -> Box<dyn Animation> {
Box::new(Any::new(animations))
}