use std::{marker::PhantomData, time::Duration};
use bevy::{
prelude::{Local, Res, Resource},
time::{Time, Timer, TimerMode},
};
use crate::TComp;
#[allow(clippy::module_name_repetitions)]
#[derive(Resource, Default)]
pub struct TimestepLength<Comp>(pub Duration, pub(crate) PhantomData<Comp>);
impl<Comp> TimestepLength<Comp> {
pub fn set_duration(&mut self, duration: Duration) {
self.0 = duration;
}
#[must_use]
pub fn get_duration(&self) -> Duration {
self.0
}
}
#[allow(clippy::needless_pass_by_value)]
pub fn on_timer_changeable<Comp>(
length: Res<TimestepLength<Comp>>,
time: Res<Time>,
mut timer: Local<Timer>,
) -> bool
where
Comp: TComp,
{
if length.get_duration() != timer.duration() {
timer.set_mode(TimerMode::Repeating);
timer.set_duration(length.get_duration());
}
timer.tick(time.delta());
timer.just_finished()
}