diff options
| author | Jon Santmyer <jon@jonsantmyer.com> | 2026-05-03 18:37:35 -0400 |
|---|---|---|
| committer | Jon Santmyer <jon@jonsantmyer.com> | 2026-05-03 18:37:35 -0400 |
| commit | dd5de0107163bc3ea3898c07089d00f82feeec5e (patch) | |
| tree | d8feef7d8a89c01ef183c6b032b29cd6d451aaec /src/timeman.rs | |
| parent | 759d5d27c7773c7fe8b165ce08b57204db990b74 (diff) | |
| download | systemic4x-dd5de0107163bc3ea3898c07089d00f82feeec5e.tar.gz systemic4x-dd5de0107163bc3ea3898c07089d00f82feeec5e.tar.bz2 systemic4x-dd5de0107163bc3ea3898c07089d00f82feeec5e.zip | |
add tick and subtick system for solar system orbit updates
Diffstat (limited to 'src/timeman.rs')
| -rw-r--r-- | src/timeman.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/timeman.rs b/src/timeman.rs index 32a6f45..64250ee 100644 --- a/src/timeman.rs +++ b/src/timeman.rs @@ -12,9 +12,12 @@ pub const HOUR: Second = MINUTE * 60; pub const DAY: Second = HOUR * 24; pub const YEAR: Second = DAY * 365; +pub const SYSTEM_TICK_INTERVAL: Second = 1 * DAY; + pub struct TimeMan { time: Second, + last_time: Second, pub auto_tick: Option<Second> } @@ -24,6 +27,7 @@ impl TimeMan -> Self { Self { time, + last_time: time, auto_tick: None } } @@ -42,11 +46,33 @@ impl TimeMan pub fn update( &mut self) + -> Vec<Second> { match self.auto_tick { Some(advance) => { self.time += advance; }, None => {} } + + let time_diff = self.time - self.last_time; + + let tick_diff = self.time / SYSTEM_TICK_INTERVAL - + self.last_time / SYSTEM_TICK_INTERVAL; + + if time_diff == 0 || tick_diff == 0 { + self.last_time = self.time; + return vec![]; + } + + let time_start = self.last_time - (self.last_time % SYSTEM_TICK_INTERVAL); + let time_end = self.time - (self.time % SYSTEM_TICK_INTERVAL); + + let tick_start = time_start / SYSTEM_TICK_INTERVAL; + let tick_end = time_end / SYSTEM_TICK_INTERVAL; + + self.last_time = self.time; + (tick_start..tick_end).map(|v| { + v * SYSTEM_TICK_INTERVAL + }).collect::<Vec<_>>() } pub fn format_duration(time: Second) -> String |
