implement holding

master
Edward Shen 2020-03-22 18:21:48 -04:00
parent d719338097
commit 450900b167
Signed by: edward
GPG Key ID: 19182661E818369F
3 changed files with 23 additions and 16 deletions

View File

@ -95,7 +95,6 @@ impl Game {
fn spawn_tetromino(&mut self) {
self.playfield.spawn_tetromino();
self.playfield.tick_gravity();
self.update_gravity_tick();
}
@ -195,20 +194,9 @@ impl Controllable for Game {
}
fn hold(&mut self) {
if self.can_swap_hold {
match self.hold_piece {
None => {
self.hold_piece = Some(self.active_piece);
self.get_new_piece();
}
Some(piece) => {
self.hold_piece = Some(self.active_piece);
self.active_piece = piece;
self.reset_position();
}
}
self.can_swap_hold = false;
match self.playfield.try_swap_hold() {
Ok(_) => {}
Err(_) => (),
}
}
}

View File

@ -134,6 +134,7 @@ impl PlayField {
));
self.next_pieces.push_back(self.bag.get_tetromino());
self.can_swap_hold = true;
self.tick_gravity();
}
pub fn tick_gravity(&mut self) {
@ -196,6 +197,25 @@ impl PlayField {
&& self.field[*y as usize][*x as usize].is_none()
})
}
pub fn try_swap_hold(&mut self) -> Result<(), ()> {
if self.can_swap_hold {
match self.active_piece {
Some(piece) => {
match self.hold_piece {
Some(hold) => self.next_pieces.push_front(hold),
None => (),
}
self.hold_piece = Some(piece.piece_type);
self.spawn_tetromino();
self.can_swap_hold = false;
}
None => return Err(()),
}
}
Err(())
}
}
impl Renderable for PlayField {

View File

@ -45,7 +45,6 @@ impl SRS {
}
let cur_offsets = offset_data.get(&test_tetromino.rotation_state).unwrap();
let mut offsets = Vec::with_capacity(cur_offsets.len());
for i in 0..cur_offsets.len() {