From a310bddce0305b820f560d36af9ad5228fc1f30d Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Thu, 30 Oct 2025 16:19:54 -0700 Subject: [PATCH] Sprite background (ish) and correct size for ui elements --- src/bin/tetris/main.rs | 106 ++++++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 49 deletions(-) diff --git a/src/bin/tetris/main.rs b/src/bin/tetris/main.rs index f5975fb..377c1c0 100644 --- a/src/bin/tetris/main.rs +++ b/src/bin/tetris/main.rs @@ -89,7 +89,6 @@ fn main() { sync_singleton_to_ui::.run_if(any_component_changed::), ), ) - .add_systems(Update, draw_grid) .run(); } @@ -278,17 +277,34 @@ fn init_world( mut meshes: ResMut>, mut materials: ResMut>, ) { + let mesh = meshes.add(Rectangle::new(SCALE, SCALE)); + let block_material = materials.add(ColorMaterial { + color: WHITE.into(), + ..default() + }); + let grid_material = materials.add(ColorMaterial { + color: BLACK.into(), + ..default() + }); commands.insert_resource(Visuals { - material: materials.add(ColorMaterial { - color: WHITE.into(), - ..default() - }), - mesh: meshes.add(Rectangle::new(SCALE, SCALE)), + material: block_material.clone(), + mesh: mesh.clone(), }); - (0..Y_MAX).for_each(|i| { - info!("Spawning line {i}"); - commands.spawn((Line(i), LineBlocks::default(), TETRIS)); + (0..Y_MAX).for_each(|y| { + // Spawn tiles for this line + (0..X_MAX).for_each(|x| { + commands.spawn(( + Mesh2d(mesh.clone()), + MeshMaterial2d(grid_material.clone()), + GridPosition { x, y }, + Transform::from_xyz(0.0, 0.0, -1.0), + TETRIS, + )); + }); + + // Spawn line for holding blocks + commands.spawn((Line(y), LineBlocks::default(), TETRIS)); }); } @@ -309,8 +325,7 @@ fn init_cameras( mut images: ResMut>, mut output_images: ResMut, ) { - fn render_target_image() -> Image { - let (width, height) = (1028, 1028); + fn render_target_image((width, height): (u32, u32)) -> Image { let size = Extent3d { width, height, @@ -329,7 +344,8 @@ fn init_cameras( } { - let img = render_target_image(); + let (width, height) = (SCALE as u32 * X_MAX as u32, SCALE as u32 * Y_MAX as u32); + let img = render_target_image((width, height)); let target = { let handle = images.add(img); output_images.tetris = handle.clone(); @@ -353,7 +369,7 @@ fn init_cameras( } { - let img = render_target_image(); + let img = render_target_image((512, 512)); let target = { let handle = images.add(img); output_images.battler = handle.clone(); @@ -377,16 +393,14 @@ fn init_cameras( } } -fn init_ui(mut commands: Commands, output_images: Res) { +fn init_ui(mut commands: Commands, output_images: Res, images: Res>) { commands - .spawn(( - Node { - align_self: AlignSelf::Center, - justify_self: JustifySelf::End, - flex_direction: FlexDirection::Column, - ..default() - }, - )) + .spawn((Node { + align_self: AlignSelf::Center, + justify_self: JustifySelf::End, + flex_direction: FlexDirection::Column, + ..default() + },)) .with_children(|parent| { parent .spawn((Node { @@ -428,10 +442,12 @@ fn init_ui(mut commands: Commands, output_images: Res) { ..default() }, BorderColor(BLACK.into()), - children![( + )).with_children(|parent| { + let img = images.get(&output_images.tetris).unwrap(); + parent.spawn(( Node { - width: Val::Px(256.0), - height: Val::Px(256.0), + width: Val::Px(img.size_f32().x * 0.5), + height: Val::Px(img.size_f32().y * 0.5), ..default() }, ImageNode { @@ -439,8 +455,8 @@ fn init_ui(mut commands: Commands, output_images: Res) { image_mode: NodeImageMode::Stretch, ..default() }, - )] - )); + )); + }); commands.spawn(( Node { @@ -450,10 +466,12 @@ fn init_ui(mut commands: Commands, output_images: Res) { ..default() }, BorderColor(BLACK.into()), - children![( + )).with_children(|parent| { + let img = images.get(&output_images.battler).unwrap(); + parent.spawn(( Node { - width: Val::Px(256.0), - height: Val::Px(256.0), + width: Val::Px(img.size_f32().x * 0.5), + height: Val::Px(img.size_f32().y * 0.5), ..default() }, ImageNode { @@ -461,8 +479,8 @@ fn init_ui(mut commands: Commands, output_images: Res) { image_mode: NodeImageMode::Stretch, ..default() }, - )] - )); + )); + }); commands .spawn(( @@ -697,7 +715,8 @@ fn update_position( gp, v3 ); - t.translation = gp.into(); + t.translation.x = v3.x; + t.translation.y = v3.y; }); } @@ -793,20 +812,9 @@ fn kb_input( ); } -fn draw_grid(mut gizmos: Gizmos) { - gizmos - .grid_2d( - Isometry2d::IDENTITY, - UVec2::new(X_MAX as u32, Y_MAX as u32), - Vec2::new(SCALE, SCALE), - GREEN, - ) - .outer_edges(); -} - fn falling(mut shape: Query>, mut commands: Commands) { shape.iter_mut().for_each(|e| { - info!("Making {:?} fall", e); + debug!("Making {:?} fall", e); commands.entity(e).trigger(Movement::Down); }); } @@ -911,7 +919,7 @@ fn clear_line( fn adjust_block_lines( query: Query<(Entity, &Line), Changed>, parent: Query<&LineBlocks>, - mut blocks: Query<&mut GridPosition>, + mut blocks: Query<&mut GridPosition, With>, ) { query.iter().for_each(|(e, Line(i))| { parent.iter_descendants(e).for_each(|block| { @@ -939,9 +947,9 @@ enum Movement { // TODO: When out of bounds left/right, try to move piece away from wall fn movement( trigger: Trigger, - mut grid_positions: Query<&mut GridPosition, Or<(With, With)>>, + mut grid_positions: Query<&mut GridPosition, Or<(With, With, Without)>>, mut shape: Query<&mut Shape>, - inactive: Query<&GridPosition, (Without, Without)>, + inactive: Query<&GridPosition, (Without, Without, With)>, mut commands: Commands, ) { if let (Ok(this_shape), Ok(center)) = ( @@ -1046,7 +1054,7 @@ fn swap( fn deactivate_shape( mut events: RemovedComponents, - grid_positions: Query<&GridPosition>, + grid_positions: Query<&GridPosition, With>, parent: Query<&ShapeBlocks>, lines: Query<(Entity, &Line), With>, mut commands: Commands,