This commit is contained in:
Christian Nieves
2024-12-11 15:08:27 -06:00
parent 6f8fe45a0e
commit b0ef90953d

View File

@ -61,10 +61,10 @@ fn spawn_tile(commands: &mut Commands, color: Color, size: f32, position: Positi
.id() .id()
} }
fn scale_translation( fn post_scale_tiles(
window: Single<&Window>, window: Single<&Window>,
mut q: Query<(&Size, &mut Transform)>,
matrix: Res<Matrix>, matrix: Res<Matrix>,
mut q: Query<(&Size, &mut Transform), With<Tile>>,
) { ) {
for (sprite_size, mut transform) in q.iter_mut() { for (sprite_size, mut transform) in q.iter_mut() {
transform.scale = Vec3::new( transform.scale = Vec3::new(
@ -75,10 +75,32 @@ fn scale_translation(
} }
} }
fn position_translation( fn post_translate_tetrominos(
window: Single<&Window>, window: Single<&Window>,
mut q: Query<(&Position, &mut Transform)>,
matrix: Res<Matrix>, matrix: Res<Matrix>,
mut q: Query<(&Position, &mut Transform), With<Tile>>,
) {
// 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<Matrix>,
mut q: Query<(&Position, &mut Transform), With<Tile>>,
) { ) {
// We subtract half the window width because our coordinate system starts at the bottom left, and // 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 // 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)] #[derive(Component, Debug)]
struct BrickColor(Color); struct Tile;
#[derive(Bundle, Debug)] #[derive(Bundle, Debug)]
struct TileBundle { struct TileBundle {
position: Position, position: Position,
size: Size, size: Size,
sprite: Sprite, sprite: Sprite,
tile: Tile,
} }
fn spawn_tetromino(commands: &mut Commands, tetromino: TetrominoCoords, x: u32, y: u32) { 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), color: Color::srgb(0.8, 1.0, 0.8),
..Default::default() ..Default::default()
}, },
tile: Tile,
}); });
} }
} }
@ -154,7 +178,7 @@ fn main() {
Startup, Startup,
(init_singletons, init_window, init_tile_grid).chain(), (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); app.add_plugins(debug_plugin::debug_plugin);