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
pub type DirIndex = u8;
#[derive(Default, Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct Dir {
pub index: DirIndex,
}
impl Dir {
pub fn new(index: DirIndex) -> Dir {
Dir { index: index }
}
/// Returns `true` if `self` points toward an immediately
/// adjacent cell, or equivalently toward an edge of the
/// current cell.
///
/// Assumes this is in the context of a hexagonal cell --
/// i.e. not one of the 12 pentagons in the world.
/// If you need to ask an equivalent question when you might
/// be in a pentagonal cell, then first rebase your
/// `(Pos, Dir)` onto a root quad that `self` points into,
/// and then the relevant part of the current cell will
/// then be equivalent to a hexagon for this purpose.
pub fn points_at_hex_edge(&self) -> bool {
// On a hexagonal cell, any even direction index
// points to an edge.
self.index % 2 == 0
}
pub fn next_hex_edge_left(&self) -> Dir {
Dir::new((self.index + 2) % 12)
}
pub fn next_hex_edge_right(&self) -> Dir {
Dir::new((self.index + 12 - 2) % 12)
}
pub fn opposite(&self) -> Dir {
Dir::new((self.index + 6) % 12)
}
}
impl From<DirIndex> for Dir {
fn from(dir_index: DirIndex) -> Dir {
Dir::new(dir_index)
}
}