use std::time::Duration;
use lotus_proc_macros::Resource;
#[derive(Clone, Default, Debug, PartialEq)]
pub enum TimerType {
#[default]
Once,
Repeat
}
#[derive(Clone, Debug, Resource)]
pub struct Timer {
pub timer_type: TimerType,
pub duration: Duration,
pub elapsed: Duration,
pub is_finished: bool
}
impl Timer {
pub fn new(timer_type: TimerType, duration: Duration) -> Self {
return Self {
timer_type,
duration,
elapsed: Duration::ZERO,
is_finished: false
}
}
pub fn tick(&mut self, delta: f32) {
self.elapsed += Duration::from_secs_f32(delta);
self.is_finished = false;
if self.elapsed >= self.duration {
match self.timer_type {
TimerType::Once => {
self.is_finished = true;
self.elapsed = self.duration;
},
TimerType::Repeat => {
self.is_finished = true;
self.elapsed = Duration::ZERO;
}
}
}
}
pub fn reset(&mut self) {
self.is_finished = false;
self.elapsed = Duration::ZERO;
}
pub fn is_finished(&self) -> bool {
return self.is_finished;
}
pub fn duration_as_secs_f32(&self) -> f32 {
return self.duration.as_secs_f32();
}
pub fn elapsed_as_secs_f32(&self) -> f32 {
return self.elapsed.as_secs_f32();
}
}