diff options
Diffstat (limited to 'src/timeman.rs')
| -rw-r--r-- | src/timeman.rs | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/timeman.rs b/src/timeman.rs new file mode 100644 index 0000000..9d17999 --- /dev/null +++ b/src/timeman.rs @@ -0,0 +1,69 @@ +use std::cell::RefCell; +use std::time::Duration; +use std::{fmt::Display, string}; +use std::error::Error; + +use crate::GameState; +use crate::window::ui::GameWindowUiState; + +pub type Second = u64; + +pub struct TimeMan +{ + time: Second, + auto_tick: Option<Second> +} + +impl TimeMan +{ + pub fn new(time: Second) + -> Self { + Self { + time, + auto_tick: None + } + } + + pub fn seconds(&self) + -> Second { + self.time + } + + pub fn advance( + &mut self, + by: Second) + { + self.time += by; + } + + pub fn update( + &mut self) + { + match self.auto_tick { + Some(advance) => { self.time += advance; }, + None => {} + } + } + + pub fn format_duration(time: Second) -> String + { + let seconds = time % 60; + let minutes = (time / 60) % 60; + let hours = (time / (60 * 60)) % 24; + let days = (time / (60 * 60 * 24)) % 365; + let years = time / (60 * 60 * 24 * 365); + + if time == 0 { + return "0s".to_string(); + } + + format!("{}{}{}{}{}", + if seconds > 0 { format!("{}s", seconds) } else { format!("") }, + if minutes > 0 { format!("{}m", minutes) } else { format!("") }, + if hours > 0 { format!("{}h", hours) } else { format!("") }, + if days > 0 { format!("{}d", days) } else { format!("") }, + if years > 0 { format!("{}y", years) } else { format!("") }) + } + + +} //impl TimeMan |
