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
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use super::ICoord;

use enum_map::Enum;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Four-way directions.
///
/// These start at North and increment counter-clockwise,
/// so you can convert them to integers with `as` and use them
/// in rotational calculations if you need.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Enum)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Direction4 {
    North,
    East,
    South,
    West,
}

impl Direction4 {
    /// All the directions in order.
    /// This is used internally for rotations and flips.
    /// I made it public just in case it's helpful for you the programmer.
    pub const DIRECTIONS: [Direction4; 4] = [
        Direction4::North,
        Direction4::East,
        Direction4::South,
        Direction4::West,
    ];

    /// Rotate this by the given amount.
    ///
    /// ```
    /// # use cogs_gamedev::grids::{Direction4, Rotation};
    /// use Direction4::*;
    /// use Rotation::*;
    ///
    /// assert_eq!(North.rotate(Clockwise), East);
    /// assert_eq!(North.rotate(CounterClockwise), West);
    /// ```
    pub fn rotate(self, rot: Rotation) -> Self {
        self.rotate_by(rot.steps_clockwise())
    }

    /// Get this direction, rotated by this many steps clockwise.
    /// Negative numbers go counter-clockwise.
    ///
    /// ```
    /// # use cogs_gamedev::grids::Direction4;
    /// use Direction4::*;
    /// assert_eq!(North.rotate_by(1), East);
    /// assert_eq!(North.rotate_by(2), South);
    /// assert_eq!(North.rotate_by(-1), West);
    /// assert_eq!(North.rotate_by(5).rotate_by(-11), South);
    /// ```
    pub fn rotate_by(self, steps_clockwise: isize) -> Self {
        let idx = self as isize;
        let new_idx =
            ((idx + steps_clockwise).rem_euclid(Self::DIRECTIONS.len() as isize)) as usize;
        Self::DIRECTIONS[new_idx]
    }

    /// Flip this direction.
    ///
    /// ```
    /// # use cogs_gamedev::directions::Direction4;
    /// use Direction4::*;
    /// assert_eq!(North.flip(), South);
    /// assert_eq!(West.flip(), East);
    /// assert_eq!(East.flip().flip(), East);
    /// ```
    pub fn flip(self) -> Self {
        self.rotate_by(2)
    }

    /// Get this direction in radians.
    ///
    /// This uses trigonometric + graphical standard, where:
    /// - 0 radians is to the right
    /// - Positive radians increment *clockwise*. NOTE: this is opposite from normal trig,
    /// but makes sense in computer graphics where +Y is downwards.
    ///
    /// If you need it in degrees just call `.to_degrees` on the result.
    ///
    /// ```
    /// # use cogs_gamedev::directions::Direction4;
    /// use Direction4::*;
    /// use std::f32::consts::TAU;
    ///
    /// let north_radians = North.radians();
    /// assert!((north_radians - (TAU / 4.0 * 3.0)).abs() < 1e-10);
    ///
    /// let west_radians = West.radians();
    /// assert!((west_radians - (TAU / 4.0 * 2.0)).abs() < 1e-10);
    ///
    /// ```
    pub fn radians(self) -> f32 {
        ((self as i8) - 1).rem_euclid(4) as f32 * std::f32::consts::TAU / 4.0
    }

    /// Get the deltas a step in this direction would result in, as a ICoord.
    ///
    /// ```
    /// # use cogs_gamedev::directions::Direction4;
    /// # use cogs_gamedev::int_coords::ICoord;
    /// use Direction4::*;
    ///
    /// assert_eq!(North.deltas(), ICoord {x: 0, y: -1});
    /// assert_eq!(West.deltas(), ICoord {x: -1, y: 0});
    /// ```
    pub fn deltas(self) -> ICoord {
        let (x, y) = match self {
            Direction4::North => (0, -1),
            Direction4::East => (1, 0),
            Direction4::South => (0, 1),
            Direction4::West => (-1, 0),
        };
        ICoord { x, y }
    }
}

/// Eight-way directions.
///
/// These start at North and increment counter-clockwise,
/// so you can convert them to integers with `as` and use them
/// in rotational calculations if you need.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Enum)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Direction8 {
    North,
    NorthEast,
    East,
    SouthEast,
    South,
    SouthWest,
    West,
    NorthWest,
}

impl Direction8 {
    /// All the directions in order.
    /// This is used internally for rotations and flips.
    /// I made it public just in case it's helpful for you the programmer.
    pub const DIRECTIONS: [Direction8; 8] = [
        Direction8::North,
        Direction8::NorthEast,
        Direction8::East,
        Direction8::SouthEast,
        Direction8::South,
        Direction8::SouthWest,
        Direction8::West,
        Direction8::NorthWest,
    ];

