1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! Chronological units

use std::ops::{Add, AddAssign, Mul, Sub, SubAssign};

use serde::{Deserialize, Serialize};

units! {
    /// Internal trait just because declarative macros are stupid.
    _TimeTrait(Clone + Copy);

    #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] u32:

    /// Synchronized time span.
    ///
    /// The underlying integer is in 1/100 seconds.
    Time("", " s");
}

impl Time {
    /// Converts the time span to number of seconds.
    ///
    /// This value is not precise and shall not be used for critical logic.
    pub fn as_secs(self) -> f64 { self.value() as f64 * 0.01 }

    /// An empty interval.
    pub fn zero() -> Self { Self(0) }

    /// Returns the integer quotient of the two time spans.
    pub fn int_div(self, other: Self) -> u32 { self.0 / other.0 }
}

/// A specific point of time,
/// represented as a duration since game epoch.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Instant(pub Time);

impl Instant {
    /// Returns the time since epoch
    pub fn since_epoch(self) -> Time { self.0 }
}

impl Add<Time> for Instant {
    type Output = Self;

    fn add(self, other: Time) -> Self { Self(self.0 + other) }
}

impl AddAssign<Time> for Instant {
    fn add_assign(&mut self, other: Time) { self.0 += other; }
}

impl Sub<Time> for Instant {
    type Output = Self;

    fn sub(self, other: Time) -> Self { Self(self.0 - other) }
}

impl Sub<Instant> for Instant {
    type Output = Time;

    fn sub(self, other: Self) -> Time { self.0 - other.0 }
}

impl SubAssign<Time> for Instant {
    fn sub_assign(&mut self, other: Time) { self.0 -= other; }
}

/// The rate of change.
///
/// The inner value is the amount of change over one second.
#[derive(Debug, Clone, Copy, Default, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Rate<T>(pub T);

impl<T: Mul<f64, Output = T>> std::ops::Mul<Time> for Rate<T> {
    type Output = T;

    fn mul(self, time: Time) -> T { self.0 * (time.value() as f64) }
}