bugged
This commit is contained in:
parent
450900b167
commit
2aa1602dc1
3 changed files with 55 additions and 11 deletions
43
src/game.rs
43
src/game.rs
|
@ -25,6 +25,9 @@ pub struct Game {
|
||||||
next_lock_tick: u64,
|
next_lock_tick: u64,
|
||||||
next_spawn_tick: u64,
|
next_spawn_tick: u64,
|
||||||
is_game_over: bool,
|
is_game_over: bool,
|
||||||
|
/// The last clear action performed, used for determining if a back-to-back
|
||||||
|
/// bonus is needed.
|
||||||
|
last_clear_action: ClearAction,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for Game {
|
impl fmt::Debug for Game {
|
||||||
|
@ -47,6 +50,7 @@ impl Default for Game {
|
||||||
next_lock_tick: 0,
|
next_lock_tick: 0,
|
||||||
next_spawn_tick: 0,
|
next_spawn_tick: 0,
|
||||||
is_game_over: false,
|
is_game_over: false,
|
||||||
|
last_clear_action: ClearAction::Single, // Doesn't matter what it's initialized to
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -60,7 +64,6 @@ impl Tickable for Game {
|
||||||
if self.is_game_over() {
|
if self.is_game_over() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.tick += 1;
|
self.tick += 1;
|
||||||
match self.tick {
|
match self.tick {
|
||||||
t if t == self.next_spawn_tick => self.spawn_tetromino(),
|
t if t == self.next_spawn_tick => self.spawn_tetromino(),
|
||||||
|
@ -79,6 +82,18 @@ impl Tickable for Game {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum ClearAction {
|
||||||
|
Single,
|
||||||
|
Double,
|
||||||
|
Triple,
|
||||||
|
Tetris,
|
||||||
|
MiniTSpin,
|
||||||
|
TSpin,
|
||||||
|
TSpinSingle,
|
||||||
|
TSpinDouble,
|
||||||
|
TSpinTriple,
|
||||||
|
}
|
||||||
|
|
||||||
impl Game {
|
impl Game {
|
||||||
pub fn is_game_over(&self) -> bool {
|
pub fn is_game_over(&self) -> bool {
|
||||||
self.is_game_over || !self.playfield.is_active_piece_in_valid_position()
|
self.is_game_over || !self.playfield.is_active_piece_in_valid_position()
|
||||||
|
@ -99,9 +114,22 @@ impl Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns if some lines were cleared
|
/// Returns if some lines were cleared
|
||||||
fn clear_lines(&mut self) -> bool {
|
fn clear_lines(&mut self) -> usize {
|
||||||
// todo: award points based on how lines were cleared
|
let rows = self
|
||||||
return false;
|
.playfield
|
||||||
|
.active_piece
|
||||||
|
.map(|t| t.get_cur_occupied_spaces())
|
||||||
|
.map(|i| i.iter().map(|p| p.y).collect::<Vec<_>>())
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
let mut rows_cleared = 0;
|
||||||
|
for row in rows {
|
||||||
|
if self.playfield.try_clear_row(row as usize).is_ok() {
|
||||||
|
rows_cleared += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rows_cleared
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_lock_tetromino(&mut self) -> bool {
|
fn try_lock_tetromino(&mut self) -> bool {
|
||||||
|
@ -110,11 +138,14 @@ impl Game {
|
||||||
let positions = self.playfield.lock_active_piece();
|
let positions = self.playfield.lock_active_piece();
|
||||||
self.is_game_over =
|
self.is_game_over =
|
||||||
self.is_game_over || positions.iter().all(|Position { x: _, y }| *y < 20);
|
self.is_game_over || positions.iter().all(|Position { x: _, y }| *y < 20);
|
||||||
if self.clear_lines() {
|
|
||||||
|
if self.clear_lines() > 0 {
|
||||||
|
self.playfield.active_piece = None;
|
||||||
self.next_spawn_tick = self.tick + LINE_CLEAR_DELAY;
|
self.next_spawn_tick = self.tick + LINE_CLEAR_DELAY;
|
||||||
} else {
|
} else {
|
||||||
self.spawn_tetromino();
|
self.spawn_tetromino();
|
||||||
}
|
}
|
||||||
|
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
|
@ -191,6 +222,8 @@ impl Controllable for Game {
|
||||||
if !self.try_lock_tetromino() {
|
if !self.try_lock_tetromino() {
|
||||||
println!("couldn't lock tetromino despite hard dropping!");
|
println!("couldn't lock tetromino despite hard dropping!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.next_lock_tick = std::u64::MAX;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hold(&mut self) {
|
fn hold(&mut self) {
|
||||||
|
|
|
@ -85,5 +85,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
interval.tick().await;
|
interval.tick().await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dbg!(game);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -171,19 +171,16 @@ impl PlayField {
|
||||||
self.field[*y as usize][*x as usize] = Some(active_color);
|
self.field[*y as usize][*x as usize] = Some(active_color);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.active_piece = None;
|
|
||||||
new_pieces
|
new_pieces
|
||||||
}
|
}
|
||||||
None => panic!("Tried to lock active piece while active piece doesn't exist!"),
|
None => vec![],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_active_piece_in_valid_position(&self) -> bool {
|
pub fn is_active_piece_in_valid_position(&self) -> bool {
|
||||||
match self.active_piece {
|
match self.active_piece {
|
||||||
Some(active_piece) => {
|
Some(active_piece) => self.can_piece_be_at_position(&active_piece),
|
||||||
self.can_piece_be_at_position(&active_piece)
|
None => true,
|
||||||
},
|
|
||||||
None => panic!("Tried checking if active piece is in a valid position but active piece doesn't exist")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -216,6 +213,18 @@ impl PlayField {
|
||||||
|
|
||||||
Err(())
|
Err(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn try_clear_row(&mut self, row: usize) -> Result<(), ()> {
|
||||||
|
if self.field[row].iter().all(|cell| cell.is_some()) {
|
||||||
|
self.field[row] = [None; PLAYFIELD_WIDTH];
|
||||||
|
for y in (1..=row).rev() {
|
||||||
|
self.field[y] = self.field[y - 1];
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Renderable for PlayField {
|
impl Renderable for PlayField {
|
||||||
|
|
Loading…
Reference in a new issue