1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
use azalea_buf::McBuf;
#[derive(Clone, Copy, Debug, McBuf, Default)]
pub enum Direction {
#[default]
Down = 0,
Up,
North,
South,
West,
East,
}
// TODO: make azalea_block use this instead of FacingCardinal
#[derive(Clone, Copy, Debug, McBuf)]
pub enum CardinalDirection {
North,
South,
West,
East,
}
#[derive(Clone, Copy, Debug)]
pub enum Axis {
X = 0,
Y = 1,
Z = 2,
}
#[derive(Clone, Copy, Debug)]
pub enum AxisCycle {
None = 0,
Forward = 1,
Backward = 2,
}
impl CardinalDirection {
#[inline]
pub fn x(self) -> i32 {
match self {
CardinalDirection::East => 1,
CardinalDirection::West => -1,
_ => 0,
}
}
#[inline]
pub fn z(self) -> i32 {
match self {
CardinalDirection::South => 1,
CardinalDirection::North => -1,
_ => 0,
}
}
pub fn iter() -> impl Iterator<Item = CardinalDirection> {
[
CardinalDirection::North,
CardinalDirection::South,
CardinalDirection::West,
CardinalDirection::East,
]
.iter()
.copied()
}
#[inline]
pub fn right(self) -> CardinalDirection {
match self {
CardinalDirection::North => CardinalDirection::East,
CardinalDirection::South => CardinalDirection::West,
CardinalDirection::West => CardinalDirection::North,
CardinalDirection::East => CardinalDirection::South,
}
}
#[inline]
pub fn left(self) -> CardinalDirection {
match self {
CardinalDirection::North => CardinalDirection::West,
CardinalDirection::South => CardinalDirection::East,
CardinalDirection::West => CardinalDirection::South,
CardinalDirection::East => CardinalDirection::North,
}
}
}
impl Axis {
/// Pick x, y, or z from the arguments depending on the axis.
#[inline]
pub fn choose<T>(&self, x: T, y: T, z: T) -> T {
match self {
Axis::X => x,
Axis::Y => y,
Axis::Z => z,
}
}
pub fn from_ordinal(ordinal: u32) -> Self {
match ordinal {
0 => Axis::X,
1 => Axis::Y,
2 => Axis::Z,
_ => panic!("Invalid ordinal {ordinal}"),
}
}
}
impl AxisCycle {
pub fn from_ordinal(ordinal: u32) -> Self {
match ordinal {
0 => Self::None,
1 => Self::Forward,
2 => Self::Backward,
_ => panic!("invalid ordinal"),
}
}
pub fn between(axis0: Axis, axis1: Axis) -> Self {
Self::from_ordinal(i32::rem_euclid(axis1 as i32 - axis0 as i32, 3) as u32)
}
pub fn inverse(self) -> Self {
match self {
Self::None => Self::None,
Self::Forward => Self::Backward,
Self::Backward => Self::Forward,
}
}
pub fn cycle(self, axis: Axis) -> Axis {
match self {
Self::None => axis,
Self::Forward => Axis::from_ordinal(i32::rem_euclid(axis as i32 + 1, 3) as u32),
Self::Backward => Axis::from_ordinal(i32::rem_euclid(axis as i32 - 1, 3) as u32),
}
}
pub fn cycle_xyz(self, x: i32, y: i32, z: i32, axis: Axis) -> i32 {
match self {
Self::None => axis.choose(x, y, z),
Self::Forward => axis.choose(z, x, y),
Self::Backward => axis.choose(y, z, x),
}
}
}
|