summaryrefslogtreecommitdiff
path: root/src/geometry
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2019-04-22 21:50:00 +0200
committerJustin Worthe <justin@worthe-it.co.za>2019-04-22 21:50:00 +0200
commit88430f31c73f469086b68f2b77d1e1ba5f9178e7 (patch)
tree292a0aceba92e4d0c38679ed919b9b463c82152b /src/geometry
parent3e54b01003aa9d27de8f4ca13c9240fe785ec0e1 (diff)
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.
Diffstat (limited to 'src/geometry')
-rw-r--r--src/geometry/direction.rs56
1 files changed, 56 insertions, 0 deletions
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<i8> {
+ 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),
+ }
+ }
+}