Compare commits

...

3 Commits

4 changed files with 39 additions and 12 deletions

View File

@ -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;
}

View File

@ -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,
@ -11,12 +12,28 @@ pub struct CurrentTetromino {
impl CurrentTetromino {
fn new(tetromino: Tetromino) -> Self {
const PIECE_WIDTH: i8 = 2;
let width = tetromino
.pattern(&Direction::Up)
.into_iter()
.map(|(x, _y)| x)
.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: (Board::WIDTH as i8 - PIECE_WIDTH) / 2,
y: -1,
x,
y: -(height as i8),
}
}
}
@ -252,7 +269,14 @@ impl Game {
let current = std::mem::replace(&mut self.current_tetromino, next);
let pattern = current.tetromino.pattern(&current.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;
}

View File

@ -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);
};

View File

@ -9,6 +9,7 @@ pub enum Tetromino {
Z,
}
#[derive(Debug)]
pub enum Direction {
Up,
Right,