Skip to main content

hive/engine/grid/coordinate/
cube.rs

1use std::fmt::{Display, Formatter, Result};
2
3use crate::engine::grid::coordinate::hex::Hex;
4
5#[derive(Debug, Hash, Eq, PartialEq, Clone, Copy, Ord, PartialOrd)]
6pub struct Cube {
7  pub x: i64,
8  pub z: i64,
9  pub y: i64,
10}
11
12impl Cube {
13  pub fn new(x: i64, z: i64, y: i64) -> Cube {
14    Cube { x, z, y }
15  }
16
17  pub fn xy(x: i64, y: i64) -> Cube {
18    Cube { x, y, z: -x - y }
19  }
20
21  pub fn xz(x: i64, z: i64) -> Cube {
22    Cube { x, y: -x - z, z }
23  }
24
25  pub fn yz(y: i64, z: i64) -> Cube {
26    Cube { x: -y - z, y, z }
27  }
28
29  pub fn neighbors(&self) -> Vec<Cube> {
30    let x = self.x;
31    let z = self.z;
32    let y = self.y;
33
34    let neighbors: Vec<Cube> = vec![
35      Cube {
36        x: x + 1,
37        z: z - 1,
38        y,
39      },
40      Cube {
41        x: x + 1,
42        z,
43        y: y - 1,
44      },
45      Cube {
46        x,
47        z: z + 1,
48        y: y - 1,
49      },
50      Cube {
51        x: x - 1,
52        z: z + 1,
53        y,
54      },
55      Cube {
56        x: x - 1,
57        z,
58        y: y + 1,
59      },
60      Cube {
61        x,
62        z: z - 1,
63        y: y + 1,
64      },
65    ];
66
67    neighbors
68  }
69}
70
71impl From<Hex> for Cube {
72  fn from(hex: Hex) -> Cube {
73    Cube {
74      x: hex.q,
75      z: hex.r,
76      y: -hex.q - hex.r,
77    }
78  }
79}
80
81impl Display for Cube {
82  fn fmt(&self, f: &mut Formatter) -> Result {
83    write!(f, "x: {}, z: {}, y: {}", self.x, self.z, self.y)
84  }
85}