azalea_core/
delta.rs

1pub use azalea_buf::AzBuf;
2
3use crate::position::Vec3;
4
5pub trait PositionDeltaTrait {
6    fn x(&self) -> f64;
7    fn y(&self) -> f64;
8    fn z(&self) -> f64;
9}
10
11/// Only works for up to 8 blocks
12#[derive(Clone, Debug, AzBuf, Default)]
13pub struct PositionDelta8 {
14    pub xa: i16,
15    pub ya: i16,
16    pub za: i16,
17}
18
19impl PositionDelta8 {
20    #[deprecated]
21    pub fn float(&self) -> (f64, f64, f64) {
22        (
23            (self.xa as f64) / 4096.0,
24            (self.ya as f64) / 4096.0,
25            (self.za as f64) / 4096.0,
26        )
27    }
28}
29
30impl PositionDeltaTrait for PositionDelta8 {
31    fn x(&self) -> f64 {
32        (self.xa as f64) / 4096.0
33    }
34    fn y(&self) -> f64 {
35        (self.ya as f64) / 4096.0
36    }
37    fn z(&self) -> f64 {
38        (self.za as f64) / 4096.0
39    }
40}
41
42impl Vec3 {
43    #[must_use]
44    pub fn with_delta(&self, delta: &impl PositionDeltaTrait) -> Vec3 {
45        Vec3 {
46            x: self.x + delta.x(),
47            y: self.y + delta.y(),
48            z: self.z + delta.z(),
49        }
50    }
51
52    pub fn normalize(&self) -> Vec3 {
53        let length = f64::sqrt(self.x * self.x + self.y * self.y + self.z * self.z);
54        if length < 1e-4 {
55            return Vec3::default();
56        }
57        Vec3 {
58            x: self.x / length,
59            y: self.y / length,
60            z: self.z / length,
61        }
62    }
63
64    pub fn multiply(&self, x: f64, y: f64, z: f64) -> Vec3 {
65        Vec3 {
66            x: self.x * x,
67            y: self.y * y,
68            z: self.z * z,
69        }
70    }
71    pub fn scale(&self, amount: f64) -> Vec3 {
72        self.multiply(amount, amount, amount)
73    }
74}