Skip to content

Commit

Permalink
Rewrite Piece.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramirisu committed Sep 23, 2024
1 parent 348f5d6 commit 7856ea5
Show file tree
Hide file tree
Showing 9 changed files with 481 additions and 253 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ A Classic Tetris (NES Tetris) clone written in BEVY/RUST.

## Keybindings

| Menu | In Game | Keyboard | Controller: Mapping A | Controller: Mapping B |
| :--------- | :------------------------ | :------: | :--------------------: | :--------------------: |
| Move Up | ||||
| Move Down | Soft Drop ||||
| Move Left | Move Left ||||
| Move Right | Move Right ||||
| | Rotate Clockwisely | X | A (→) | B (↓) |
| Back | Rotate Counterclockwisely | Z | B (↓) | Y (←) |
| Start | Pause/Resume | Enter | Start | Start |
| Soft Reset | Soft Reset | Esc | Select + Start + A + B | Select + Start + B + Y |
| Menu | In Game | Keyboard | Controller: Mapping A | Controller: Mapping B |
| :--------- | :---------------------- | :------: | :--------------------: | :--------------------: |
| Move Up | ||||
| Move Down | Soft Drop ||||
| Move Left | Move Left ||||
| Move Right | Move Right ||||
| | Rotate Clockwise | X | A (→) | B (↓) |
| Back | Rotate Counterclockwise | Z | B (↓) | Y (←) |
| Start | Pause/Resume | Enter | Start | Start |
| Soft Reset | Soft Reset | Esc | Select + Start + A + B | Select + Start + B + Y |

> A, B, X and Y button mapping is in NES/SNES/NSwitch Controller layout.
Expand Down
20 changes: 10 additions & 10 deletions src/game/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy::prelude::*;

use super::{
palette::{get_square_image, SquareImageSize},
piece::PieceShape,
piece::Piece,
};

