From 2a5d7363fe9a0d4e82e3639985f917a338d70871 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Thu, 30 Oct 2025 16:39:35 -0700 Subject: [PATCH] Assert when two entities should not share the same grid position but do --- src/bin/tetris/main.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/bin/tetris/main.rs b/src/bin/tetris/main.rs index 377c1c0..3c13adc 100644 --- a/src/bin/tetris/main.rs +++ b/src/bin/tetris/main.rs @@ -77,6 +77,7 @@ fn main() { adjust_block_lines .run_if(any_component_changed::) .after(clear_line), + assert_grid_position_uniqueness.run_if(any_component_changed::), ), ) // UI systems @@ -272,6 +273,9 @@ impl Display for ShapeStore { } } +#[derive(Component)] +struct GridBackground; + fn init_world( mut commands: Commands, mut meshes: ResMut>, @@ -299,6 +303,7 @@ fn init_world( MeshMaterial2d(grid_material.clone()), GridPosition { x, y }, Transform::from_xyz(0.0, 0.0, -1.0), + GridBackground, TETRIS, )); }); @@ -441,13 +446,13 @@ fn init_ui(mut commands: Commands, output_images: Res, images: Res border: UiRect::all(Val::Px(5.0)), ..default() }, - BorderColor(BLACK.into()), + BorderColor(WHITE.into()), )).with_children(|parent| { let img = images.get(&output_images.tetris).unwrap(); parent.spawn(( Node { - width: Val::Px(img.size_f32().x * 0.5), - height: Val::Px(img.size_f32().y * 0.5), + width: Val::Px(img.size_f32().x * 0.75), + height: Val::Px(img.size_f32().y * 0.75), ..default() }, ImageNode { @@ -465,7 +470,7 @@ fn init_ui(mut commands: Commands, output_images: Res, images: Res border: UiRect::all(Val::Px(5.0)), ..default() }, - BorderColor(BLACK.into()), + BorderColor(WHITE.into()), )).with_children(|parent| { let img = images.get(&output_images.battler).unwrap(); parent.spawn(( @@ -919,7 +924,7 @@ fn clear_line( fn adjust_block_lines( query: Query<(Entity, &Line), Changed>, parent: Query<&LineBlocks>, - mut blocks: Query<&mut GridPosition, With>, + mut blocks: Query<&mut GridPosition>, ) { query.iter().for_each(|(e, Line(i))| { parent.iter_descendants(e).for_each(|block| { @@ -1093,3 +1098,11 @@ fn update_next_shapes(mut buffer: ResMut) { ]); } } + +fn assert_grid_position_uniqueness( + grid_positions: Query<&GridPosition, (Without, Without)> +) { + grid_positions.iter_combinations().for_each(|[a, b]| { + assert_ne!(a, b, "Two entities are in the same grid position!"); + }); +}