From 381609c26121b9d5faecef5edfe12f8dc1f11f49 Mon Sep 17 00:00:00 2001 From: hheik <4469778+hheik@users.noreply.github.com> Date: Wed, 20 Aug 2025 15:13:42 +0300 Subject: [PATCH] Added piece type component --- src/game.rs | 2 ++ src/game/prefab.rs | 25 ++++++++++-------- src/game/systems/game_scene.rs | 47 +++++++++++++++++++++++++++------- src/game/tetris.rs | 4 +-- 4 files changed, 57 insertions(+), 21 deletions(-) diff --git a/src/game.rs b/src/game.rs index 0f4e911..cd2039c 100644 --- a/src/game.rs +++ b/src/game.rs @@ -21,6 +21,8 @@ pub fn init(app: &mut App) { .register_type::() .register_type::() .register_type::() + .register_type::() + .register_type::() .register_type::() .register_type::() .add_systems(Startup, systems::setup_game_scene) diff --git a/src/game/prefab.rs b/src/game/prefab.rs index f228d9f..fdd81dd 100644 --- a/src/game/prefab.rs +++ b/src/game/prefab.rs @@ -17,10 +17,12 @@ use super::{PIXEL_SCALE, grid::*, tetris::*}; )] pub struct DemoCamera2d; -#[derive(Clone, Copy, Debug, Reflect)] +#[derive(Component, Clone, Copy, Debug, Default, Reflect)] +#[reflect(Component)] pub enum PieceType { /// ...... /// .XXXX. + #[default] I, /// ..X.. /// .XXX. @@ -105,9 +107,11 @@ impl PieceType { } } -pub fn create_block(pos: Vector2I, color: Color) -> impl Bundle { +pub fn create_block(pos: impl Into, color: Color) -> impl Bundle { ( - GridTransform::from_xy(pos.x, pos.y), + GridTransform { + translation: pos.into(), + }, Block, create_block_sprite(color), ) @@ -127,17 +131,18 @@ pub fn create_block_sprite(color: Color) -> impl Bundle { ) } -pub fn create_piece(piece: PieceType) -> impl Bundle { - let pos_arr = piece.block_positions(); +pub fn create_piece(piece_type: PieceType) -> impl Bundle { + let pos_arr = piece_type.block_positions(); let mut pos_iter = pos_arr.iter(); ( - Name::from(piece.name()), + Name::from(piece_type.name()), Piece, + piece_type, children![ - create_block(*pos_iter.next().unwrap(), piece.color()), - create_block(*pos_iter.next().unwrap(), piece.color()), - create_block(*pos_iter.next().unwrap(), piece.color()), - create_block(*pos_iter.next().unwrap(), piece.color()), + create_block(*pos_iter.next().unwrap(), piece_type.color()), + create_block(*pos_iter.next().unwrap(), piece_type.color()), + create_block(*pos_iter.next().unwrap(), piece_type.color()), + create_block(*pos_iter.next().unwrap(), piece_type.color()), ], ) } diff --git a/src/game/systems/game_scene.rs b/src/game/systems/game_scene.rs index bbed285..0b527bb 100644 --- a/src/game/systems/game_scene.rs +++ b/src/game/systems/game_scene.rs @@ -94,7 +94,7 @@ pub fn apply_piece_movement( // TODO: replace with actual logic if piece_controls.instant_drop { grid_transform.translation.y = 0; - on_piece_placed.write(OnPiecePlaced { entity }); + on_piece_placed.write(OnPiecePlaced { piece: entity }); *input_repeat = None; continue; } @@ -105,7 +105,7 @@ pub fn apply_piece_movement( } if !can_move_down && piece_controls.cumulated_gravity >= 3.0 { // Force piece placement - on_piece_placed.write(OnPiecePlaced { entity }); + on_piece_placed.write(OnPiecePlaced { piece: entity }); *input_repeat = None; continue; } @@ -152,17 +152,19 @@ pub fn handle_piece_placed( mut on_piece_placed: EventReader, mut commands: Commands, parent_query: Query<&ChildOf>, + child_query: Query<&Children>, + grid_transform_query: Query<&GridTransform>, + block_query: Query<&GridTransform, With>, mut next_piece_query: Query<(&mut NextPiece, &GameArea)>, ) { - for event in on_piece_placed.read() { - info!("Placed piece {:#}", event.entity); - let mut entity_cmds = commands - .get_entity(event.entity) - .unwrap() - .remove::<(PieceControls, ControllablePiece)>(); - let parent = parent_query.get(event.entity).unwrap().0; + for OnPiecePlaced { piece } in on_piece_placed.read() { + let piece = *piece; + + let parent = parent_query.get(piece).unwrap().0; let (mut next_piece, game_area) = next_piece_query.get_mut(parent).unwrap(); let new_piece = next_piece.take_and_generate(); + + // Create new piece commands.entity(parent).with_child(( GridTransform { translation: game_area.block_spawn_point(), @@ -170,6 +172,33 @@ pub fn handle_piece_placed( ControllablePiece, create_piece(new_piece), )); + + // Move blocks from old piece to under game area + let piece_position = grid_transform_query.get(piece).cloned().unwrap_or_default(); + for child in child_query + .get(piece) + .iter() + .flat_map(|children| children.iter()) + { + if let Ok(block_pos) = block_query.get(child) { + let global_pos = piece_position.translation + block_pos.translation; + commands.entity(child).insert(( + GridTransform { + translation: global_pos, + }, + ChildOf(parent), + )); + } + } + + // Despawn old piece + commands.entity(piece).despawn(); + + // commands + // .get_entity(event.entity) + // .unwrap() + // .remove::<(PieceControls, ControllablePiece)>(); + // TODO: Finish up clearing the piece / lines // TODO: Lose condition } diff --git a/src/game/tetris.rs b/src/game/tetris.rs index a4e18d8..a9dbde7 100644 --- a/src/game/tetris.rs +++ b/src/game/tetris.rs @@ -123,11 +123,11 @@ impl NextPiece { } } -#[derive(Component, Clone, Debug, Reflect)] +#[derive(Component, Clone, Debug, Default, Reflect)] #[reflect(Component)] pub struct Block; #[derive(Event, Clone, Debug)] pub struct OnPiecePlaced { - pub entity: Entity, + pub piece: Entity, }