Skip to content

Commit

Permalink
Make next piece and piece count centering.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramirisu committed Sep 23, 2024
1 parent 7856ea5 commit e86584b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/game/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl Board {
}

pub fn get_curr_piece_squares(&self) -> [Square; 4] {
self.curr_piece.to_squares().map(|sqr| {
self.curr_piece.get_squares().map(|sqr| {
Square(
sqr.0 + self.curr_translation.0,
sqr.1 + self.curr_translation.1,
Expand Down
46 changes: 31 additions & 15 deletions src/game/piece.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum PieceT {
}

impl PieceT {
pub fn to_squares(&self) -> [Square; 4] {
pub fn get_squares(&self) -> [Square; 4] {
match self {
PieceT::T0 => [Square(0, -1), Square(-1, 0), Square(0, 0), Square(1, 0)],
PieceT::T1 => [Square(0, -1), Square(-1, 0), Square(0, 0), Square(0, 1)],
Expand Down Expand Up @@ -53,7 +53,7 @@ pub enum PieceJ {
}

impl PieceJ {
pub fn to_squares(&self) -> [Square; 4] {
pub fn get_squares(&self) -> [Square; 4] {
match self {
PieceJ::J0 => [Square(-1, 0), Square(0, 0), Square(1, 0), Square(1, -1)],
PieceJ::J1 => [Square(-1, -1), Square(0, -1), Square(0, 0), Square(0, 1)],
Expand Down Expand Up @@ -85,7 +85,7 @@ pub enum PieceZ {
}

impl PieceZ {
pub fn to_squares(&self) -> [Square; 4] {
pub fn get_squares(&self) -> [Square; 4] {
match self {
PieceZ::Z0 => [Square(0, -1), Square(1, -1), Square(-1, 0), Square(0, 0)],
PieceZ::Z1 => [Square(0, -1), Square(0, 0), Square(1, 0), Square(1, 1)],
Expand Down Expand Up @@ -114,7 +114,7 @@ pub enum PieceO {
}

impl PieceO {
pub fn to_squares(&self) -> [Square; 4] {
pub fn get_squares(&self) -> [Square; 4] {
[Square(-1, -1), Square(0, -1), Square(-1, 0), Square(0, 0)]
}

Expand All @@ -131,7 +131,7 @@ pub enum PieceS {
}

impl PieceS {
pub fn to_squares(&self) -> [Square; 4] {
pub fn get_squares(&self) -> [Square; 4] {
match self {
PieceS::S0 => [Square(-1, -1), Square(0, -1), Square(0, 0), Square(1, 0)],
PieceS::S1 => [Square(1, -1), Square(0, 0), Square(1, 0), Square(0, 1)],
Expand Down Expand Up @@ -163,7 +163,7 @@ pub enum PieceL {
}

impl PieceL {
pub fn to_squares(&self) -> [Square; 4] {
pub fn get_squares(&self) -> [Square; 4] {
match self {
PieceL::L0 => [Square(-1, -1), Square(-1, 0), Square(0, 0), Square(1, 0)],
PieceL::L1 => [Square(0, -1), Square(0, 0), Square(-1, 1), Square(0, 1)],
Expand Down Expand Up @@ -195,7 +195,7 @@ pub enum PieceI {
}

impl PieceI {
pub fn to_squares(&self) -> [Square; 4] {
pub fn get_squares(&self) -> [Square; 4] {
match self {
PieceI::I0 => [Square(-2, 0), Square(-1, 0), Square(0, 0), Square(1, 0)],
PieceI::I1 => [Square(0, -1), Square(0, 0), Square(0, 1), Square(0, 2)],
Expand Down Expand Up @@ -280,19 +280,35 @@ impl Piece {
}
}

pub fn to_squares(&self) -> [Square; 4] {
pub fn get_squares(&self) -> [Square; 4] {
match self {
Piece::T(piece) => piece.to_squares(),
Piece::J(piece) => piece.to_squares(),
Piece::Z(piece) => piece.to_squares(),
Piece::O(piece) => piece.to_squares(),
Piece::S(piece) => piece.to_squares(),
Piece::L(piece) => piece.to_squares(),
Piece::I(piece) => piece.to_squares(),
Piece::T(piece) => piece.get_squares(),
Piece::J(piece) => piece.get_squares(),
Piece::Z(piece) => piece.get_squares(),
Piece::O(piece) => piece.get_squares(),
Piece::S(piece) => piece.get_squares(),
Piece::L(piece) => piece.get_squares(),
Piece::I(piece) => piece.get_squares(),
Piece::X => panic!("Piece::X is a placeholder"),
}
}

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 rotate_clockwise(&mut self) {
match self {
Piece::T(piece) => piece.rotate_clockwise(),
Expand Down
22 changes: 15 additions & 7 deletions src/game/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,15 @@ fn setup_screen(
Piece::iter()
.filter(|piece| **piece != Piece::X)
.for_each(|piece| {
piece.to_squares().iter().for_each(|square| {
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(),
),
),
sprite: Sprite {
Expand Down Expand Up @@ -439,17 +440,20 @@ fn setup_screen(
},
GameEntityMarker,
));

player_data
.board
.get_next_piece()
.to_squares()
.get_squares()
.iter()
.for_each(|sqr| {
commands.spawn((
SpriteBundle {
transform: Transform::from_translation(
game_transform.next_piece_translation(sqr.0, sqr.1),
),
transform: Transform::from_translation(game_transform.next_piece_translation(
sqr.0,
sqr.1,
player_data.board.get_next_piece().get_center_offset(),
)),
sprite: Sprite {
custom_size: Some(game_transform.square_size()),
..default()
Expand Down Expand Up @@ -826,12 +830,16 @@ mod state_game_entry_delay {
});
std::iter::zip(
query.p2().iter_mut(),
player_data.board.get_next_piece().to_squares(),
player_data.board.get_next_piece().get_squares(),
)
.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);
transform.translation = game_transform.next_piece_translation(
sqr.0,
sqr.1,
player_data.board.get_next_piece().get_center_offset(),
);
});
query.p3().iter_mut().for_each(|(mut image, piece)| {
*image = square_image_assets.get_image(SquareImageSize::Small, piece.0);
Expand Down
31 changes: 21 additions & 10 deletions src/game/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,33 @@ impl GameTransform {
Vec3::new(self.board_width(), -self.square_height() * 6.0, BOARD_LAYER)
}

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

pub fn next_piece_slot_translation(&self) -> Vec3 {
Vec3::new(self.board_width(), 0.0, BOARD_LAYER)
self.next_piece_translation_offset().extend(BOARD_LAYER)
}

pub fn next_piece_slot_size(&self) -> Vec2 {
Vec2::new(self.square_width() * 6.0, self.square_height() * 6.0)
Vec2::new(self.square_width() * 5.0, self.square_height() * 5.0)
}

pub fn next_piece_slot_background_translation(&self) -> Vec3 {
Vec3::new(self.board_width(), 0.0, BOARD_BACKGROUND_LAYER)
self.next_piece_translation_offset()
.extend(BOARD_BACKGROUND_LAYER)
}

pub fn next_piece_slot_background_size(&self) -> Vec2 {
Vec2::new(self.square_width() * 6.1, self.square_height() * 6.1)
Vec2::new(self.square_width() * 5.1, self.square_height() * 5.1)
}

pub fn statistics_translation(&self) -> Vec3 {
Expand All @@ -138,8 +143,14 @@ 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) -> Vec3 {
(Vec2::new(x as f32 + 0.5, y as f32) * self.piece_count_square_size()
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()
+ 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 e86584b

Please sign in to comment.