paiagram/units/
speed.rs

1use super::distance::Distance;
2use super::time::Duration;
3use bevy::prelude::Reflect;
4use derive_more::{Add, AddAssign, Sub, SubAssign};
5use std::ops;
6
7#[derive(Reflect, Debug, Clone, Copy, Add, AddAssign, Sub, SubAssign)]
8pub struct Velocity(pub f32);
9
10impl ops::Mul<f32> for Velocity {
11    type Output = Velocity;
12    fn mul(self, rhs: f32) -> Self::Output {
13        Self(self.0 * rhs)
14    }
15}
16
17impl ops::MulAssign<f32> for Velocity {
18    fn mul_assign(&mut self, rhs: f32) {
19        self.0 *= rhs
20    }
21}
22
23impl ops::Div<f32> for Velocity {
24    type Output = Self;
25    fn div(self, rhs: f32) -> Self::Output {
26        Self(self.0 / rhs)
27    }
28}
29
30impl ops::DivAssign<f32> for Velocity {
31    fn div_assign(&mut self, rhs: f32) {
32        self.0 /= rhs
33    }
34}
35
36impl ops::Mul<Duration> for Velocity {
37    type Output = Distance;
38    fn mul(self, rhs: Duration) -> Self::Output {
39        Distance((self.0 * rhs.0 as f32).round() as i32)
40    }
41}
42
43impl ops::Div<Velocity> for Distance {
44    type Output = Duration;
45    fn div(self, rhs: Velocity) -> Self::Output {
46        if rhs.0 == 0.0 {
47            return Duration(0);
48        }
49        Duration((self.0 as f32 / rhs.0).round() as i32)
50    }
51}
52
53impl ops::Div<Duration> for Distance {
54    type Output = Velocity;
55    fn div(self, rhs: Duration) -> Self::Output {
56        if rhs.0 == 0 {
57            return Velocity(0.0);
58        }
59        Velocity(self.0 as f32 / rhs.0 as f32)
60    }
61}