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