Skip to main content

euv_engine/timer/
impl.rs

1use super::*;
2
3/// Implements creation and countdown logic for `Timer`.
4impl Timer {
5    /// Creates a one-shot timer that fires once after `duration` seconds.
6    ///
7    /// # Arguments
8    ///
9    /// - `f64` - The countdown duration in seconds.
10    ///
11    /// # Returns
12    ///
13    /// - `Timer` - The new one-shot timer.
14    pub fn create(duration: f64) -> Timer {
15        Timer::new(duration.max(0.0), false)
16    }
17
18    /// Creates a repeating timer that fires every `duration` seconds.
19    ///
20    /// # Arguments
21    ///
22    /// - `f64` - The interval between firings in seconds.
23    ///
24    /// # Returns
25    ///
26    /// - `Timer` - The new repeating timer.
27    pub fn create_repeating(duration: f64) -> Timer {
28        Timer::new(duration.max(0.0), true)
29    }
30
31    /// Advances the timer by the given delta time.
32    ///
33    /// # Arguments
34    ///
35    /// - `f64` - The time elapsed since the last update, in seconds.
36    ///
37    /// # Returns
38    ///
39    /// - `u32` - The number of times the timer fired during this update.
40    pub fn update(&mut self, delta_time: f64) -> u32 {
41        if self.get_paused() || self.get_finished() || self.get_duration() <= 0.0 {
42            return 0;
43        }
44        *self.get_mut_elapsed() += delta_time.max(0.0);
45        let mut fire_count: u32 = 0;
46        while self.get_elapsed() >= self.get_duration() {
47            fire_count += 1;
48            if self.get_repeating() {
49                *self.get_mut_elapsed() -= self.get_duration();
50            } else {
51                self.set_elapsed(self.get_duration());
52                self.set_finished(true);
53                break;
54            }
55        }
56        fire_count
57    }
58
59    /// Resets the timer to its initial state so it can count down again.
60    pub fn reset(&mut self) {
61        self.set_elapsed(0.0);
62        self.set_finished(false);
63    }
64
65    /// Pauses the timer, preserving the accumulated elapsed time.
66    pub fn pause(&mut self) {
67        self.set_paused(true);
68    }
69
70    /// Resumes a paused timer.
71    pub fn resume(&mut self) {
72        self.set_paused(false);
73    }
74
75    /// Returns whether the timer is currently paused.
76    ///
77    /// # Returns
78    ///
79    /// - `bool` - True if paused.
80    pub fn is_paused(&self) -> bool {
81        self.get_paused()
82    }
83
84    /// Returns whether a one-shot timer has fired and stopped.
85    ///
86    /// # Returns
87    ///
88    /// - `bool` - True if finished.
89    pub fn is_finished(&self) -> bool {
90        self.get_finished()
91    }
92
93    /// Returns the countdown progress in the range 0.0 to 1.0.
94    ///
95    /// # Returns
96    ///
97    /// - `f64` - The progress ratio.
98    pub fn progress(&self) -> f64 {
99        if self.get_duration() <= 0.0 {
100            return 1.0;
101        }
102        (self.get_elapsed() / self.get_duration()).min(1.0)
103    }
104
105    /// Returns the time remaining until the next firing, in seconds.
106    ///
107    /// # Returns
108    ///
109    /// - `f64` - The remaining time.
110    pub fn remaining(&self) -> f64 {
111        (self.get_duration() - self.get_elapsed()).max(0.0)
112    }
113}
114
115/// Forwards `Timer::update` through the [`Updatable`] trait so timers can
116/// participate in the same generic update loop as entities, animators,
117/// scenes, and physics worlds.
118impl Updatable for Timer {
119    fn update(&mut self, delta_time: f64) {
120        let _: u32 = Timer::update(self, delta_time);
121    }
122}