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