use std::time::Duration;
use bevy::reflect::Reflect;
#[derive(Clone, Debug, Reflect)]
pub struct Repeat {
duration_before_first_repeat: Duration,
duration_between_repeat: Duration,
time_counter: Duration,
}
impl Repeat {
pub(crate) fn new(
duration_before_first_repeat: Duration,
duration_between_repeat: Duration,
) -> Self {
Self {
duration_before_first_repeat,
duration_between_repeat,
time_counter: Duration::ZERO,
}
}
pub(crate) fn reset(&mut self) {
self.time_counter = Duration::ZERO;
}
pub(crate) fn is_triggered(&mut self, signal: bool, dt: &Duration) -> bool {
if !signal {
self.time_counter = Duration::ZERO;
return false;
}
let previous_time = self.time_counter.clone();
self.time_counter += *dt;
if previous_time < self.duration_before_first_repeat
&& self.time_counter >= self.duration_before_first_repeat
{
return true;
}
let mut next_repeat = self.duration_before_first_repeat + self.duration_between_repeat;
while previous_time > next_repeat {
next_repeat += self.duration_between_repeat;
}
if previous_time < next_repeat && self.time_counter >= next_repeat {
return true;
}
return false;
}
}