From 88430f31c73f469086b68f2b77d1e1ba5f9178e7 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Mon, 22 Apr 2019 21:50:00 +0200 Subject: More minimal game state I'd prefer to start with just the state that I need, and progressively readd the bits that I've skipped as I find I need them, or as the competition evolves. --- src/geometry/direction.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/geometry/direction.rs (limited to 'src/geometry') diff --git a/src/geometry/direction.rs b/src/geometry/direction.rs new file mode 100644 index 0000000..84fe785 --- /dev/null +++ b/src/geometry/direction.rs @@ -0,0 +1,56 @@ +use std::fmt; +use crate::geometry::vec::Vec2d; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +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), + } + } +} -- cgit v1.2.3