Add back Spin type in new submodule 'input'

This commit is contained in:
tali 2022-12-14 10:34:52 -05:00
parent 2dc972c756
commit 1075546dd8
2 changed files with 66 additions and 0 deletions

65
mino/src/input.rs Normal file
View File

@ -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);
}
}

View File

@ -1,5 +1,6 @@
#![no_std]
pub mod input;
pub mod matrix;
pub mod piece;