impl From<(x,y,r)> for Loc

This commit is contained in:
tali 2022-12-14 15:03:21 -05:00
parent b3d9e3c476
commit 096189373c
2 changed files with 14 additions and 26 deletions

View File

@ -15,13 +15,20 @@ pub struct Loc {
pub r: Rot,
}
impl From<(i16, i16)> for Loc {
fn from((x, y): (i16, i16)) -> Self {
let r = Rot::default();
impl From<(i16, i16, Rot)> for Loc {
#[inline]
fn from((x, y, r): (i16, i16, Rot)) -> Self {
Loc { x, y, r }
}
}
impl From<(i16, i16)> for Loc {
#[inline]
fn from((x, y): (i16, i16)) -> Self {
(x, y, Rot::default()).into()
}
}
/// 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)]

View File

@ -61,14 +61,7 @@ mod test {
fn test_spawn() {
use Shape::*;
for s in [L, O, J, I, S, T, Z] {
assert_eq!(
Piece::spawn(s).loc,
Loc {
x: 4,
y: 19,
r: Rot::N
}
);
assert_eq!(Piece::spawn(s).loc, (4, 19, Rot::N).into());
}
}
@ -129,11 +122,7 @@ mod test {
use input::Spin::*;
let mut piece = Piece {
shape: Shape::O,
loc: Loc {
x: 6,
y: 6,
r: Rot::N,
},
loc: (6, 6, Rot::N).into(),
};
assert_eq!(input::rotate(&mut piece, Mat::EMPTY, Cw), Some(0));
assert_eq!(piece.loc.x, 6);
@ -160,11 +149,7 @@ mod test {
};
let mut piece = Piece {
shape: Shape::T,
loc: Loc {
x: 5,
y: 3,
r: Rot::N,
},
loc: (5, 3, Rot::N).into(),
};
assert_eq!(input::rotate(&mut piece, SETUP, input::Spin::Ccw), Some(4));
assert_eq!(piece.loc.x, 6);
@ -182,11 +167,7 @@ mod test {
};
let mut piece = Piece {
shape: Shape::Z,
loc: Loc {
x: 5,
y: 3,
r: Rot::N,
},
loc: (5, 3, Rot::N).into(),
};
assert_eq!(input::rotate(&mut piece, SETUP, input::Spin::Cw), Some(3));
assert_eq!(piece.loc.x, 5);