summaryrefslogtreecommitdiff
path: root/2019-worms/src/geometry/direction.rs
blob: e37f750a9049a0748e223d3719a65141f0dadfdc (plain)
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
use crate::geometry::vec::Vec2d;
use std::fmt;

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Direction {
    North,
    NorthEast,
    East,
    SouthEast,
    South,
    SouthWest,
    West,
    NorthWest,
}

impl fmt::Display for Direction {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use Direction::*;
        let s = match self {
            North => "N",
            NorthEast => "NE",
            East => "E",
            SouthEast => "SE",
            South => "S",
            SouthWest => "SW",
            West => "W",
            NorthWest => "NW",
        };
        f.write_str(s)
    }
}

impl Direction {
    pub fn is_diagonal(&self) -> bool {
        use Direction::*;

        match self {
            NorthEast | SouthEast | SouthWest | NorthWest => true,
            _ => false,
        }
    }

    pub fn as_vec(&self) -> Vec2d {
        use Direction::*;
        match self {
            North => Vec2d::new(0, -1),
            NorthEast => Vec2d::new(1, -1),
            East => Vec2d::new(1, 0),
            SouthEast => Vec2d::new(1, 1),
            South => Vec2d::new(0, 1),
            SouthWest => Vec2d::new(-1, 1),
            West => Vec2d::new(-1, 0),
            NorthWest => Vec2d::new(-1, -1),
        }
    }

    pub const ALL: [Direction; 8] = [
        Direction::North,
        Direction::NorthEast,
        Direction::East,
        Direction::SouthEast,
        Direction::South,
        Direction::SouthWest,
        Direction::West,
        Direction::NorthWest,
    ];
}