Rename types in location.rs
This commit is contained in:
parent
0597b3b538
commit
fa6aac86f6
|
@ -3,7 +3,7 @@
|
|||
pub mod location;
|
||||
pub mod matrix;
|
||||
|
||||
pub use location::Location;
|
||||
pub use location::Loc;
|
||||
pub use matrix::Mat;
|
||||
|
||||
/// Allows constructing a `Mat` constant with string literals:
|
||||
|
|
|
@ -2,20 +2,20 @@
|
|||
|
||||
/// Represents a location for a piece, including its orientation.
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
||||
pub struct Location {
|
||||
pub struct Loc {
|
||||
/// Horizontal coordinate.
|
||||
pub x: i16,
|
||||
/// Vertical coordinate.
|
||||
pub y: i16,
|
||||
/// Orientation.
|
||||
pub r: Orientation,
|
||||
/// Rotation state.
|
||||
pub r: Rot,
|
||||
}
|
||||
|
||||
/// Represents an orientation state for a piece. The initial state is "north" (`N`), and
|
||||
/// there are 4 total orientations to represent each 90 degree turn possible.
|
||||
/// Represents a rotation state for a piece. The initial state is "north" (`N`), and there
|
||||
/// are 4 total orientations to represent each 90 degree turn possible.
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Default)]
|
||||
#[repr(i8)]
|
||||
pub enum Orientation {
|
||||
pub enum Rot {
|
||||
/// North. The initial state.
|
||||
#[default]
|
||||
N = 0,
|
||||
|
@ -27,26 +27,26 @@ pub enum Orientation {
|
|||
W = 3,
|
||||
}
|
||||
|
||||
impl From<i8> for Orientation {
|
||||
impl From<i8> for Rot {
|
||||
#[inline]
|
||||
fn from(v: i8) -> Self {
|
||||
unsafe { core::mem::transmute(v & 3) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents a rotation operation. Includes 180 degree "flips", which are non-standard
|
||||
/// Represents a rotating operation. Includes 180 degree "flips", which are non-standard
|
||||
/// for tetris but exist in many unofficial iterations of the game.
|
||||
///
|
||||
/// Rotations may be combined with orientations using the [addition
|
||||
/// Rotations may be performed on [rotation states](Rot) using the [addition
|
||||
/// operator](core::ops::Add).
|
||||
///
|
||||
/// ```
|
||||
/// # use mino::location::{Orientation, Rotation};
|
||||
/// assert_eq!(Orientation::N + Rotation::Cw, Orientation::E);
|
||||
/// # use mino::location::{Rot, Spin};
|
||||
/// assert_eq!(Rot::N + Spin::Cw, Rot::E);
|
||||
/// ```
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
|
||||
#[repr(i8)]
|
||||
pub enum Rotation {
|
||||
pub enum Spin {
|
||||
/// Clockwise.
|
||||
Cw = 1,
|
||||
/// 180 degrees.
|
||||
|
@ -55,15 +55,15 @@ pub enum Rotation {
|
|||
Ccw = 3,
|
||||
}
|
||||
|
||||
impl core::ops::Add<Rotation> for Orientation {
|
||||
type Output = Orientation;
|
||||
fn add(self, r: Rotation) -> Orientation {
|
||||
impl core::ops::Add<Spin> for Rot {
|
||||
type Output = Rot;
|
||||
fn add(self, r: Spin) -> Rot {
|
||||
(self as i8).wrapping_add(r as i8).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl core::ops::AddAssign<Rotation> for Orientation {
|
||||
fn add_assign(&mut self, r: Rotation) {
|
||||
impl core::ops::AddAssign<Spin> for Rot {
|
||||
fn add_assign(&mut self, r: Spin) {
|
||||
*self = *self + r;
|
||||
}
|
||||
}
|
||||
|
@ -74,8 +74,8 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn test_angle_rotate() {
|
||||
use Orientation::*;
|
||||
use Rotation::*;
|
||||
use Rot::*;
|
||||
use Spin::*;
|
||||
assert_eq!(N + Cw, E);
|
||||
assert_eq!(N + Ccw, W);
|
||||
assert_eq!(N + Flip, S);
|
||||
|
@ -96,14 +96,14 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn test_angle_from_i8() {
|
||||
let mut r = Orientation::N;
|
||||
let mut r = Rot::N;
|
||||
for i in 0i8..=127 {
|
||||
assert_eq!(r, i.into(), "i={i}");
|
||||
r += Rotation::Cw;
|
||||
r += Spin::Cw;
|
||||
}
|
||||
r = Orientation::N;
|
||||
r = Rot::N;
|
||||
for i in (-128i8..=-1).rev() {
|
||||
r += Rotation::Ccw;
|
||||
r += Spin::Ccw;
|
||||
assert_eq!(r, i.into(), "i={i}");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue