Skip to main content

azalea_core/
direction.rs

1use azalea_buf::AzBuf;
2
3use crate::position::{BlockPos, Vec3, Vec3i};
4
5#[derive(Clone, Copy, Debug, AzBuf, Default, Eq, PartialEq)]
6pub enum Direction {
7    #[default]
8    Down = 0,
9    Up,
10    North,
11    South,
12    West,
13    East,
14}
15
16impl Direction {
17    pub const HORIZONTAL: [Direction; 4] = [
18        Direction::North,
19        Direction::East,
20        Direction::South,
21        Direction::West,
22    ];
23    pub const VERTICAL: [Direction; 2] = [Direction::Down, Direction::Up];
24
25    pub fn nearest(vec: Vec3) -> Direction {
26        let mut best_direction = Direction::North;
27        let mut best_direction_amount = 0.0;
28
29        for dir in [
30            Direction::Down,
31            Direction::Up,
32            Direction::North,
33            Direction::South,
34            Direction::West,
35            Direction::East,
36        ]
37        .iter()
38        {
39            let amount = dir.normal_vec3().dot(vec);
40            if amount > best_direction_amount {
41                best_direction = *dir;
42                best_direction_amount = amount;
43            }
44        }
45
46        best_direction
47    }
48
49    #[inline]
50    pub fn normal(self) -> BlockPos {
51        match self {
52            Direction::Down => BlockPos::new(0, -1, 0),
53            Direction::Up => BlockPos::new(0, 1, 0),
54            Direction::North => BlockPos::new(0, 0, -1),
55            Direction::South => BlockPos::new(0, 0, 1),
56            Direction::West => BlockPos::new(-1, 0, 0),
57            Direction::East => BlockPos::new(1, 0, 0),
58        }
59    }
60
61    #[inline]
62    pub fn normal_vec3(self) -> Vec3 {
63        self.normal().to_vec3_floored()
64    }
65
66    pub fn opposite(self) -> Direction {
67        match self {
68            Direction::Down => Direction::Up,
69            Direction::Up => Direction::Down,
70            Direction::North => Direction::South,
71            Direction::South => Direction::North,
72            Direction::West => Direction::East,
73            Direction::East => Direction::West,
74        }
75    }
76
77    pub fn x(self) -> i32 {
78        self.normal().x
79    }
80    pub fn y(self) -> i32 {
81        self.normal().y
82    }
83    pub fn z(self) -> i32 {
84        self.normal().z
85    }
86}
87
88/// The four cardinal directions.
89///
90/// Note that azalea_block has a similar enum named `FacingCardinal` that is
91/// used for block states.
92#[derive(AzBuf, Clone, Copy, Debug, Eq, Hash, PartialEq)]
93pub enum CardinalDirection {
94    North,
95    South,
96    West,
97    East,
98}
99
100/// A 3D axis like x, y, z.
101#[derive(Clone, Copy, Debug)]
102pub enum Axis {
103    X = 0,
104    Y = 1,
105    Z = 2,
106}
107
108#[derive(Clone, Copy, Debug)]
109pub enum AxisCycle {
110    None = 0,
111    Forward = 1,
112    Backward = 2,
113}
114
115impl CardinalDirection {
116    #[inline]
117    pub fn x(self) -> i16 {
118        match self {
119            CardinalDirection::East => 1,
120            CardinalDirection::West => -1,
121            _ => 0,
122        }
123    }
124    #[inline]
125    pub fn z(self) -> i16 {
126        match self {
127            CardinalDirection::South => 1,
128            CardinalDirection::North => -1,
129            _ => 0,
130        }
131    }
132
133    pub fn iter() -> impl Iterator<Item = CardinalDirection> {
134        [
135            CardinalDirection::North,
136            CardinalDirection::South,
137            CardinalDirection::West,
138            CardinalDirection::East,
139        ]
140        .iter()
141        .copied()
142    }
143
144    #[inline]
145    pub fn right(self) -> CardinalDirection {
146        match self {
147            CardinalDirection::North => CardinalDirection::East,
148            CardinalDirection::South => CardinalDirection::West,
149            CardinalDirection::West => CardinalDirection::North,
150            CardinalDirection::East => CardinalDirection::South,
151        }
152    }
153    #[inline]
154    pub fn left(self) -> CardinalDirection {
155        match self {
156            CardinalDirection::North => CardinalDirection::West,
157            CardinalDirection::South => CardinalDirection::East,
158            CardinalDirection::West => CardinalDirection::South,
159            CardinalDirection::East => CardinalDirection::North,
160        }
161    }
162}
163impl From<CardinalDirection> for Direction {
164    fn from(value: CardinalDirection) -> Self {
165        match value {
166            CardinalDirection::North => Direction::North,
167            CardinalDirection::South => Direction::South,
168            CardinalDirection::West => Direction::West,
169            CardinalDirection::East => Direction::East,
170        }
171    }
172}
173
174impl Axis {
175    /// Pick x, y, or z from the arguments depending on the axis.
176    #[inline]
177    pub fn choose<T>(&self, x: T, y: T, z: T) -> T {
178        match self {
179            Axis::X => x,
180            Axis::Y => y,
181            Axis::Z => z,
182        }
183    }
184
185    pub fn from_ordinal(ordinal: u32) -> Self {
186        match ordinal {
187            0 => Axis::X,
188            1 => Axis::Y,
189            2 => Axis::Z,
190            _ => panic!("Invalid ordinal {ordinal}"),
191        }
192    }
193}
194
195impl AxisCycle {
196    pub fn from_ordinal(ordinal: u32) -> Self {
197        match ordinal {
198            0 => Self::None,
199            1 => Self::Forward,
200            2 => Self::Backward,
201            _ => panic!("invalid ordinal"),
202        }
203    }
204    pub fn between(axis0: Axis, axis1: Axis) -> Self {
205        Self::from_ordinal(i32::rem_euclid(axis1 as i32 - axis0 as i32, 3) as u32)
206    }
207    pub fn inverse(self) -> Self {
208        match self {
209            Self::None => Self::None,
210            Self::Forward => Self::Backward,
211            Self::Backward => Self::Forward,
212        }
213    }
214    pub fn cycle(self, axis: Axis) -> Axis {
215        match self {
216            Self::None => axis,
217            Self::Forward => Axis::from_ordinal(i32::rem_euclid(axis as i32 + 1, 3) as u32),
218            Self::Backward => Axis::from_ordinal(i32::rem_euclid(axis as i32 - 1, 3) as u32),
219        }
220    }
221    pub fn cycle_xyz(self, pos: Vec3i, axis: Axis) -> i32 {
222        match self {
223            Self::None => axis.choose(pos.x, pos.y, pos.z),
224            Self::Forward => axis.choose(pos.z, pos.x, pos.y),
225            Self::Backward => axis.choose(pos.y, pos.z, pos.x),
226        }
227    }
228}