This commit is contained in:
Christian Nieves
2024-12-11 15:00:43 -06:00
parent 9ee613c68b
commit 6f8fe45a0e

View File

@ -6,7 +6,7 @@ use std::ops::{Add, Sub};
use glite::debug_plugin;
#[cfg_attr(feature = "debug", derive(bevy_inspector_egui::InspectorOptions))]
#[derive(Component, Debug, Default, Copy, Clone, Eq, PartialEq)]
#[derive(Component, Debug, Default, Copy, Clone, Eq, PartialEq, Reflect)]
struct Position {
pub x: u32,
pub y: u32,
@ -35,7 +35,7 @@ impl Sub for Position {
}
}
#[derive(Component)]
#[derive(Component, Debug, Reflect)]
struct Size {
width: f32,
height: f32,
@ -97,35 +97,46 @@ fn position_translation(
}
}
#[derive(Component)]
struct Tetromino([[u32; 2]; 4]);
#[derive(Component, Debug)]
struct TetrominoCoords([[u32; 2]; 4]);
// TODO: check these positions are right
const BRICK_I: Tetromino = Tetromino([[0, 0], [1, 0], [2, 0], [3, 0]]);
const BRICK_J: Tetromino = Tetromino([[0, 0], [1, 0], [2, 0], [2, 1]]);
const BRICK_L: Tetromino = Tetromino([[0, 0], [1, 0], [2, 0], [0, 1]]);
const BRICK_O: Tetromino = Tetromino([[0, 0], [1, 0], [0, 1], [1, 1]]);
const BRICK_S: Tetromino = Tetromino([[0, 0], [1, 0], [1, 1], [2, 1]]);
const BRICK_T: Tetromino = Tetromino([[0, 0], [1, 0], [2, 0], [1, 1]]);
const BRICK_Z: Tetromino = Tetromino([[0, 1], [1, 1], [1, 0], [2, 0]]);
const BRICK_I: TetrominoCoords = TetrominoCoords([[0, 0], [1, 0], [2, 0], [3, 0]]);
const BRICK_J: TetrominoCoords = TetrominoCoords([[0, 0], [1, 0], [2, 0], [2, 1]]);
const BRICK_L: TetrominoCoords = TetrominoCoords([[0, 0], [1, 0], [2, 0], [0, 1]]);
const BRICK_O: TetrominoCoords = TetrominoCoords([[0, 0], [1, 0], [0, 1], [1, 1]]);
const BRICK_S: TetrominoCoords = TetrominoCoords([[0, 0], [1, 0], [1, 1], [2, 1]]);
const BRICK_T: TetrominoCoords = TetrominoCoords([[0, 0], [1, 0], [2, 0], [1, 1]]);
const BRICK_Z: TetrominoCoords = TetrominoCoords([[0, 1], [1, 1], [1, 0], [2, 0]]);
enum RenderLayer {
Background = 0,
Brick = 10,
Brick = 1,
}
fn spawn_tetromino(commands: &mut Commands, tetromino: Tetromino, x: u32, y: u32) {
#[derive(Component, Debug)]
struct BrickColor(Color);
#[derive(Bundle, Debug)]
struct TileBundle {
position: Position,
size: Size,
sprite: Sprite,
}
fn spawn_tetromino(commands: &mut Commands, tetromino: TetrominoCoords, x: u32, y: u32) {
for coord in tetromino.0 {
spawn_tile(
commands,
Color::srgb(1.0, 1.0, 1.0),
0.77,
Position {
commands.spawn(TileBundle {
position: Position {
x: coord[0] + x,
y: coord[1] + y,
z: RenderLayer::Brick as u32,
},
);
size: Size::square(0.77),
sprite: Sprite {
color: Color::srgb(0.8, 1.0, 0.8),
..Default::default()
},
});
}
}
@ -147,6 +158,9 @@ fn main() {
app.add_plugins(debug_plugin::debug_plugin);
app.register_type::<Position>();
app.register_type::<Size>();
// Run the app
app.run();
}
@ -217,6 +231,7 @@ fn test_render_all_tetrominos(mut commands: Commands) {
spawn_tetromino(&mut commands, BRICK_T, 4, 0);
spawn_tetromino(&mut commands, BRICK_J, 8, 0);
spawn_tetromino(&mut commands, BRICK_L, 12, 0);
spawn_tetromino(&mut commands, BRICK_O, 0, 4);
spawn_tetromino(&mut commands, BRICK_S, 4, 4);
spawn_tetromino(&mut commands, BRICK_Z, 8, 4);