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
62
use crate::coord::{CardinalCoord, UnitCoord};
use grid_2d::Coord;
#[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize};

#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug)]
pub struct Step {
    pub to_coord: Coord,
    pub in_direction: UnitCoord,
}

impl Step {
    pub fn forward(&self) -> Self {
        let in_direction = self.in_direction;
        Self {
            to_coord: self.to_coord + in_direction.to_coord(),
            in_direction,
        }
    }
    pub fn left(&self) -> Self {
        let in_direction = self.in_direction.left90();
        Self {
            to_coord: self.to_coord + in_direction.to_coord(),
            in_direction,
        }
    }
    pub fn right(&self) -> Self {
        let in_direction = self.in_direction.right90();
        Self {
            to_coord: self.to_coord + in_direction.to_coord(),
            in_direction,
        }
    }
    pub fn scale_back(&self, by: u32) -> Jump {
        Jump {
            to_coord: self.to_coord,
            in_direction: self.in_direction.scale(by),
        }
    }
    pub fn to_coord(&self) -> Coord {
        self.to_coord
    }
    pub fn from_coord(&self) -> Coord {
        self.to_coord - self.in_direction.to_coord()
    }
}

#[derive(Clone, Copy, Debug)]
pub struct Jump {
    pub to_coord: Coord,
    pub in_direction: CardinalCoord,
}

impl Jump {
    pub fn last_step(&self) -> Step {
        Step {
            to_coord: self.to_coord,
            in_direction: self.in_direction.to_unit_coord(),
        }
    }
}