Rename a lot of stuff related to piece types
- srs::Shape => srs::PieceType - Piece.shape => Piece.ty - Piece<S> => Piece<T>
This commit is contained in:
parent
41df73bafc
commit
0d52d6b1b5
|
@ -148,13 +148,13 @@ where
|
|||
/// Rotates a piece, performing kicks to find a final location. Returns `Some(idx)` if
|
||||
/// successful, where `idx` is the index into the kick table performed. Index `0` means it
|
||||
/// was not kicked. Returns `None` if the rotation failed.
|
||||
pub fn rotate<'c, 'k, S>(piece: &mut Piece<S>, mat: &Mat, dir: Spin) -> Option<usize>
|
||||
pub fn rotate<'c, 'k, T>(piece: &mut Piece<T>, mat: &Mat, dir: Spin) -> Option<usize>
|
||||
where
|
||||
S: Shape<'c> + Kicks<'k>,
|
||||
T: Shape<'c> + Kicks<'k>,
|
||||
{
|
||||
let r0 = piece.loc.r;
|
||||
let r1 = piece.loc.r + dir;
|
||||
let kicks = piece.shape.kicks(r0, r1);
|
||||
let kicks = piece.ty.kicks(r0, r1);
|
||||
|
||||
piece.loc.r = r1;
|
||||
let cells = piece.cells();
|
||||
|
@ -208,7 +208,7 @@ mod test {
|
|||
|
||||
fn drop(x: i16, y: i16, r: Rot) -> i16 {
|
||||
let mut piece = Piece {
|
||||
shape: Tri,
|
||||
ty: Tri,
|
||||
loc: Loc { x, y, r },
|
||||
};
|
||||
let did_fall = super::drop(&mut piece, MAT);
|
||||
|
@ -238,7 +238,7 @@ mod test {
|
|||
|
||||
fn shift(x: i16, y: i16, r: Rot, dir: Horizontal) -> i16 {
|
||||
let mut piece = Piece {
|
||||
shape: Tri,
|
||||
ty: Tri,
|
||||
loc: Loc { x, y, r },
|
||||
};
|
||||
let did_shift = super::shift(&mut piece, MAT, dir);
|
||||
|
@ -278,7 +278,7 @@ mod test {
|
|||
|
||||
fn rotate(x: i16, y: i16, r: Rot, dir: Spin) -> (i16, i16, Rot, Option<usize>) {
|
||||
let loc = Loc { x, y, r };
|
||||
let mut piece = Piece { shape: Tri, loc };
|
||||
let mut piece = Piece { ty: Tri, loc };
|
||||
let result = super::rotate(&mut piece, MAT, dir);
|
||||
assert_eq!(result.is_some(), piece.loc != loc, "{:?},{:?}", piece, loc);
|
||||
(piece.loc.x, piece.loc.y, piece.loc.r, result)
|
||||
|
|
|
@ -198,19 +198,17 @@ impl core::fmt::Debug for Cells<'_> {
|
|||
|
||||
/// Represents the current state of a piece.
|
||||
#[derive(Clone, Eq, PartialEq, Debug, Hash)]
|
||||
pub struct Piece<S> {
|
||||
/// The shape of the piece.
|
||||
pub shape: S,
|
||||
pub struct Piece<T> {
|
||||
/// The piece type (i.e. its shape).
|
||||
pub ty: T,
|
||||
/// The location of the piece.
|
||||
pub loc: Loc,
|
||||
}
|
||||
|
||||
impl<'c, S: Shape<'c>> Piece<S> {
|
||||
impl<'c, T: Shape<'c>> Piece<T> {
|
||||
/// Returns the cells occupied by this piece.
|
||||
pub fn cells(&self) -> Cells<'c> {
|
||||
self.shape
|
||||
.cells(self.loc.r)
|
||||
.translate(self.loc.x, self.loc.y)
|
||||
self.ty.cells(self.loc.r).translate(self.loc.x, self.loc.y)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -220,10 +218,10 @@ pub trait Spawn {
|
|||
fn spawn_pos(&self) -> (i16, i16);
|
||||
}
|
||||
|
||||
impl<S: Spawn> Piece<S> {
|
||||
pub fn spawn(shape: S) -> Self {
|
||||
let loc = shape.spawn_pos().into();
|
||||
Self { shape, loc }
|
||||
impl<T: Spawn> Piece<T> {
|
||||
pub fn spawn(ty: T) -> Self {
|
||||
let loc = ty.spawn_pos().into();
|
||||
Self { ty, loc }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -268,7 +266,7 @@ pub mod test {
|
|||
|
||||
fn coords(x: i16, y: i16, r: Rot) -> Vec<(i16, i16)> {
|
||||
let piece = Piece {
|
||||
shape: Tri,
|
||||
ty: Tri,
|
||||
loc: Loc { x, y, r },
|
||||
};
|
||||
let mut coords: Vec<_> = piece.cells().coords().collect();
|
||||
|
@ -293,7 +291,7 @@ pub mod test {
|
|||
fn isects(xs: RangeInclusive<i16>, y: i16, r: Rot) -> Vec<bool> {
|
||||
xs.map(|x| {
|
||||
let piece = Piece {
|
||||
shape: Tri,
|
||||
ty: Tri,
|
||||
loc: Loc { x, y, r },
|
||||
};
|
||||
piece.cells().intersects(MAT)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
//! Implementation of SRS shapes and movement quirks.
|
||||
|
||||
use crate::input::Kicks as KicksTrait;
|
||||
use crate::piece::{Cells, Rot, Shape as ShapeTrait, Spawn as SpawnTrait};
|
||||
use crate::input::Kicks;
|
||||
use crate::piece::{Cells, Rot, Shape, Spawn};
|
||||
|
||||
mod code_gen {
|
||||
include!(concat!(env!("OUT_DIR"), "/srs.rs"));
|
||||
|
@ -9,7 +9,7 @@ mod code_gen {
|
|||
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
|
||||
#[repr(u8)]
|
||||
pub enum Shape {
|
||||
pub enum PieceType {
|
||||
I = code_gen::shape_indices::I as u8,
|
||||
J = code_gen::shape_indices::J as u8,
|
||||
L = code_gen::shape_indices::L as u8,
|
||||
|
@ -19,7 +19,7 @@ pub enum Shape {
|
|||
Z = code_gen::shape_indices::Z as u8,
|
||||
}
|
||||
|
||||
impl ShapeTrait<'static> for Shape {
|
||||
impl Shape<'static> for PieceType {
|
||||
fn cells(&self, r: Rot) -> Cells<'static> {
|
||||
let i = (*self as usize) * 4 + r as usize;
|
||||
let xs = code_gen::extents::X0[i]..code_gen::extents::X1[i];
|
||||
|
@ -30,7 +30,7 @@ impl ShapeTrait<'static> for Shape {
|
|||
}
|
||||
}
|
||||
|
||||
impl KicksTrait<'static> for Shape {
|
||||
impl Kicks<'static> for PieceType {
|
||||
fn kicks(&self, r0: Rot, r1: Rot) -> &'static [(i16, i16)] {
|
||||
let i = (*self as usize) * 16 + (r0 as usize) * 4 + r1 as usize;
|
||||
let idx = code_gen::KICKS_INDEX[i];
|
||||
|
@ -39,13 +39,13 @@ impl KicksTrait<'static> for Shape {
|
|||
}
|
||||
}
|
||||
|
||||
impl SpawnTrait for Shape {
|
||||
impl Spawn for PieceType {
|
||||
fn spawn_pos(&self) -> (i16, i16) {
|
||||
code_gen::SPAWN
|
||||
}
|
||||
}
|
||||
|
||||
pub type Piece = crate::piece::Piece<Shape>;
|
||||
pub type Piece = crate::piece::Piece<PieceType>;
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
|
@ -57,19 +57,18 @@ mod test {
|
|||
use alloc::vec::Vec;
|
||||
use core::ops::Range;
|
||||
|
||||
use PieceType::*;
|
||||
|
||||
#[test]
|
||||
fn test_spawn() {
|
||||
use Shape::*;
|
||||
for s in [L, O, J, I, S, T, Z] {
|
||||
assert_eq!(Piece::spawn(s).loc, (4, 19, Rot::N).into());
|
||||
for ty in [L, O, J, I, S, T, Z] {
|
||||
assert_eq!(Piece::spawn(ty).loc, (4, 19, Rot::N).into());
|
||||
}
|
||||
}
|
||||
|
||||
fn get_cells(s: Shape, r: Rot) -> (Range<i16>, Range<i16>, Vec<(i16, i16)>) {
|
||||
let piece = Piece {
|
||||
shape: s,
|
||||
loc: Loc { x: 0, y: 0, r },
|
||||
};
|
||||
fn get_cells(ty: PieceType, r: Rot) -> (Range<i16>, Range<i16>, Vec<(i16, i16)>) {
|
||||
let loc = Loc { x: 0, y: 0, r };
|
||||
let piece = Piece { ty, loc };
|
||||
let mut coords = piece.cells().coords().collect::<Vec<_>>();
|
||||
coords.sort();
|
||||
let (xs, ys) = piece.cells().extents();
|
||||
|
@ -96,32 +95,32 @@ mod test {
|
|||
xy
|
||||
}
|
||||
|
||||
fn test_cells_all_rotations(s: Shape, expected_coords: &[(i16, i16)]) {
|
||||
fn test_cells_all_rotations(ty: PieceType, expected_coords: &[(i16, i16)]) {
|
||||
for r in (0..4).map(Rot::from) {
|
||||
let (act_xs, act_ys, act_coords) = get_cells(s, r);
|
||||
let (act_xs, act_ys, act_coords) = get_cells(ty, r);
|
||||
let (exp_xs, exp_ys, exp_coords) = compute_cells(expected_coords, r);
|
||||
assert_eq!(act_xs, exp_xs, "{s:?},{r:?}");
|
||||
assert_eq!(act_ys, exp_ys, "{s:?},{r:?}");
|
||||
assert_eq!(act_coords, exp_coords, "{s:?},{r:?}");
|
||||
assert_eq!(act_xs, exp_xs, "{ty:?},{r:?}");
|
||||
assert_eq!(act_ys, exp_ys, "{ty:?},{r:?}");
|
||||
assert_eq!(act_coords, exp_coords, "{ty:?},{r:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_srs_cells() {
|
||||
test_cells_all_rotations(Shape::I, &[(-1, 0), (0, 0), (1, 0), (2, 0)]);
|
||||
test_cells_all_rotations(Shape::J, &[(-1, 1), (-1, 0), (0, 0), (1, 0)]);
|
||||
test_cells_all_rotations(Shape::L, &[(1, 1), (-1, 0), (0, 0), (1, 0)]);
|
||||
test_cells_all_rotations(Shape::O, &[(0, 0), (1, 0), (0, 1), (1, 1)]);
|
||||
test_cells_all_rotations(Shape::S, &[(-1, 0), (0, 0), (0, 1), (1, 1)]);
|
||||
test_cells_all_rotations(Shape::T, &[(0, 1), (-1, 0), (0, 0), (1, 0)]);
|
||||
test_cells_all_rotations(Shape::Z, &[(1, 0), (0, 0), (0, 1), (-1, 1)]);
|
||||
test_cells_all_rotations(I, &[(-1, 0), (0, 0), (1, 0), (2, 0)]);
|
||||
test_cells_all_rotations(J, &[(-1, 1), (-1, 0), (0, 0), (1, 0)]);
|
||||
test_cells_all_rotations(L, &[(1, 1), (-1, 0), (0, 0), (1, 0)]);
|
||||
test_cells_all_rotations(O, &[(0, 0), (1, 0), (0, 1), (1, 1)]);
|
||||
test_cells_all_rotations(S, &[(-1, 0), (0, 0), (0, 1), (1, 1)]);
|
||||
test_cells_all_rotations(T, &[(0, 1), (-1, 0), (0, 0), (1, 0)]);
|
||||
test_cells_all_rotations(Z, &[(1, 0), (0, 0), (0, 1), (-1, 1)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_srs_o_kicks_in_place() {
|
||||
use input::Spin::*;
|
||||
let mut piece = Piece {
|
||||
shape: Shape::O,
|
||||
ty: O,
|
||||
loc: (6, 6, Rot::N).into(),
|
||||
};
|
||||
assert_eq!(input::rotate(&mut piece, Mat::EMPTY, Cw), Some(0));
|
||||
|
@ -148,7 +147,7 @@ mod test {
|
|||
"xxxxxx.xxx";
|
||||
};
|
||||
let mut piece = Piece {
|
||||
shape: Shape::T,
|
||||
ty: T,
|
||||
loc: (5, 3, Rot::N).into(),
|
||||
};
|
||||
assert_eq!(input::rotate(&mut piece, SETUP, input::Spin::Ccw), Some(4));
|
||||
|
@ -166,7 +165,7 @@ mod test {
|
|||
"xxxxx.xxxx";
|
||||
};
|
||||
let mut piece = Piece {
|
||||
shape: Shape::Z,
|
||||
ty: Z,
|
||||
loc: (5, 3, Rot::N).into(),
|
||||
};
|
||||
assert_eq!(input::rotate(&mut piece, SETUP, input::Spin::Cw), Some(3));
|
||||
|
@ -177,18 +176,18 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn test_srs_cells_eq() {
|
||||
fn rotate(s: Shape, dir: input::Spin) -> Piece {
|
||||
let mut piece = Piece::spawn(s);
|
||||
fn rotate(ty: PieceType, dir: input::Spin) -> Piece {
|
||||
let mut piece = Piece::spawn(ty);
|
||||
input::rotate(&mut piece, Mat::EMPTY, dir).unwrap();
|
||||
piece
|
||||
}
|
||||
for s in [Shape::S, Shape::Z, Shape::I] {
|
||||
let cw = rotate(s, input::Spin::Cw);
|
||||
let ccw = rotate(s, input::Spin::Ccw);
|
||||
for ty in [S, Z, I] {
|
||||
let cw = rotate(ty, input::Spin::Cw);
|
||||
let ccw = rotate(ty, input::Spin::Ccw);
|
||||
// rotation states differ
|
||||
assert_ne!(cw.loc.r, ccw.loc.r, "shape={s:?}");
|
||||
assert_ne!(cw.loc.r, ccw.loc.r, "ty={ty:?}");
|
||||
// cells() are equal after translating, since they occupy the same blocks
|
||||
assert_eq!(cw.cells().translate(-1, 0), ccw.cells(), "shape={s:?}");
|
||||
assert_eq!(cw.cells().translate(-1, 0), ccw.cells(), "ty={ty:?}");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -196,7 +195,7 @@ mod test {
|
|||
fn test_o_cells_eq() {
|
||||
fn cells(x: i16, y: i16, r: Rot) -> Cells<'static> {
|
||||
Piece {
|
||||
shape: Shape::O,
|
||||
ty: O,
|
||||
loc: Loc { x, y, r },
|
||||
}
|
||||
.cells()
|
||||
|
|
Loading…
Reference in New Issue