2018-11-04 15:15:34 -05:00
|
|
|
use crate::direction::Direction;
|
2020-06-21 15:17:24 -04:00
|
|
|
use std::fmt;
|
2016-04-03 15:53:40 -04:00
|
|
|
use std::ops;
|
|
|
|
|
2016-04-04 17:08:24 -04:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
2016-04-03 15:53:40 -04:00
|
|
|
pub struct Position {
|
|
|
|
pub x: i32,
|
|
|
|
pub y: i32,
|
|
|
|
pub z: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Position {
|
|
|
|
pub fn new(x: i32, y: i32, z: i32) -> Position {
|
2020-06-21 15:17:24 -04:00
|
|
|
Position { x, y, z }
|
2016-04-03 15:53:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn shift(self, dir: Direction) -> Position {
|
|
|
|
let (ox, oy, oz) = dir.get_offset();
|
|
|
|
self + (ox, oy, oz)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn shift_by(self, dir: Direction, by: i32) -> Position {
|
|
|
|
let (ox, oy, oz) = dir.get_offset();
|
|
|
|
self + (ox * by, oy * by, oz * by)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Add<Position> for Position {
|
|
|
|
type Output = Position;
|
|
|
|
|
|
|
|
fn add(self, o: Position) -> Position {
|
|
|
|
Position {
|
|
|
|
x: self.x + o.x,
|
|
|
|
y: self.y + o.y,
|
|
|
|
z: self.z + o.z,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Add<(i32, i32, i32)> for Position {
|
|
|
|
type Output = Position;
|
|
|
|
|
|
|
|
fn add(self, (x, y, z): (i32, i32, i32)) -> Position {
|
|
|
|
Position {
|
|
|
|
x: self.x + x,
|
|
|
|
y: self.y + y,
|
|
|
|
z: self.z + z,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Sub<Position> for Position {
|
|
|
|
type Output = Position;
|
|
|
|
|
|
|
|
fn sub(self, o: Position) -> Position {
|
|
|
|
Position {
|
|
|
|
x: self.x - o.x,
|
|
|
|
y: self.y - o.y,
|
|
|
|
z: self.z - o.z,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Sub<(i32, i32, i32)> for Position {
|
|
|
|
type Output = Position;
|
|
|
|
|
|
|
|
fn sub(self, (x, y, z): (i32, i32, i32)) -> Position {
|
|
|
|
Position {
|
|
|
|
x: self.x - x,
|
|
|
|
y: self.y - y,
|
|
|
|
z: self.z - z,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Position {
|
|
|
|
fn default() -> Position {
|
|
|
|
Position::new(0, 0, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Position {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "<{},{},{}>", self.x, self.y, self.z)
|
|
|
|
}
|
|
|
|
}
|