1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::{fmt, ops::{Add, Sub, Neg}};
use crate::Rotation;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Delta {
pub dx: i32,
pub dy: i32,
}
impl Delta {
pub const LEFT: Self = Self::new(-1, 0);
pub const UP: Self = Self::new( 0, -1);
pub const RIGHT: Self = Self::new( 1, 0);
pub const DOWN: Self = Self::new( 0, 1);
pub const fn new(dx: i32, dy: i32) -> Self {
Self { dx, dy }
}
pub fn random_cardinal() -> Self {
Rotation::random_cardinal() * Self::RIGHT
}
}
impl fmt::Display for Delta {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}, {})", self.dx, self.dy)
}
}
impl Add for Delta {
type Output = Delta;
fn add(self, rhs: Self) -> Self {
Self::new(self.dx + rhs.dx, self.dy + rhs.dy)
}
}
impl Sub for Delta {
type Output = Delta;
fn sub(self, rhs: Self) -> Self {
Self::new(self.dx - rhs.dx, self.dy - rhs.dy)
}
}
impl Neg for Delta {
type Output = Delta;
fn neg(self) -> Self {
Self::new(-self.dx, -self.dy)
}
}