Add Piece::spawn() function, Spawn trait
This commit is contained in:
parent
614c183234
commit
aefa1deb6a
|
@ -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::*;
|
||||
|
|
Loading…
Reference in New Issue