Compare commits
No commits in common. "2aa1602dc1e76bb3bda06ab381557a5102dabd13" and "765b2655a3c4133d24a88e41a2283ad39eb2e494" have entirely different histories.
2aa1602dc1
...
765b2655a3
4 changed files with 31 additions and 86 deletions
70
src/game.rs
70
src/game.rs
|
@ -25,9 +25,6 @@ pub struct Game {
|
|||
next_lock_tick: u64,
|
||||
next_spawn_tick: u64,
|
||||
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 {
|
||||
|
@ -50,7 +47,6 @@ impl Default for Game {
|
|||
next_lock_tick: 0,
|
||||
next_spawn_tick: 0,
|
||||
is_game_over: false,
|
||||
last_clear_action: ClearAction::Single, // Doesn't matter what it's initialized to
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -64,6 +60,7 @@ impl Tickable for Game {
|
|||
if self.is_game_over() {
|
||||
return;
|
||||
}
|
||||
|
||||
self.tick += 1;
|
||||
match self.tick {
|
||||
t if t == self.next_spawn_tick => self.spawn_tetromino(),
|
||||
|
@ -82,26 +79,13 @@ impl Tickable for Game {
|
|||
}
|
||||
}
|
||||
|
||||
enum ClearAction {
|
||||
Single,
|
||||
Double,
|
||||
Triple,
|
||||
Tetris,
|
||||
MiniTSpin,
|
||||
TSpin,
|
||||
TSpinSingle,
|
||||
TSpinDouble,
|
||||
TSpinTriple,
|
||||
}
|
||||
|
||||
impl Game {
|
||||
pub fn is_game_over(&self) -> bool {
|
||||
self.is_game_over || !self.playfield.is_active_piece_in_valid_position()
|
||||
}
|
||||
|
||||
fn update_gravity_tick(&mut self) {
|
||||
// self.next_gravity_tick = (-1 as i64) as u64;
|
||||
self.next_gravity_tick = self.tick + TICKS_PER_SECOND as u64;
|
||||
self.next_gravity_tick = (-1 as i64) as u64; //self.tick + TICKS_PER_SECOND as u64;
|
||||
}
|
||||
|
||||
fn update_lock_tick(&mut self) {
|
||||
|
@ -110,26 +94,14 @@ impl Game {
|
|||
|
||||
fn spawn_tetromino(&mut self) {
|
||||
self.playfield.spawn_tetromino();
|
||||
self.playfield.tick_gravity();
|
||||
self.update_gravity_tick();
|
||||
}
|
||||
|
||||
/// Returns if some lines were cleared
|
||||
fn clear_lines(&mut self) -> usize {
|
||||
let rows = self
|
||||
.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 clear_lines(&mut self) -> bool {
|
||||
// todo: award points based on how lines were cleared
|
||||
return false;
|
||||
}
|
||||
|
||||
fn try_lock_tetromino(&mut self) -> bool {
|
||||
|
@ -138,14 +110,11 @@ impl Game {
|
|||
let positions = self.playfield.lock_active_piece();
|
||||
self.is_game_over =
|
||||
self.is_game_over || positions.iter().all(|Position { x: _, y }| *y < 20);
|
||||
|
||||
if self.clear_lines() > 0 {
|
||||
self.playfield.active_piece = None;
|
||||
if self.clear_lines() {
|
||||
self.next_spawn_tick = self.tick + LINE_CLEAR_DELAY;
|
||||
} else {
|
||||
self.spawn_tetromino();
|
||||
}
|
||||
|
||||
true
|
||||
} else {
|
||||
false
|
||||
|
@ -172,16 +141,12 @@ pub trait Controllable {
|
|||
impl Controllable for Game {
|
||||
fn move_left(&mut self) {
|
||||
self.playfield.move_offset(-1, 0);
|
||||
if !self.playfield.can_active_piece_move_down() {
|
||||
self.update_lock_tick();
|
||||
}
|
||||
}
|
||||
fn move_right(&mut self) {
|
||||
self.playfield.move_offset(1, 0);
|
||||
if !self.playfield.can_active_piece_move_down() {
|
||||
self.update_lock_tick();
|
||||
}
|
||||
}
|
||||
fn move_down(&mut self) {
|
||||
if self.playfield.move_offset(0, 1) {
|
||||
self.score += 1;
|
||||
|
@ -222,14 +187,23 @@ impl Controllable for Game {
|
|||
if !self.try_lock_tetromino() {
|
||||
println!("couldn't lock tetromino despite hard dropping!");
|
||||
}
|
||||
|
||||
self.next_lock_tick = std::u64::MAX;
|
||||
}
|
||||
|
||||
fn hold(&mut self) {
|
||||
match self.playfield.try_swap_hold() {
|
||||
Ok(_) => {}
|
||||
Err(_) => (),
|
||||
}
|
||||
// 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;
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -86,6 +86,5 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
}
|
||||
|
||||
dbg!(game);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -134,7 +134,6 @@ 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) {
|
||||
|
@ -171,16 +170,19 @@ impl PlayField {
|
|||
self.field[*y as usize][*x as usize] = Some(active_color);
|
||||
}
|
||||
|
||||
self.active_piece = None;
|
||||
new_pieces
|
||||
}
|
||||
None => vec![],
|
||||
None => panic!("Tried to lock active piece while active piece doesn't exist!"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_active_piece_in_valid_position(&self) -> bool {
|
||||
match self.active_piece {
|
||||
Some(active_piece) => self.can_piece_be_at_position(&active_piece),
|
||||
None => true,
|
||||
Some(active_piece) => {
|
||||
self.can_piece_be_at_position(&active_piece)
|
||||
},
|
||||
None => panic!("Tried checking if active piece is in a valid position but active piece doesn't exist")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -194,37 +196,6 @@ 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(())
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
|
@ -45,6 +45,7 @@ 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() {
|
||||
|
|
Loading…
Reference in a new issue