Add Piece::spawn() function, Spawn trait

This commit is contained in:
tali 2022-12-14 13:36:13 -05:00
parent 614c183234
commit aefa1deb6a
1 changed files with 20 additions and 0 deletions

View File

@ -15,6 +15,13 @@ pub struct Loc {
pub r: Rot,
}
impl From<(i16, i16)> for Loc {
fn from((x, y): (i16, i16)) -> Self {
let r = Rot::default();
Loc { x, y, r }
}
}
/// 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)]
@ -165,6 +172,19 @@ impl<'c, S: Shape<'c>> Piece<S> {
}
}
/// Interface for piece types that have a initial spawn location.
pub trait Spawn {
/// Returns the (x,y) coordinates where the piece should 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 }
}
}
#[cfg(test)]
pub mod test {
use super::*;