-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16ae4d9
commit 32dc239
Showing
5 changed files
with
90 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use nd_vec::{vector, Vec2}; | ||
use num_traits::Num; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
pub enum Direction { | ||
Up, | ||
Down, | ||
Left, | ||
Right, | ||
} | ||
|
||
impl Direction { | ||
pub const ALL: [Direction; 4] = [ | ||
Direction::Up, | ||
Direction::Down, | ||
Direction::Left, | ||
Direction::Right, | ||
]; | ||
|
||
#[rustfmt::skip] | ||
pub fn try_advance<T: Num + Copy + PartialOrd>(&self, pos: Vec2<T>) -> Option<Vec2<T>> { | ||
Some(match self { | ||
Self::Up if pos.y() > T::zero() => vector!(pos.x(), pos.y() - T::one()), | ||
Self::Down => vector!(pos.x(), pos.y() + T::one()), | ||
Self::Left if pos.x() > T::zero() => vector!(pos.x() - T::one(), pos.y()), | ||
Self::Right => vector!(pos.x() + T::one(), pos.y()), | ||
_ => return None, | ||
}) | ||
} | ||
|
||
#[rustfmt::skip] | ||
pub fn advance<T: Num + Copy>(&self, pos: Vec2<T>) -> Vec2<T> { | ||
match self { | ||
Self::Up => vector!(pos.x(), pos.y() - T::one()), | ||
Self::Down => vector!(pos.x(), pos.y() + T::one()), | ||
Self::Left => vector!(pos.x() - T::one(), pos.y()), | ||
Self::Right => vector!(pos.x() + T::one(), pos.y()), | ||
} | ||
} | ||
|
||
pub fn opposite(&self) -> Self { | ||
match self { | ||
Self::Up => Self::Down, | ||
Self::Down => Self::Up, | ||
Self::Left => Self::Right, | ||
Self::Right => Self::Left, | ||
} | ||
} | ||
|
||
pub fn turn_left(&self) -> Self { | ||
match self { | ||
Self::Up => Self::Left, | ||
Self::Down => Self::Right, | ||
Self::Left => Self::Down, | ||
Self::Right => Self::Up, | ||
} | ||
} | ||
|
||
pub fn turn_right(&self) -> Self { | ||
match self { | ||
Self::Up => Self::Right, | ||
Self::Down => Self::Left, | ||
Self::Left => Self::Up, | ||
Self::Right => Self::Down, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#[macro_use] | ||
pub mod regex; | ||
pub mod direction; | ||
pub mod math; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters