Skip to content

Commit

Permalink
Code refinement.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramirisu committed Sep 24, 2024
1 parent bcca211 commit 730d829
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 97 deletions.
45 changes: 21 additions & 24 deletions src/game/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Board {
randomizer: PieceRandomizer,
squares: Vec<Vec<Piece>>,
curr_piece: Piece,
curr_translation: (i32, i32),
curr_pos: (i32, i32),
next_piece: Piece,
lines: usize,
score: usize,
Expand Down Expand Up @@ -40,7 +40,7 @@ impl Board {
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),
curr_pos: (Self::BOARD_PIECE_START_X, Self::BOARD_PIECE_START_Y),
next_piece: next_piece,
lines: 0,
score: 0,
Expand Down Expand Up @@ -114,15 +114,15 @@ impl Board {
self.squares[y as usize][x as usize]
}

pub fn get_line_clear_indexes(&self) -> Vec<usize> {
let mut indexes = vec![];
for index in 0..Self::BOARD_ROWS {
if self.squares[index].iter().all(|sqr| !sqr.is_placeholder()) {
indexes.push(index);
pub fn get_line_clear_rows(&self) -> Vec<usize> {
let mut rows = vec![];
for row in 0..Self::BOARD_ROWS {
if self.squares[row].iter().all(|sqr| !sqr.is_placeholder()) {
rows.push(row);
}
}

indexes
rows
}

pub fn get_piece_count(&self, piece: Piece) -> usize {
Expand All @@ -136,14 +136,14 @@ impl Board {
}

pub fn clear_lines(&mut self) {
let indexes = self.get_line_clear_indexes();
indexes.iter().rev().for_each(|index| {
self.squares.remove(*index);
let rows = self.get_line_clear_rows();
rows.iter().rev().for_each(|row| {
self.squares.remove(*row);
});

self.score += get_score(indexes.len(), self.level());
self.lines += indexes.len();
match indexes.len() {
self.score += get_score(rows.len(), self.level());
self.lines += rows.len();
match rows.len() {
1 => self.single += 1,
2 => self.double += 1,
3 => self.triple += 1,
Expand All @@ -159,7 +159,7 @@ impl Board {
&mut self.next_piece,
self.randomizer.gen_1h2r(self.curr_piece),
);
self.curr_translation = (Self::BOARD_PIECE_START_X, Self::BOARD_PIECE_START_Y);
self.curr_pos = (Self::BOARD_PIECE_START_X, Self::BOARD_PIECE_START_Y);
match self.curr_piece {
Piece::I(_) => self.drought = 0,
_ => {
Expand All @@ -175,12 +175,9 @@ impl Board {
}

pub fn get_curr_piece_squares(&self) -> [Square; 4] {
self.curr_piece.get_squares().map(|sqr| {
Square(
sqr.0 + self.curr_translation.0,
sqr.1 + self.curr_translation.1,
)
})
self.curr_piece
.get_squares()
.map(|sqr| Square(sqr.0 + self.curr_pos.0, sqr.1 + self.curr_pos.1))
}

pub fn get_next_piece(&self) -> Piece {
Expand Down Expand Up @@ -220,7 +217,7 @@ impl Board {
});

if movable {
self.curr_translation.1 -= 1;
self.curr_pos.1 -= 1;
}

movable
Expand All @@ -229,7 +226,7 @@ impl Board {
pub fn move_piece_left(&mut self) -> bool {
let movable = self.is_left_movable();
if movable {
self.curr_translation.0 -= 1;
self.curr_pos.0 -= 1;
}

movable
Expand All @@ -238,7 +235,7 @@ impl Board {
pub fn move_piece_right(&mut self) -> bool {
let movable = self.is_right_movable();
if movable {
self.curr_translation.0 += 1;
self.curr_pos.0 += 1;
}

movable
Expand Down
34 changes: 20 additions & 14 deletions src/game/piece.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,20 +293,10 @@ impl Piece {
}
}

pub fn get_center_offset(&self) -> (f32, f32) {
let mut maxx = 0;
let mut minx = 0;
let mut maxy = 0;
let mut miny = 0;

self.get_squares().iter().for_each(|sqr| {
maxx = maxx.max(sqr.0);
minx = minx.min(sqr.0);
maxy = maxy.max(sqr.1);
miny = miny.min(sqr.1);
});

((maxx + minx) as f32 / -2.0, (maxy + miny) as f32 / -2.0)
pub fn get_squares_with_piece_center_align(&self) -> [(f32, f32); 4] {
let offset = self.get_piece_center_align_offset();
self.get_squares()
.map(|sqr| (sqr.0 as f32 + offset.0, sqr.1 as f32 + offset.1))
}

pub fn rotate_clockwise(&mut self) {
Expand Down Expand Up @@ -348,6 +338,22 @@ impl Piece {
];
PIECES.iter()
}

fn get_piece_center_align_offset(&self) -> (f32, f32) {
let mut maxx = 0;
let mut minx = 0;
let mut maxy = 0;
let mut miny = 0;

self.get_squares().iter().for_each(|sqr| {
maxx = maxx.max(sqr.0);
minx = minx.min(sqr.0);
maxy = maxy.max(sqr.1);
miny = miny.min(sqr.1);
});

((maxx + minx) as f32 / -2.0, (maxy + miny) as f32 / -2.0)
}
}

impl From<usize> for Piece {
Expand Down
92 changes: 46 additions & 46 deletions src/game/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,43 +360,46 @@ fn setup_screen(
Piece::iter()
.filter(|piece| **piece != Piece::X)
.for_each(|piece| {
piece.get_squares().iter().for_each(|square| {
commands.spawn((
SpriteBundle {
transform: Transform::from_translation(
game_transform.piece_count_translation(
piece.variant_index(),
square.0,
square.1,
piece.get_center_offset(),
piece
.get_squares_with_piece_center_align()
.iter()
.for_each(|square| {
commands.spawn((
SpriteBundle {
transform: Transform::from_translation(
game_transform.piece_count_translation(
piece.variant_index(),
square.0,
square.1,
),
),
),
sprite: Sprite {
custom_size: Some(game_transform.piece_count_square_size()),
sprite: Sprite {
custom_size: Some(game_transform.piece_count_square_size()),
..default()
},
texture: square_image_assets.get_image(SquareImageSize::Small, *piece),
..default()
},
texture: square_image_assets.get_image(SquareImageSize::Small, *piece),
..default()
},
GameEntityMarker,
PieceCountEntityMarker(*piece),
));
commands.spawn((
Text2dBundle {
text: Text::from_sections([TextSection::from_style(TextStyle {
font_size: game_transform.scale() * 36.0,
color: WHITE.into(),
GameEntityMarker,
PieceCountEntityMarker(*piece),
));
commands.spawn((
Text2dBundle {
text: Text::from_sections([TextSection::from_style(TextStyle {
font_size: game_transform.scale() * 36.0,
color: WHITE.into(),
..default()
})]),
transform: Transform::from_translation(
game_transform
.piece_count_counter_translation(piece.variant_index()),
),
..default()
})]),
transform: Transform::from_translation(
game_transform.piece_count_counter_translation(piece.variant_index()),
),
..default()
},
GameEntityMarker,
PieceCountCounterEntityMarker(*piece),
));
});
},
GameEntityMarker,
PieceCountCounterEntityMarker(*piece),
));
});
});

player_data
Expand Down Expand Up @@ -452,16 +455,14 @@ fn setup_screen(
player_data
.board
.get_next_piece()
.get_squares()
.get_squares_with_piece_center_align()
.iter()
.for_each(|sqr| {
commands.spawn((
SpriteBundle {
transform: Transform::from_translation(game_transform.next_piece_translation(
sqr.0,
sqr.1,
player_data.board.get_next_piece().get_center_offset(),
)),
transform: Transform::from_translation(
game_transform.next_piece_translation(sqr.0, sqr.1),
),
sprite: Sprite {
custom_size: Some(game_transform.square_size()),
..default()
Expand Down Expand Up @@ -732,7 +733,7 @@ mod state_game_running {
);
});

let lines = player_data.board.get_line_clear_indexes();
let lines = player_data.board.get_line_clear_rows();
match lines.len() {
0 => {
e_play_sound.send(PlaySoundEvent::LockCurrPiece);
Expand Down Expand Up @@ -838,16 +839,15 @@ mod state_game_entry_delay {
});
std::iter::zip(
query.p2().iter_mut(),
player_data.board.get_next_piece().get_squares(),
player_data
.board
.get_next_piece()
.get_squares_with_piece_center_align(),
)
.for_each(|((mut transform, mut image), sqr)| {
*image = square_image_assets
.get_image(SquareImageSize::Normal, player_data.board.get_next_piece());
transform.translation = game_transform.next_piece_translation(
sqr.0,
sqr.1,
player_data.board.get_next_piece().get_center_offset(),
);
transform.translation = game_transform.next_piece_translation(sqr.0, sqr.1);
});
query.p3().iter_mut().for_each(|(mut image, piece)| {
*image = square_image_assets.get_image(SquareImageSize::Small, piece.0);
Expand Down
18 changes: 5 additions & 13 deletions src/game/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,9 @@ impl GameTransform {
Vec2::new(self.board_width() * 0.9, 0.0)
}

pub fn next_piece_translation(&self, x: i32, y: i32, offset: (f32, f32)) -> Vec3 {
(Vec2::new(
(x as f32 + offset.0) * self.square_width(),
(y as f32 + offset.1) * self.square_height(),
) + self.next_piece_translation_offset())
pub fn next_piece_translation(&self, x: f32, y: f32) -> Vec3 {
(Vec2::new(x * self.square_width(), y * self.square_height())
+ self.next_piece_translation_offset())
.extend(CURR_PIECE_LAYER)
}

Expand Down Expand Up @@ -147,14 +145,8 @@ impl GameTransform {
Vec2::new(self.square_width() * 0.5, self.square_height() * 0.5)
}

pub fn piece_count_translation(
&self,
index: usize,
x: i32,
y: i32,
offset: (f32, f32),
) -> Vec3 {
(Vec2::new(x as f32 + 0.5 + offset.0, y as f32 + offset.1) * self.piece_count_square_size()
pub fn piece_count_translation(&self, index: usize, x: f32, y: f32) -> Vec3 {
(Vec2::new(x + 0.5, y) * self.piece_count_square_size()
+ Vec2::new(
-self.board_width() - self.square_width() * 2.0,
-self.square_height() * 2.0 - self.square_height() * 1.5 * index as f32,
Expand Down

0 comments on commit 730d829

Please sign in to comment.