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
use std::ops::{Add, AddAssign};

use super::direction::Direction;

/// Axial hex coordinates.
///
/// See [reference](https://www.redblobgames.com/grids/hexagons/#coordinates).
///
/// Constraint: `q + r + s == 0`
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
pub struct Coordinate {
    pub q: i32,
    pub r: i32,
}

impl Coordinate {
    pub fn neighbors(self) -> impl 'static + Iterator<Item = Coordinate> {
        Direction::iter().map(move |direction| self + direction)
    }
}

impl AddAssign<Direction> for Coordinate {
    fn add_assign(&mut self, rhs: Direction) {
        match rhs {
            Direction::East => {
                self.q += 1;
            }
            Direction::Southeast => {
                self.r += 1;
            }
            Direction::Southwest => {
                self.q -= 1;
                self.r += 1;
            }
            Direction::West => {
                self.q -= 1;
            }
            Direction::Northwest => {
                self.r -= 1;
            }
            Direction::Northeast => {
                self.q += 1;
                self.r -= 1;
            }
        }
    }
}

impl Add<Direction> for Coordinate {
    type Output = Coordinate;

    fn add(mut self, rhs: Direction) -> Self::Output {
        self += rhs;
        self
    }
}