From b0ef90953dfa067c3e349703d0d230d32a17938f Mon Sep 17 00:00:00 2001 From: Christian Nieves Date: Wed, 11 Dec 2024 15:08:27 -0600 Subject: [PATCH] idk --- src/bin/tetris.rs | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/bin/tetris.rs b/src/bin/tetris.rs index 0081328..e897d2c 100644 --- a/src/bin/tetris.rs +++ b/src/bin/tetris.rs @@ -61,10 +61,10 @@ fn spawn_tile(commands: &mut Commands, color: Color, size: f32, position: Positi .id() } -fn scale_translation( +fn post_scale_tiles( window: Single<&Window>, - mut q: Query<(&Size, &mut Transform)>, matrix: Res, + mut q: Query<(&Size, &mut Transform), With>, ) { for (sprite_size, mut transform) in q.iter_mut() { transform.scale = Vec3::new( @@ -75,10 +75,32 @@ fn scale_translation( } } -fn position_translation( +fn post_translate_tetrominos( window: Single<&Window>, - mut q: Query<(&Position, &mut Transform)>, matrix: Res, + mut q: Query<(&Position, &mut Transform), With>, +) { + // We subtract half the window width because our coordinate system starts at the bottom left, and + // Translation starts from the center. We then add half the size of a single tile, because we want + // our sprites bottom left corner to be at the bottom left of a tile, not the center. + fn convert(pos: f32, bound_window: f32, bound_game: f32) -> f32 { + let tile_size = bound_window / bound_game; + pos / bound_game * bound_window - (bound_window / 2.) + (tile_size / 2.) + } + + for (pos, mut transform) in q.iter_mut() { + transform.translation = Vec3::new( + convert(pos.x as f32, window.width(), matrix.width as f32), + convert(pos.y as f32, window.height(), matrix.height as f32), + pos.z as f32, + ); + } +} + +fn post_translate_tiles( + window: Single<&Window>, + matrix: Res, + mut q: Query<(&Position, &mut Transform), With>, ) { // We subtract half the window width because our coordinate system starts at the bottom left, and // Translation starts from the center. We then add half the size of a single tile, because we want @@ -114,13 +136,14 @@ enum RenderLayer { } #[derive(Component, Debug)] -struct BrickColor(Color); +struct Tile; #[derive(Bundle, Debug)] struct TileBundle { position: Position, size: Size, sprite: Sprite, + tile: Tile, } fn spawn_tetromino(commands: &mut Commands, tetromino: TetrominoCoords, x: u32, y: u32) { @@ -136,6 +159,7 @@ fn spawn_tetromino(commands: &mut Commands, tetromino: TetrominoCoords, x: u32, color: Color::srgb(0.8, 1.0, 0.8), ..Default::default() }, + tile: Tile, }); } } @@ -154,7 +178,7 @@ fn main() { Startup, (init_singletons, init_window, init_tile_grid).chain(), ) - .add_systems(PostUpdate, (scale_translation, position_translation)); + .add_systems(PostUpdate, (post_scale_tiles, post_translate_tiles)); app.add_plugins(debug_plugin::debug_plugin);