use std::fmt;
use std::time::Duration;
use nadir_core::Key;
use nadir_core::log::{debug, info};
use crate::TICK_HZ;
pub(crate) const PAUSE: Key = Key::F5;
pub(crate) const STEP: Key = Key::F6;
pub(crate) const SCALE: Key = Key::F7;
pub(crate) const SCALES: [f32; 5] = [0.25, 0.5, 1.0, 2.0, 4.0];
pub(crate) const TICK: Duration = Duration::from_nanos(1_000_000_000_u64.div_ceil(TICK_HZ as u64));
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Time {
pub(crate) scale: f32,
paused: bool,
steps: u32,
shift: [bool; 2],
}
impl Time {
pub(crate) fn new() -> Self {
Self {
scale: 1.0,
paused: false,
steps: 0,
shift: [false; 2],
}
}
pub(crate) fn is_real(&self) -> bool {
!self.paused && self.scale == 1.0
}
pub(crate) fn key(&mut self, key: Key, pressed: bool, repeat: bool) {
match key {
Key::LeftShift => self.shift[0] = pressed,
Key::RightShift => self.shift[1] = pressed,
PAUSE if pressed && !repeat => {
self.paused = !self.paused;
self.steps = 0;
if self.paused {
info!("the game's time is paused: F5 runs it again, F6 steps it a tick");
} else {
info!(
"the game's time runs again, at {} times real time",
self.scale
);
}
}
STEP if pressed => {
if !self.paused {
self.paused = true;
info!("the game's time is paused: F5 runs it again, F6 steps it a tick");
}
self.steps = self.steps.saturating_add(1);
debug!("the game's time steps a tick");
}
SCALE if pressed && !repeat => {
let down = self.shift[0] || self.shift[1];
self.scale = next_scale(self.scale, down);
info!("{}", self.scale_note());
}
_ => {}
}
}
pub(crate) fn scale_note(&self) -> ScaleNote {
ScaleNote {
paused: self.paused,
scale: self.scale,
}
}
pub(crate) fn focus_lost(&mut self) {
self.shift = [false; 2];
}
pub(crate) fn game_time(&mut self, elapsed: Duration) -> Duration {
if self.paused {
let steps = std::mem::take(&mut self.steps);
return TICK.saturating_mul(steps);
}
if self.scale == 1.0 {
return elapsed;
}
let scaled = elapsed.as_secs_f64() * f64::from(self.scale);
Duration::try_from_secs_f64(scaled).unwrap_or(Duration::MAX)
}
}
pub(crate) struct ScaleNote {
paused: bool,
scale: f32,
}
impl fmt::Display for ScaleNote {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.paused {
write!(
f,
"the game's time will run at {} times real time once F5 runs it again",
self.scale
)
} else {
write!(f, "the game's time runs at {} times real time", self.scale)
}
}
}
impl fmt::Display for Time {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match (self.paused, self.scale == 1.0) {
(true, true) => f.write_str("paused"),
(true, false) => write!(f, "paused, x{}", self.scale),
(false, false) => write!(f, "time x{}", self.scale),
(false, true) => Ok(()),
}
}
}
fn next_scale(scale: f32, down: bool) -> f32 {
let first = SCALES[0];
let last = SCALES[SCALES.len() - 1];
if down {
SCALES
.iter()
.rev()
.copied()
.find(|&candidate| candidate < scale)
.unwrap_or(last)
} else {
SCALES
.iter()
.copied()
.find(|&candidate| candidate > scale)
.unwrap_or(first)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn press(time: &mut Time, key: Key) {
time.key(key, true, false);
time.key(key, false, false);
}
#[test]
fn a_tick_s_length_runs_one_tick() {
assert_eq!(TICK, Duration::from_nanos(16_666_667));
let mut clock = nadir_core::time::FixedClock::new(TICK_HZ, Duration::from_millis(250));
for before in [0, 1, 5_000_000, 16_666_000] {
clock.begin_frame(Duration::from_nanos(before));
while clock.consume_tick().is_some() {}
let ticks = clock.ticks();
let alpha = clock.alpha();
clock.begin_frame(TICK);
while clock.consume_tick().is_some() {}
assert_eq!(clock.ticks(), ticks + 1, "{before}");
assert!((clock.alpha() - alpha).abs() < 1e-6, "{before}");
}
}
#[test]
fn f5_pauses_and_runs_again_and_f6_steps_a_tick_each() {
let mut time = Time::new();
let frame = Duration::from_millis(16);
assert!(time.is_real());
assert_eq!(time.game_time(frame), frame);
press(&mut time, PAUSE);
assert!(time.paused && !time.is_real());
assert_eq!(time.game_time(frame), Duration::ZERO);
press(&mut time, STEP);
time.key(STEP, true, true);
assert_eq!(time.game_time(frame), TICK * 2);
assert_eq!(time.game_time(frame), Duration::ZERO);
press(&mut time, STEP);
time.key(PAUSE, true, true);
assert!(time.paused);
press(&mut time, PAUSE);
assert!(!time.paused);
assert_eq!(time.game_time(frame), frame);
press(&mut time, STEP);
assert!(time.paused);
assert_eq!(time.game_time(frame), TICK);
}
#[test]
fn f7_steps_the_scale_up_and_shift_f7_down_wrapping_at_the_ends() {
let mut time = Time::new();
let mut seen = Vec::new();
for _ in 0..5 {
press(&mut time, SCALE);
seen.push(time.scale);
}
assert_eq!(seen, [2.0, 4.0, 0.25, 0.5, 1.0]);
time.key(Key::RightShift, true, false);
press(&mut time, SCALE);
assert_eq!(time.scale, 0.5);
time.key(SCALE, true, true);
assert_eq!(time.scale, 0.5);
time.focus_lost();
press(&mut time, SCALE);
assert_eq!(time.scale, 1.0, "Shift let go with the focus");
assert_eq!(next_scale(0.3, false), 0.5);
assert_eq!(next_scale(0.3, true), 0.25);
assert_eq!(next_scale(0.2, true), 4.0);
assert_eq!(next_scale(9.0, false), 0.25);
time.scale = 0.5;
assert_eq!(
time.game_time(Duration::from_millis(20)),
Duration::from_millis(10)
);
press(&mut time, STEP);
assert_eq!(time.game_time(Duration::from_millis(20)), TICK);
time.scale = 4.0;
press(&mut time, PAUSE);
assert_eq!(time.game_time(Duration::MAX), Duration::MAX);
}
#[test]
fn a_scale_s_note_says_when_the_time_will_run_at_it() {
let mut time = Time::new();
time.scale = 2.0;
assert_eq!(
time.scale_note().to_string(),
"the game's time runs at 2 times real time"
);
press(&mut time, PAUSE);
assert_eq!(
time.scale_note().to_string(),
"the game's time will run at 2 times real time once F5 runs it again"
);
}
#[test]
fn the_label_says_what_is_not_real() {
let mut time = Time::new();
assert_eq!(time.to_string(), "");
time.scale = 0.25;
assert_eq!(time.to_string(), "time x0.25");
press(&mut time, PAUSE);
assert_eq!(time.to_string(), "paused, x0.25");
time.scale = 1.0;
assert_eq!(time.to_string(), "paused");
}
}