Compare commits
3 Commits
3ef61c0e89
...
6699ea683c
Author | SHA1 | Date | |
---|---|---|---|
6699ea683c | |||
bda06192ba | |||
2d644f34f6 |
@ -52,6 +52,10 @@ impl Board {
|
|||||||
let x = *x_offset as i8 + x;
|
let x = *x_offset as i8 + x;
|
||||||
let y = *y_offset as i8 + y;
|
let y = *y_offset as i8 + y;
|
||||||
|
|
||||||
|
if x < 0 || x >= Board::WIDTH as i8 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if y < 0 {
|
if y < 0 {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -60,10 +64,6 @@ impl Board {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if x < 0 || x >= Board::WIDTH as i8 {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.0[y as usize][x as usize].is_some() {
|
if self.0[y as usize][x as usize].is_some() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
32
src/game.rs
32
src/game.rs
@ -2,6 +2,7 @@ use crate::actions::{Action, ActionsHeld};
|
|||||||
use crate::board::Board;
|
use crate::board::Board;
|
||||||
use crate::tetromino::{Direction, DirectionDiff, Tetromino};
|
use crate::tetromino::{Direction, DirectionDiff, Tetromino};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct CurrentTetromino {
|
pub struct CurrentTetromino {
|
||||||
pub tetromino: Tetromino,
|
pub tetromino: Tetromino,
|
||||||
pub direction: Direction,
|
pub direction: Direction,
|
||||||
@ -11,12 +12,28 @@ pub struct CurrentTetromino {
|
|||||||
|
|
||||||
impl CurrentTetromino {
|
impl CurrentTetromino {
|
||||||
fn new(tetromino: Tetromino) -> Self {
|
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 {
|
Self {
|
||||||
tetromino,
|
tetromino,
|
||||||
direction: Direction::Up,
|
direction: Direction::Up,
|
||||||
x: (Board::WIDTH as i8 - PIECE_WIDTH) / 2,
|
x,
|
||||||
y: -1,
|
y: -(height as i8),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -252,7 +269,14 @@ impl Game {
|
|||||||
let current = std::mem::replace(&mut self.current_tetromino, next);
|
let current = std::mem::replace(&mut self.current_tetromino, next);
|
||||||
let pattern = current.tetromino.pattern(¤t.direction);
|
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;
|
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 Some(config) = fs::read_to_string(path.as_ref()).ok() else {
|
||||||
let config = Config::default();
|
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())?;
|
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);
|
return Ok(config);
|
||||||
};
|
};
|
||||||
let Some(config) = toml::from_str(&config).ok() else {
|
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 = Config::default();
|
||||||
{
|
{
|
||||||
let config = toml::to_string(&config).map_err(|err| err.to_string())?;
|
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);
|
return Ok(config);
|
||||||
};
|
};
|
||||||
|
@ -9,6 +9,7 @@ pub enum Tetromino {
|
|||||||
Z,
|
Z,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum Direction {
|
pub enum Direction {
|
||||||
Up,
|
Up,
|
||||||
Right,
|
Right,
|
||||||
|
Loading…
Reference in New Issue
Block a user