Refactor common types.
This commit is contained in:
parent
f284b692cc
commit
48419b4674
14 changed files with 376 additions and 250 deletions
|
|
@ -1,170 +1,8 @@
|
|||
use core::panic;
|
||||
use std::io::{self, Write};
|
||||
|
||||
use rand::prelude::Distribution;
|
||||
use termcolor::{ColorSpec, StandardStream, WriteColor};
|
||||
|
||||
pub type PositionType = i32;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
|
||||
pub enum Direction {
|
||||
Up,
|
||||
Right,
|
||||
Down,
|
||||
Left,
|
||||
}
|
||||
|
||||
impl Direction {
|
||||
pub fn from_neighbors(pos: &Position, neighbor: &Position) -> Self {
|
||||
let x_diff = pos.x as i64 - neighbor.x as i64;
|
||||
let y_diff = pos.y as i64 - neighbor.y as i64;
|
||||
|
||||
match (x_diff, y_diff) {
|
||||
(1, 0) => Direction::Left,
|
||||
(0, 1) => Direction::Up,
|
||||
(-1, 0) => Direction::Right,
|
||||
(0, -1) => Direction::Down,
|
||||
_ => {
|
||||
panic!("Positions are not neighbors.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn vertical(&self) -> bool {
|
||||
match self {
|
||||
Direction::Up => true,
|
||||
Direction::Right => false,
|
||||
Direction::Down => true,
|
||||
Direction::Left => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn horizontal(&self) -> bool {
|
||||
!self.vertical()
|
||||
}
|
||||
|
||||
pub fn reverse(&self) -> Self {
|
||||
match self {
|
||||
Direction::Up => Direction::Down,
|
||||
Direction::Right => Direction::Left,
|
||||
Direction::Down => Direction::Up,
|
||||
Direction::Left => Direction::Right,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clockwise(&self) -> Self {
|
||||
match self {
|
||||
Direction::Up => Direction::Right,
|
||||
Direction::Right => Direction::Down,
|
||||
Direction::Down => Direction::Left,
|
||||
Direction::Left => Direction::Up,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn counter_clockwise(&self) -> Self {
|
||||
match self {
|
||||
Direction::Up => Direction::Left,
|
||||
Direction::Right => Direction::Up,
|
||||
Direction::Down => Direction::Right,
|
||||
Direction::Left => Direction::Down,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_index(&self) -> u8 {
|
||||
match self {
|
||||
Direction::Up => 0,
|
||||
Direction::Right => 1,
|
||||
Direction::Down => 2,
|
||||
Direction::Left => 3,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_index(i: u8) -> Self {
|
||||
match i {
|
||||
0 => Direction::Up,
|
||||
1 => Direction::Right,
|
||||
2 => Direction::Down,
|
||||
3 => Direction::Left,
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Distribution<Direction> for rand::distributions::Standard {
|
||||
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> Direction {
|
||||
let a = [
|
||||
Direction::Up,
|
||||
Direction::Right,
|
||||
Direction::Down,
|
||||
Direction::Left,
|
||||
];
|
||||
let r = rng.gen_range(0..4);
|
||||
a[r]
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct Position {
|
||||
pub x: PositionType,
|
||||
pub y: PositionType,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct Dimension {
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
}
|
||||
|
||||
impl Position {
|
||||
pub fn new(x: PositionType, y: PositionType) -> Self {
|
||||
Self { x, y }
|
||||
}
|
||||
|
||||
pub fn in_direction(&self, dir: &Direction, len: PositionType) -> Position {
|
||||
match dir {
|
||||
Direction::Up => Position::new(self.x, self.y - len),
|
||||
Direction::Right => Position::new(self.x + len, self.y),
|
||||
Direction::Down => Position::new(self.x, self.y + len),
|
||||
Direction::Left => Position::new(self.x - len, self.y),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn in_range(&self, min: &Position, max: &Position) -> Option<&Position> {
|
||||
if self.x < min.x {
|
||||
return None;
|
||||
}
|
||||
if self.x >= max.x {
|
||||
return None;
|
||||
}
|
||||
if self.y < min.y {
|
||||
return None;
|
||||
}
|
||||
if self.y >= max.y {
|
||||
return None;
|
||||
}
|
||||
Some(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Add for Position {
|
||||
type Output = Position;
|
||||
|
||||
fn add(mut self, rhs: Self) -> Self::Output {
|
||||
self.x += rhs.x;
|
||||
self.y += rhs.y;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Sub for Position {
|
||||
type Output = Position;
|
||||
|
||||
fn sub(mut self, rhs: Self) -> Self::Output {
|
||||
self.x -= rhs.x;
|
||||
self.y -= rhs.y;
|
||||
self
|
||||
}
|
||||
}
|
||||
use crate::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, Copy)]
|
||||
pub enum PathField {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue