implement holding

This commit is contained in:
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) { fn spawn_tetromino(&mut self) {
self.playfield.spawn_tetromino(); self.playfield.spawn_tetromino();
self.playfield.tick_gravity();
self.update_gravity_tick(); self.update_gravity_tick();
} }
@ -195,20 +194,9 @@ impl Controllable for Game {
} }
fn hold(&mut self) { fn hold(&mut self) {
if self.can_swap_hold { match self.playfield.try_swap_hold() {
match self.hold_piece { Ok(_) => {}
None => { Err(_) => (),
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;
} }
} }
} }

View file

@ -134,6 +134,7 @@ impl PlayField {
)); ));
self.next_pieces.push_back(self.bag.get_tetromino()); self.next_pieces.push_back(self.bag.get_tetromino());
self.can_swap_hold = true; self.can_swap_hold = true;
self.tick_gravity();
} }
pub fn tick_gravity(&mut self) { pub fn tick_gravity(&mut self) {
@ -196,6 +197,25 @@ impl PlayField {
&& self.field[*y as usize][*x as usize].is_none() && 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 { impl Renderable for PlayField {

View file

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