    /// Rotate this by the given amount.
    ///
    /// ```
    /// # use cogs_gamedev::grids::{Direction8, Rotation};
    /// use Direction8::*;
    /// use Rotation::*;
    ///
    /// assert_eq!(NorthEast.rotate(Clockwise), East);
    /// assert_eq!(South.rotate(CounterClockwise), SouthWest);
    /// ```
    pub fn rotate(self, rot: Rotation) -> Self {
        self.rotate_by(rot.steps_clockwise())
    }

    /// Get this direction, rotated by this many steps clockwise.
    /// Negative numbers go counter-clockwise.
    ///
    /// ```
    /// # use cogs_gamedev::grids::Direction8;
    /// use Direction8::*;
    /// let north = North;
    /// assert_eq!(north.rotate_by(1), NorthEast);
    /// assert_eq!(north.rotate_by(2), East);
    /// assert_eq!(north.rotate_by(-1), NorthWest);
    /// assert_eq!(north.rotate_by(4), South);
    /// assert_eq!(north.rotate_by(5).rotate_by(-11), East);
    /// ```
    pub fn rotate_by(self, steps_clockwise: isize) -> Self {
        let idx = self as isize;
        let new_idx =
            ((idx + steps_clockwise).rem_euclid(Self::DIRECTIONS.len() as isize)) as usize;
        Self::DIRECTIONS[new_idx]
    }

    /// Flip this direction.
    ///
    /// ```
    /// # use cogs_gamedev::directions::Direction8;
    /// use Direction8::*;
    /// assert_eq!(North.flip(), South);
    /// assert_eq!(West.flip(), East);
    /// assert_eq!(SouthWest.flip(), NorthEast);
    /// assert_eq!(East.flip().flip(), East);
    /// ```
    pub fn flip(self) -> Self {
        self.rotate_by(4)
    }

    /// Get this direction in radians.
    ///
    /// This uses trigonometric + graphical standard, where:
    /// - 0 radians is to the right
    /// - Positive radians increment *clockwise*. NOTE: this is opposite from normal trig,
    /// but makes sense in computer graphics where +Y is downwards.
    ///
    /// If you need it in degrees just call `.to_degrees` on the result.
    ///
    /// ```
    /// # use cogs_gamedev::directions::Direction8;
    /// use Direction8::*;
    /// use std::f32::consts::TAU;
    ///
    /// let north_radians = North.radians();
    /// assert!((north_radians - (TAU / 8.0 * 6.0)).abs() < 1e-10);
    ///
    /// let west_radians = West.radians();
    /// assert!((west_radians - (TAU / 8.0 * 4.0)).abs() < 1e-10);
    ///
    /// let southeast_radians = SouthEast.radians();
    /// assert!((southeast_radians - (TAU / 8.0)).abs() < 1e-10);
    ///
    /// ```
    pub fn radians(self) -> f32 {
        ((self as i8) - 2).rem_euclid(8) as f32 * std::f32::consts::TAU / 8.0
    }

    /// Get the deltas a step in this direction would result in,
    /// as an ICoord.
    ///
    /// ```
    /// # use cogs_gamedev::directions::Direction8;
    /// # use cogs_gamedev::int_coords::ICoord;
    /// use Direction8::*;
    ///
    /// assert_eq!(East.deltas(), ICoord {x: 1, y: 0});
    /// assert_eq!(NorthWest.deltas(), ICoord {x: -1, y: -1});
    /// ```
    pub fn deltas(self) -> ICoord {
        let (x, y) = match self {
            Direction8::North => (0, -1),
            Direction8::NorthEast => (1, -1),
            Direction8::East => (1, 0),
            Direction8::SouthEast => (1, 1),
            Direction8::South => (0, 1),
            Direction8::SouthWest => (-1, 1),
            Direction8::West => (-1, 0),
            Direction8::NorthWest => (-1, -1),
        };
        ICoord { x, y }
    }
}

/// 2-way rotations: clockwise or counterclockwise.
/// These don't indicate any specific angle by themselves, only in relation to something.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Enum)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Rotation {
    Clockwise,
    CounterClockwise,
}

impl Rotation {
    /// Get the number of steps clockwise this does.
    /// - `Clockwise` is 1
    /// - `CounterClockwise` is -1
    pub fn steps_clockwise(&self) -> isize {
        match self {
            Rotation::Clockwise => 1,
            Rotation::CounterClockwise => -1,
        }
    }
}