#[derive(Resource)]
Expand All @@ -14,23 +14,23 @@ pub struct SquareImageAssets {
impl SquareImageAssets {
pub fn new(image_assets: &mut Assets<Image>, level: usize) -> Self {
Self {
normal: PieceShape::iter()
.map(|shape| {
image_assets.add(get_square_image(SquareImageSize::Normal, *shape, level))
normal: Piece::iter()
.map(|piece| {
image_assets.add(get_square_image(SquareImageSize::Normal, *piece, level))
})
.collect(),
small: PieceShape::iter()
.map(|shape| {
image_assets.add(get_square_image(SquareImageSize::Small, *shape, level))
small: Piece::iter()
.map(|piece| {
image_assets.add(get_square_image(SquareImageSize::Small, *piece, level))
})
.collect(),
}
}

pub fn get_image(&self, size: SquareImageSize, shape: PieceShape) -> Handle<Image> {
pub fn get_image(&self, size: SquareImageSize, piece: Piece) -> Handle<Image> {
match size {
SquareImageSize::Normal => self.normal[shape as usize].clone(),
SquareImageSize::Small => self.small[shape as usize].clone(),
SquareImageSize::Normal => self.normal[piece.variant_index()].clone(),
SquareImageSize::Small => self.small[piece.variant_index()].clone(),
}
}
}
77 changes: 39 additions & 38 deletions src/game/board.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use super::{
piece::{Piece, PieceShape, Square},
piece::{Piece, Square},
rand::PieceRandomizer,
score::get_score,
transition::Transition,
};

pub struct Board {
start_level: usize,
transition: Transition,
squares: Vec<Vec<PieceShape>>,
randomizer: PieceRandomizer,
squares: Vec<Vec<Piece>>,
curr_piece: Piece,
curr_translation: (i32, i32),
next_piece: Piece,
Expand All @@ -19,7 +21,7 @@ pub struct Board {
tetris: usize,
drought: usize,
max_drought: usize,
piece_count: [usize; PieceShape::variant_size()],
piece_count: [usize; Piece::variant_len()],
}

impl Board {
Expand All @@ -30,13 +32,16 @@ impl Board {
const BOARD_PIECE_START_Y: i32 = (Self::BOARD_ROWS - 1) as i32;

pub fn new(start_level: usize, transition: Transition) -> Self {
let randomizer = PieceRandomizer::System;
let next_piece = randomizer.gen();
let mut board = Self {
start_level,
transition,
squares: vec![vec![PieceShape::default(); Self::BOARD_COLS]; Self::BOARD_ROWS],
curr_piece: Piece::rand(),
randomizer,
squares: vec![vec![Piece::default(); Self::BOARD_COLS]; Self::BOARD_ROWS],
curr_piece: Piece::X,
curr_translation: (Self::BOARD_PIECE_START_X, Self::BOARD_PIECE_START_Y),
next_piece: Piece::rand(),
next_piece: next_piece,
lines: 0,
score: 0,
single: 0,
Expand All @@ -45,7 +50,7 @@ impl Board {
tetris: 0,
drought: 0,
max_drought: 0,
piece_count: [0; PieceShape::variant_size()],
piece_count: [0; Piece::variant_len()],
};

// auto apply `drought` and `rand_1h2r`
Expand Down Expand Up @@ -105,7 +110,7 @@ impl Board {
self.max_drought
}

pub fn get_square(&self, x: i32, y: i32) -> PieceShape {
pub fn get_square(&self, x: i32, y: i32) -> Piece {
self.squares[y as usize][x as usize]
}

Expand All @@ -120,13 +125,13 @@ impl Board {
indexes
}

pub fn get_piece_count(&self, shape: PieceShape) -> usize {
self.piece_count[shape as usize]
pub fn get_piece_count(&self, piece: Piece) -> usize {
self.piece_count[piece.variant_index()]
}

pub fn lock_curr_piece(&mut self) {
for sqr in self.get_curr_piece_squares() {
self.squares[sqr.1 as usize][sqr.0 as usize] = self.curr_piece.shape();
self.squares[sqr.1 as usize][sqr.0 as usize] = self.curr_piece;
}
}

Expand All @@ -145,22 +150,24 @@ impl Board {
4 => self.tetris += 1,
_ => (),
}
self.squares.resize(
Self::BOARD_ROWS,
vec![PieceShape::default(); Self::BOARD_COLS],
);
self.squares
.resize(Self::BOARD_ROWS, vec![Piece::default(); Self::BOARD_COLS]);
}

pub fn switch_to_next_piece(&mut self) {
self.curr_piece = std::mem::replace(&mut self.next_piece, self.curr_piece.rand_1h2r());
self.curr_piece = std::mem::replace(
&mut self.next_piece,
self.randomizer.gen_1h2r(self.curr_piece),
);
self.curr_translation = (Self::BOARD_PIECE_START_X, Self::BOARD_PIECE_START_Y);
if self.curr_piece.shape() == PieceShape::I {
self.drought = 0;
} else {
self.drought += 1;
self.max_drought = self.max_drought.max(self.drought);
match self.curr_piece {
Piece::I(_) => self.drought = 0,
_ => {
self.drought += 1;
self.max_drought = self.max_drought.max(self.drought);
}
}
self.piece_count[self.curr_piece.shape() as usize] += 1;
self.piece_count[self.curr_piece.variant_index()] += 1;
}

pub fn get_curr_piece(&self) -> Piece {
Expand All @@ -182,26 +189,23 @@ impl Board {

pub fn is_left_movable(&self) -> bool {
self.get_curr_piece_squares().iter().all(|sqr| {
let x = sqr.0 - 1;
let y = sqr.1;
let (x, y) = sqr.to_coordinate(-1, 0);
Self::is_inside(x, y)
&& (y >= Self::BOARD_ROWS as i32 || self.get_square(x, y).is_placeholder())
})
}

pub fn is_right_movable(&self) -> bool {
self.get_curr_piece_squares().iter().all(|sqr| {
let x = sqr.0 + 1;
let y = sqr.1;
let (x, y) = sqr.to_coordinate(1, 0);
Self::is_inside(x, y)
&& (y >= Self::BOARD_ROWS as i32 || self.get_square(x, y).is_placeholder())
})
}

pub fn is_curr_position_valid(&self) -> bool {
self.get_curr_piece_squares().iter().all(|sqr| {
let x = sqr.0;
let y = sqr.1;
let (x, y) = sqr.to_coordinate(0, 0);
Self::is_inside(x, y)
&& y < Self::BOARD_ROWS as i32
&& self.get_square(x, y).is_placeholder()
Expand All @@ -210,8 +214,7 @@ impl Board {

pub fn move_piece_down(&mut self) -> bool {
let movable = self.get_curr_piece_squares().iter().all(|sqr| {
let x = sqr.0;
let y = sqr.1 - 1;
let (x, y) = sqr.to_coordinate(0, -1);
Self::is_inside(x, y)
&& (y >= Self::BOARD_ROWS as i32 || self.get_square(x, y).is_placeholder())
});
Expand Down Expand Up @@ -242,30 +245,28 @@ impl Board {
}

pub fn rotate_piece_clockwise(&mut self) -> bool {
self.curr_piece.next_state();
self.curr_piece.rotate_clockwise();
let rotatable = self.get_curr_piece_squares().iter().all(|sqr| {
let x = sqr.0;
let y = sqr.1;
let (x, y) = sqr.to_coordinate(0, 0);
Self::is_inside(x, y)
&& (y >= Self::BOARD_ROWS as i32 || self.get_square(x, y).is_placeholder())
});
if !rotatable {
self.curr_piece.prev_state();
self.curr_piece.rotate_counterclockwise();
}

rotatable
}

pub fn rotate_piece_counter_clockwise(&mut self) -> bool {
self.curr_piece.prev_state();
self.curr_piece.rotate_counterclockwise();
let rotatable = self.get_curr_piece_squares().iter().all(|sqr| {
let x = sqr.0;
let y = sqr.1;
let (x, y) = sqr.to_coordinate(0, 0);
Self::is_inside(x, y)
&& (y >= Self::BOARD_ROWS as i32 || self.get_square(x, y).is_placeholder())
});
if !rotatable {
self.curr_piece.next_state();
self.curr_piece.rotate_clockwise();
}

rotatable
Expand Down
1 change: 1 addition & 0 deletions src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod palette;
pub mod piece;
pub mod player;
pub mod plugin;
mod rand;
mod score;
mod tick;
mod timer;
Expand Down
22 changes: 11 additions & 11 deletions src/game/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ use bevy::{
};
use image::{DynamicImage, Rgb32FImage};

use super::{level::LEVEL_COUNT, piece::PieceShape};
use super::{level::LEVEL_COUNT, piece::Piece};

pub fn get_square_image(size: SquareImageSize, shape: PieceShape, level: usize) -> Image {
pub fn get_square_image(size: SquareImageSize, piece: Piece, level: usize) -> Image {
let palette = get_level_palette(level);
match shape {
PieceShape::T => SquareImage::new(size, SquarePattern::X, palette),
PieceShape::J => SquareImage::new(size, SquarePattern::Z, palette),
PieceShape::Z => SquareImage::new(size, SquarePattern::Y, palette),
PieceShape::O => SquareImage::new(size, SquarePattern::X, palette),
PieceShape::S => SquareImage::new(size, SquarePattern::Z, palette),
PieceShape::L => SquareImage::new(size, SquarePattern::Y, palette),
PieceShape::I => SquareImage::new(size, SquarePattern::X, palette),
PieceShape::X => SquareImage::new_empty(size),
match piece {
Piece::T(_) => SquareImage::new(size, SquarePattern::X, palette),
Piece::J(_) => SquareImage::new(size, SquarePattern::Z, palette),
Piece::Z(_) => SquareImage::new(size, SquarePattern::Y, palette),
Piece::O(_) => SquareImage::new(size, SquarePattern::X, palette),
Piece::S(_) => SquareImage::new(size, SquarePattern::Z, palette),
Piece::L(_) => SquareImage::new(size, SquarePattern::Y, palette),
Piece::I(_) => SquareImage::new(size, SquarePattern::X, palette),
Piece::X => SquareImage::new_empty(size),
}
.into()
}
Expand Down
Loading

0 comments on commit 7856ea5

Please sign in to comment.