Skip to content

Commit

Permalink
Merge pull request #53 from Brian-Catcow-B/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Brian-Catcow-B authored Mar 16, 2021
2 parents eaf54e8 + 6c60a84 commit 64d404e
Show file tree
Hide file tree
Showing 4 changed files with 304 additions and 25 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ Gamepads auto-map to the first player that doesn't have a GamepadId mapping to i
#########
Renamed project: Rustrisn-t -> Tetrisn-t.
Improved menu slightly.
Various quality-of-life changes.
Various quality-of-life changes.

#########
# 0.1.1 #
#########
Added active piece tile highlights
Added line clear animation using tile highlights
Made it easier to quit to menu
106 changes: 98 additions & 8 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ pub struct Game {
tile_size: f32,
batch_empty_tile: spritebatch::SpriteBatch,
batch_highlight_active_tile: spritebatch::SpriteBatch,
batch_highlight_clearing_standard_tile: spritebatch::SpriteBatch,
batch_highlight_clearing_tetrisnt_tile: spritebatch::SpriteBatch,
vec_batch_player_piece: Vec<spritebatch::SpriteBatch>,
vec_batch_next_piece: Vec<spritebatch::SpriteBatch>,
game_info_text: Text,
Expand Down Expand Up @@ -250,7 +252,7 @@ impl Game {
.scale(Scale::uniform(LITTLE_TEXT_SCALE)),
);
let pause_text = Text::new(
TextFragment::new("PAUSED\n\nDown + RotateCw + RotateCcw + ESC/Start to quit")
TextFragment::new("PAUSED\n\nDown + ESC/Start to quit")
.color(graphics::WHITE)
.scale(Scale::uniform(LITTLE_TEXT_SCALE)),
);
Expand Down Expand Up @@ -279,6 +281,12 @@ impl Game {
batch_highlight_active_tile: spritebatch::SpriteBatch::new(
TileGraphic::new_active_highlight(ctx).image,
),
batch_highlight_clearing_standard_tile: spritebatch::SpriteBatch::new(
TileGraphic::new_clear_standard_highlight(ctx).image,
),
batch_highlight_clearing_tetrisnt_tile: spritebatch::SpriteBatch::new(
TileGraphic::new_clear_tetrisnt_highlight(ctx).image,
),
vec_batch_player_piece,
vec_batch_next_piece,
game_info_text,
Expand Down Expand Up @@ -312,11 +320,7 @@ impl Game {
} else {
for player in &mut self.vec_players {
// should we quit to main menu?
if player.input.keydown_down.0
&& player.input.keydown_rotate_ccw.0
&& player.input.keydown_rotate_cw.0
&& player.input.keydown_start.1
{
if player.input.keydown_down.0 && player.input.keydown_start.1 {
return ProgramState::Menu;
}
// should we resume?
Expand Down Expand Up @@ -671,6 +675,64 @@ impl Game {
}
}
}
// line clear highlights
for full_line in self.board.vec_full_lines.iter() {
if full_line.lines_cleared_together < 4 {
// standard clear animation
let y = (full_line.row - BOARD_HEIGHT_BUFFER_U) as usize;
let board_max_index_remainder_2 = (self.board.width - 1) % 2;
// go from the middle to the outside and reach the end right before full_line.clear_delay reaches 0
for x in (self.board.width / 2)..self.board.width {
let highlight_pos_right = graphics::DrawParam::new().dest(Point2::new(
x as f32 * NUM_PIXEL_ROWS_PER_TILEGRAPHIC as f32,
y as f32 * NUM_PIXEL_ROWS_PER_TILEGRAPHIC as f32,
));
let highlight_pos_left = graphics::DrawParam::new().dest(Point2::new(
(self.board.width as f32 - (x + board_max_index_remainder_2) as f32)
* NUM_PIXEL_ROWS_PER_TILEGRAPHIC as f32,
y as f32 * NUM_PIXEL_ROWS_PER_TILEGRAPHIC as f32,
));

self.batch_highlight_clearing_standard_tile
.add(highlight_pos_right);
self.batch_highlight_clearing_standard_tile
.add(highlight_pos_left);

if ((x as f32) / (self.board.width as f32) - 0.5) * 2.0
> 1.0 - (full_line.clear_delay as f32 / CLEAR_DELAY as f32)
{
break;
}
}
} else {
// tetrisnt clear animation
let y = (full_line.row - BOARD_HEIGHT_BUFFER_U) as usize;
let board_max_index_remainder_2 = (self.board.width - 1) % 2;
// go from the middle to the outside and reach the end right before full_line.clear_delay reaches 0
for x in (self.board.width / 2)..self.board.width {
let highlight_pos_right = graphics::DrawParam::new().dest(Point2::new(
x as f32 * NUM_PIXEL_ROWS_PER_TILEGRAPHIC as f32,
y as f32 * NUM_PIXEL_ROWS_PER_TILEGRAPHIC as f32,
));
let highlight_pos_left = graphics::DrawParam::new().dest(Point2::new(
(self.board.width as f32 - (x + board_max_index_remainder_2) as f32)
* NUM_PIXEL_ROWS_PER_TILEGRAPHIC as f32,
y as f32 * NUM_PIXEL_ROWS_PER_TILEGRAPHIC as f32,
));

self.batch_highlight_clearing_tetrisnt_tile
.add(highlight_pos_right);
self.batch_highlight_clearing_tetrisnt_tile
.add(highlight_pos_left);

if ((x as f32) / (self.board.width as f32) - 0.5) * 2.0
> 1.0 - (full_line.clear_delay as f32 / CLEAR_DELAY as f32)
{
break;
}
}
}
}
// next pieces
for player in &mut self.vec_players {
if player.redraw_next_piece_flag {
Expand Down Expand Up @@ -726,7 +788,7 @@ impl Game {
)
.unwrap();
}
// highlights
// active tile highlights
graphics::draw(
ctx,
&self.batch_highlight_active_tile,
Expand All @@ -738,6 +800,30 @@ impl Game {
.scale(Vector2::new(scaled_tile_size, scaled_tile_size)),
)
.unwrap();
// clearing tile standard highlights
graphics::draw(
ctx,
&self.batch_highlight_clearing_standard_tile,
DrawParam::new()
.dest(Point2::new(
board_top_left_corner,
NON_BOARD_SPACE_U as f32 * self.tile_size,
))
.scale(Vector2::new(scaled_tile_size, scaled_tile_size)),
)
.unwrap();
// clearing tile tetrisnt highlights
graphics::draw(
ctx,
&self.batch_highlight_clearing_tetrisnt_tile,
DrawParam::new()
.dest(Point2::new(
board_top_left_corner,
NON_BOARD_SPACE_U as f32 * self.tile_size,
))
.scale(Vector2::new(scaled_tile_size, scaled_tile_size)),
)
.unwrap();
// next piece tiles
for player in self.vec_players.iter() {
graphics::draw(
Expand Down Expand Up @@ -767,8 +853,12 @@ impl Game {
for player in 0..self.num_players {
self.vec_batch_player_piece[player as usize].clear();
}
// clear highlight sprite batch
// clear highlight active tile sprite batch
self.batch_highlight_active_tile.clear();
// clear highlight clearing tile standard sprite batch
self.batch_highlight_clearing_standard_tile.clear();
// clear highlight clearing tile tetrisnt sprite batch
self.batch_highlight_clearing_tetrisnt_tile.clear();
}
}

Expand Down
46 changes: 32 additions & 14 deletions src/game/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct Board {
pub height: u8,
pub matrix: Vec<Vec<Tile>>,
pub vec_active_piece: Vec<Piece>,
vec_full_lines: Vec<FullLine>,
pub vec_full_lines: Vec<FullLine>,
}

// example Board coordinates system (2 width, 2 height)
Expand All @@ -26,13 +26,23 @@ impl Board {
for _ in 0..num_players {
vec_active_piece.push(Piece::new(Shapes::None));
}
let matrix = vec![
vec![Tile::default(); board_width as usize];
(board_height + BOARD_HEIGHT_BUFFER_U) as usize
];

// DEBUG TILES ADDED
// let mut matrix = vec![vec![Tile::new_empty(); board_width as usize]; (board_height + BOARD_HEIGHT_BUFFER_U) as usize];
// for x in 0..(board_width - 1) {
// for y in (board_height + BOARD_HEIGHT_BUFFER_U - 8)..(board_height + BOARD_HEIGHT_BUFFER_U) {
// matrix[y as usize][x as usize] = Tile::new(false, false, 0u8);
// }
// }

Self {
width: board_width,
height: board_height,
matrix: vec![
vec![Tile::new_empty(); board_width as usize];
(board_height + BOARD_HEIGHT_BUFFER_U) as usize
],
matrix,
vec_active_piece,
vec_full_lines: vec![],
}
Expand All @@ -45,7 +55,7 @@ impl Board {
.take(4)
{
if position != &(0xffu8, 0xffu8) {
self.matrix[position.0 as usize][position.1 as usize] = Tile::new_empty();
self.matrix[position.0 as usize][position.1 as usize] = Tile::default();
} else {
println!("[!] tried to emptify piece that contained position (0xffu8, 0xffu8)");
}
Expand Down Expand Up @@ -123,18 +133,24 @@ impl Board {
if movement == Movement::Down && self.should_lock(player) {
// lock piece and push any full lines to vec_full_lines
self.vec_active_piece[player as usize].shape = Shapes::None;
let mut is_full_line = false;
let mut full_line_rows: Vec<u8> = Vec::with_capacity(4);
for row in &self.lock_piece(player) {
if self.is_row_full(*row) {
is_full_line = true;
self.vec_full_lines.push(FullLine::new(*row, player));
full_line_rows.push(*row);
}
}
if is_full_line {
if !full_line_rows.is_empty() {
for row in full_line_rows.iter() {
self.vec_full_lines.push(FullLine::new(
*row,
full_line_rows.len() as u8,
player,
));
}
self.vec_full_lines.sort();
}

return (false, is_full_line);
return (false, !full_line_rows.is_empty());
}

return (false, false);
Expand Down Expand Up @@ -314,7 +330,7 @@ impl Board {
self.matrix
.remove(self.vec_full_lines[index - indices_destroyed].row as usize);
self.matrix
.insert(0, vec![Tile::new_empty(); self.width as usize]);
.insert(0, vec![Tile::default(); self.width as usize]);
self.vec_full_lines.remove(index - indices_destroyed);
indices_destroyed += 1;
// now is when we step backwards through the self.vec_full_lines vector,
Expand Down Expand Up @@ -342,17 +358,19 @@ impl Board {
}

#[derive(Ord, Eq, PartialOrd, PartialEq)]
struct FullLine {
pub struct FullLine {
pub row: u8,
pub lines_cleared_together: u8,
pub player: u8,
pub clear_delay: i8,
pub remove_flag: bool,
}

impl FullLine {
pub fn new(row: u8, player: u8) -> Self {
pub fn new(row: u8, lines_cleared_together: u8, player: u8) -> Self {
Self {
row,
lines_cleared_together,
player,
clear_delay: CLEAR_DELAY,
remove_flag: false,
Expand Down
Loading

0 comments on commit 64d404e

Please sign in to comment.