diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2022-04-19 21:29:18 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2022-04-19 21:29:18 +0200 |
commit | 4d1edc899a86791ec57c0d3b2b3d7427f138463f (patch) | |
tree | ed1c7031c54d8f3a3897081c4a1040ace9d8f4ae /2019-worms/src/geometry/direction.rs | |
parent | 01a849c6b26afd076989f645c6153d8ce1422281 (diff) | |
parent | 3f5492b2bb67326be43cd7c5ba02ccf0ba1ae0e3 (diff) |
Merge branch 'worms-main'
Diffstat (limited to '2019-worms/src/geometry/direction.rs')
-rw-r--r-- | 2019-worms/src/geometry/direction.rs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/2019-worms/src/geometry/direction.rs b/2019-worms/src/geometry/direction.rs new file mode 100644 index 0000000..e37f750 --- /dev/null +++ b/2019-worms/src/geometry/direction.rs @@ -0,0 +1,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, + ]; +} |