diff --git a/README.md b/README.md index ee73498..12b2034 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ -- clear lines - deadlocks? - fix arrow keys - better rng diff --git a/tetros/src/lib.rs b/tetros/src/lib.rs index c8cfcd1..2017f8c 100644 --- a/tetros/src/lib.rs +++ b/tetros/src/lib.rs @@ -165,7 +165,7 @@ extern "x86-interrupt" fn timer_handler(_stack_frame: InterruptStackFrame) { board.draw(&mut v, fall.as_ref()); if let Some(fall_inner) = fall.as_mut() { - if t % 6 == 0 { + if t % 5 == 0 { let mut fall_test = fall_inner.clone(); fall_test.translate(0, 1); @@ -175,6 +175,7 @@ extern "x86-interrupt" fn timer_handler(_stack_frame: InterruptStackFrame) { let mut x = None; core::mem::swap(&mut x, &mut fall); board.place_tetromino(x.unwrap()); + board.collapse(); } } } else { diff --git a/tetros/src/tetrisboard/mod.rs b/tetros/src/tetrisboard/mod.rs index 01c9d33..7a9ae0d 100644 --- a/tetros/src/tetrisboard/mod.rs +++ b/tetros/src/tetrisboard/mod.rs @@ -28,6 +28,39 @@ impl TetrisBoard { } } + pub fn collapse(&mut self) { + let mut y = Self::BOARD_HEIGHT - 1; + 'outer: loop { + for x in 0..Self::BOARD_WIDTH { + let cell = self.get_cell(x, y); + if cell == Some(&TetrisCell::Empty) { + if y == 0 { + break 'outer; + } + + y -= 1; + continue 'outer; + } + } + + // We found a row that needs to be cleared + + // Shift everything down + for yy in (1..=y).rev() { + for x in 0..Self::BOARD_WIDTH { + let top = *self.get_cell(x, yy - 1).unwrap(); + let bot = self.get_cell_mut(x, yy).unwrap(); + *bot = top; + } + } + + // Clear the top row + for x in 0..Self::BOARD_WIDTH { + *self.get_cell_mut(x, 0).unwrap() = TetrisCell::Empty; + } + } + } + pub fn place_tetromino(&mut self, tetromino: FallingTetromino) { for (x, y) in tetromino.tiles() { let cell = self.get_cell_mut(x, y);