spawn tetromino above game board, fix game over logic
This commit is contained in:
parent
bda06192ba
commit
6699ea683c
@ -52,6 +52,10 @@ impl Board {
|
||||
let x = *x_offset as i8 + x;
|
||||
let y = *y_offset as i8 + y;
|
||||
|
||||
if x < 0 || x >= Board::WIDTH as i8 {
|
||||
return true;
|
||||
}
|
||||
|
||||
if y < 0 {
|
||||
continue;
|
||||
}
|
||||
@ -60,10 +64,6 @@ impl Board {
|
||||
return true;
|
||||
}
|
||||
|
||||
if x < 0 || x >= Board::WIDTH as i8 {
|
||||
return true;
|
||||
}
|
||||
|
||||
if self.0[y as usize][x as usize].is_some() {
|
||||
return true;
|
||||
}
|
||||
|
21
src/game.rs
21
src/game.rs
@ -2,6 +2,7 @@ use crate::actions::{Action, ActionsHeld};
|
||||
use crate::board::Board;
|
||||
use crate::tetromino::{Direction, DirectionDiff, Tetromino};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CurrentTetromino {
|
||||
pub tetromino: Tetromino,
|
||||
pub direction: Direction,
|
||||
@ -18,12 +19,21 @@ impl CurrentTetromino {
|
||||
.max()
|
||||
.expect("pattern length > 0")
|
||||
+ 1;
|
||||
|
||||
let height = tetromino
|
||||
.pattern(&Direction::Up)
|
||||
.into_iter()
|
||||
.map(|(_x, y)| y)
|
||||
.max()
|
||||
.expect("pattern length > 0")
|
||||
+ 1;
|
||||
|
||||
let x = ((Board::WIDTH - width) / 2) as i8;
|
||||
Self {
|
||||
tetromino,
|
||||
direction: Direction::Up,
|
||||
x,
|
||||
y: -1,
|
||||
y: -(height as i8),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -259,7 +269,14 @@ impl Game {
|
||||
let current = std::mem::replace(&mut self.current_tetromino, next);
|
||||
let pattern = current.tetromino.pattern(¤t.direction);
|
||||
|
||||
if current.y <= 0 {
|
||||
let y_start = pattern
|
||||
.iter()
|
||||
.map(|(_x, y)| *y)
|
||||
.min()
|
||||
.expect("pattern length > 0") as i8;
|
||||
|
||||
let y = current.y + y_start;
|
||||
if y < 0 {
|
||||
self.game_over = true;
|
||||
}
|
||||
|
||||
|
@ -301,18 +301,20 @@ fn config_from_file<P: AsRef<std::path::Path>>(path: P) -> Result<Config, String
|
||||
let Some(config) = fs::read_to_string(path.as_ref()).ok() else {
|
||||
let config = Config::default();
|
||||
{
|
||||
println!("could not get config! attempting to create default...");
|
||||
println!("could not get config! creating default...");
|
||||
let config = toml::to_string(&config).map_err(|err| err.to_string())?;
|
||||
fs::write(path, config).map_err(|err| err.to_string())?;
|
||||
fs::write(path.as_ref(), config).map_err(|err| err.to_string())?;
|
||||
println!("created config at '{}'", path.as_ref().display());
|
||||
}
|
||||
return Ok(config);
|
||||
};
|
||||
let Some(config) = toml::from_str(&config).ok() else {
|
||||
println!("womp womp, config contains an invalid config, attempting to reset to default...");
|
||||
println!("womp womp, config contains an invalid config, resetting...");
|
||||
let config = Config::default();
|
||||
{
|
||||
let config = toml::to_string(&config).map_err(|err| err.to_string())?;
|
||||
fs::write(path, config).map_err(|err| err.to_string())?;
|
||||
fs::write(path.as_ref(), config).map_err(|err| err.to_string())?;
|
||||
println!("created config at '{}'", path.as_ref().display());
|
||||
}
|
||||
return Ok(config);
|
||||
};
|
||||
|
@ -9,6 +9,7 @@ pub enum Tetromino {
|
||||
Z,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Direction {
|
||||
Up,
|
||||
Right,
|
||||
|
Loading…
Reference in New Issue
Block a user