furmint-runtime 0.1.0

Main package of furmint game engine providing higher level API
Documentation
use std::ops::AddAssign;

/// Nanoseconds in a second
pub const NANOS_PER_SECOND: f64 = 1_000_000_000.0;

/// A struct representing game ticks
#[derive(Debug)]
pub struct Time {
    /// Amount of ticks ran since the game start
    pub ticks: u64,
    /// The tick rate in nanoseconds per tick
    pub tick_period: u64,
    /// Total elapsed time in seconds
    pub total_elapsed_time: f64,
    /// The duration of the last completed tick in seconds
    pub delta_seconds: f64,
}

impl AddAssign<u64> for Time {
    fn add_assign(&mut self, rhs: u64) {
        self.ticks += rhs;
    }
}

impl Time {
    /// Get the current ticks-per-second rate
    ///
    /// This function divides total ticks amount by total elapsed time, or if the elapsed time is
    /// zero it returns 0.0 to avoid division by zero
    pub fn tps(&self) -> f64 {
        if self.total_elapsed_time > 0.0 {
            self.ticks as f64 / self.total_elapsed_time
        } else {
            0.0
        }
    }

    /// Get target TPS
    pub const fn target_tick_rate(&self) -> f64 {
        NANOS_PER_SECOND / self.total_elapsed_time
    }
}