hive/engine/grid/coordinate/
cube.rs

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