Add back Spin type in new submodule 'input'
This commit is contained in:
parent
2dc972c756
commit
1075546dd8
|
@ -0,0 +1,65 @@
|
|||
//! Data structures and operations for game inputs, particularly ones that induce
|
||||
//! movements to the current piece.
|
||||
|
||||
use crate::piece::Rot;
|
||||
|
||||
/// 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 performed on [rotation states](Rot) using the [addition
|
||||
/// operator](core::ops::Add).
|
||||
///
|
||||
/// ```
|
||||
/// # use mino::{piece::Rot, input::Spin};
|
||||
/// assert_eq!(Rot::N + Spin::Cw, Rot::E);
|
||||
/// ```
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
|
||||
#[repr(i8)]
|
||||
pub enum Spin {
|
||||
/// Clockwise.
|
||||
Cw = 1,
|
||||
/// 180 degrees.
|
||||
Flip = 2,
|
||||
/// Counterclockwise.
|
||||
Ccw = -1,
|
||||
}
|
||||
|
||||
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<Spin> for Rot {
|
||||
fn add_assign(&mut self, r: Spin) {
|
||||
*self = *self + r;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_angle_rotate() {
|
||||
use Rot::*;
|
||||
use Spin::*;
|
||||
assert_eq!(N + Cw, E);
|
||||
assert_eq!(N + Ccw, W);
|
||||
assert_eq!(N + Flip, S);
|
||||
assert_eq!(E + Cw, S);
|
||||
assert_eq!(E + Ccw, N);
|
||||
assert_eq!(E + Flip, W);
|
||||
assert_eq!(S + Cw, W);
|
||||
assert_eq!(S + Ccw, E);
|
||||
assert_eq!(S + Flip, N);
|
||||
assert_eq!(W + Cw, N);
|
||||
assert_eq!(W + Ccw, S);
|
||||
assert_eq!(W + Flip, E);
|
||||
|
||||
let mut r = N;
|
||||
r += Ccw;
|
||||
assert_eq!(r, W);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
#![no_std]
|
||||
|
||||
pub mod input;
|
||||
pub mod matrix;
|
||||
pub mod piece;
|
||||
|
||||
|
|
Loading…
Reference in New Issue