use crate::utils::time::now_ms;
#[derive(Debug, Clone)]
pub struct Timer {
previous_time: f64,
current_time: f64,
start_time: f64,
delta: f64,
elapsed: f64,
timescale: f64,
}
impl Default for Timer {
fn default() -> Self {
Self::new()
}
}
impl Timer {
pub fn new() -> Self {
Self {
previous_time: 0.0,
current_time: 0.0,
start_time: now_ms(),
delta: 0.0,
elapsed: 0.0,
timescale: 1.0,
}
}
pub fn get_delta(&self) -> f64 {
self.delta / 1000.0
}
pub fn get_elapsed(&self) -> f64 {
self.elapsed / 1000.0
}
pub fn get_timescale(&self) -> f64 {
self.timescale
}
pub fn set_timescale(&mut self, timescale: f64) -> &mut Self {
self.timescale = timescale;
self
}
pub fn reset(&mut self) -> &mut Self {
self.current_time = now_ms() - self.start_time;
self
}
pub fn update(&mut self) -> &mut Self {
self.update_with(None)
}
pub fn update_with(&mut self, timestamp: Option<f64>) -> &mut Self {
self.previous_time = self.current_time;
self.current_time = timestamp.unwrap_or_else(now_ms) - self.start_time;
self.delta = (self.current_time - self.previous_time) * self.timescale;
self.elapsed += self.delta;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::testing::pin_time;
#[test]
fn a_pinned_clock_gives_a_zero_delta_forever() {
pin_time(Some(0.0));
let mut timer = Timer::new();
for _ in 0..4 {
timer.update();
assert_eq!(timer.get_delta(), 0.0);
assert_eq!(timer.get_elapsed(), 0.0);
}
pin_time(None);
}
#[test]
fn deltas_accumulate_into_elapsed() {
pin_time(Some(1000.0));
let mut timer = Timer::new();
pin_time(Some(1016.0));
timer.update();
assert!((timer.get_delta() - 0.016).abs() < 1e-12);
assert!((timer.get_elapsed() - 0.016).abs() < 1e-12);
pin_time(Some(1048.0));
timer.update();
assert!((timer.get_delta() - 0.032).abs() < 1e-12);
assert!((timer.get_elapsed() - 0.048).abs() < 1e-12);
pin_time(None);
}
#[test]
fn timescale_scales_the_delta() {
pin_time(Some(0.0));
let mut timer = Timer::new();
timer.set_timescale(0.5);
pin_time(Some(100.0));
timer.update();
assert!((timer.get_delta() - 0.05).abs() < 1e-12);
pin_time(None);
}
#[test]
fn reset_drops_the_gap_since_the_last_update() {
pin_time(Some(0.0));
let mut timer = Timer::new();
pin_time(Some(10_000.0));
timer.reset();
pin_time(Some(10_016.0));
timer.update();
assert!((timer.get_delta() - 0.016).abs() < 1e-12);
pin_time(None);
}
#[test]
fn an_explicit_timestamp_replaces_the_clock() {
pin_time(Some(0.0));
let mut timer = Timer::new();
timer.update_with(Some(500.0));
assert!((timer.get_delta() - 0.5).abs() < 1e-12);
pin_time(None);
}
}