From 333dae908938593c64cd09c30f31a0d6a8a9378f Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Fri, 3 Oct 2025 21:40:25 -0700 Subject: [PATCH] tetris: we have a block that can move --- justfile | 10 ++++-- src/base_game.rs | 25 +------------ src/bin/tetris/README.md | 7 ++++ src/bin/tetris/main.rs | 78 +++++++++++++++++++++++++++++++++++++++- 4 files changed, 93 insertions(+), 27 deletions(-) diff --git a/justfile b/justfile index 1345ee4..0682cc5 100644 --- a/justfile +++ b/justfile @@ -19,14 +19,20 @@ optimize NAME: # Replace old bin with new (compressed) bin mv ./dist/{{NAME}}/bin_bg-tmp.wasm ./dist/{{NAME}}/bin_bg.wasm -build-example EXAMPLE: +build-example-dev EXAMPLE: cargo build --example {{EXAMPLE}} --profile wasm-dev --target wasm32-unknown-unknown +build-example EXAMPLE: + cargo build --example {{EXAMPLE}} --profile release --target wasm32-unknown-unknown + build-bin GAME: # wasm binary cargo build --bin {{GAME}} --profile wasm-release --target wasm32-unknown-unknown --features hide_debug -example NAME: (build-example NAME) (bindgen "wasm-dev" "examples"/NAME) +example-dev NAME: (build-example-dev NAME) (bindgen "wasm-dev" "examples"/NAME) + cp web/example.html ./dist/examples/{{NAME}}/index.html + +example NAME: (build-example NAME) (bindgen "release" "examples"/NAME) cp web/example.html ./dist/examples/{{NAME}}/index.html # Build the web version diff --git a/src/base_game.rs b/src/base_game.rs index e7119d9..d983682 100644 --- a/src/base_game.rs +++ b/src/base_game.rs @@ -48,8 +48,6 @@ impl Plugin for BaseGamePlugin { .add_plugins(LoadingPlugin) .add_plugins(BaseUiPlugin) .add_plugins(ParallaxPlugin) - // .add_systems(Update, scale_game.run_if(any_component_changed::)) - .insert_resource(TargetResolution(self.target_resolution.clone())) .init_resource::(); match self.game_type { @@ -59,9 +57,6 @@ impl Plugin for BaseGamePlugin { } } -#[derive(Resource)] -struct TargetResolution(WindowResolution); - /// System to toggle the visibility of entities based on their state pub fn toggle_state_visibility( mut q: Query<(Entity, &mut Visibility, &S)>, @@ -84,6 +79,7 @@ pub fn create_camera_3d(mut commands: Commands) { Camera3d { ..default() }, Camera { ..default() }, AmbientLight::default(), + Transform::default(), )); } @@ -95,22 +91,3 @@ pub fn create_camera_2d(mut commands: Commands) { #[derive(Default, Resource)] pub struct Rand(pub RandomState); - -/// Scale the game based on the difference between the target and real resolution -fn scale_game( - mut window: Single<&mut Window>, - target_resolution: Res, - mut last_scale_factor: Local, -) { - let current_resolution: Vec2 = window.resolution.size(); - let scale_width = current_resolution.x.round() / target_resolution.0.width().round(); - let scale_height = current_resolution.y.round() / target_resolution.0.height().round(); - let scale_factor = f32::min(scale_width, scale_height); - if window.resolution.scale_factor() != scale_factor { - // Need to check the previously set scale factor because the system can flip-flop otherwise - if scale_factor != *last_scale_factor { - *last_scale_factor = window.resolution.scale_factor(); - window.resolution.set_scale_factor(scale_factor); - } - } -} diff --git a/src/bin/tetris/README.md b/src/bin/tetris/README.md index e69de29..d9368cf 100644 --- a/src/bin/tetris/README.md +++ b/src/bin/tetris/README.md @@ -0,0 +1,7 @@ +# Falling Blocks RPG + +This game is inspired by both Tetris and Peglin. + +Your goal is to play Tetris (or a similar falling block game) but while that is happening you are carrying out a 2d real-time combat RPG battle. + +Between battles/levels you choose a different level to go to in an overworld, choose upgrades, perks, and maybe some other stuff! diff --git a/src/bin/tetris/main.rs b/src/bin/tetris/main.rs index 21e8ec7..223daa5 100644 --- a/src/bin/tetris/main.rs +++ b/src/bin/tetris/main.rs @@ -3,8 +3,84 @@ use games::*; fn main() { App::new() .add_plugins(BaseGamePlugin { - name: "tetris-rpg".into(), + name: "falling-block-rpg".into(), + target_resolution: (640.0, 480.0).into(), ..default() }) + .add_systems(Startup, init_pieces) + .add_systems(Update, (kb_movement.run_if(on_event::), update_position)) .run(); } + +#[derive(Component, Default, Debug, Clone, Copy)] +struct GridPosition { x: isize, y: isize } + +impl From<&GridPosition> for Vec3 { + fn from(GridPosition { x, y }: &GridPosition) -> Vec3 { + Vec3::new((*x as f32) * 1.0, (*y as f32) * 1.0, -10.0) + } +} + +impl From<(isize, isize)> for GridPosition { + fn from((x, y): (isize, isize)) -> GridPosition { + GridPosition { x, y } + } +} + +impl std::ops::Add for GridPosition { + type Output = Self; + + fn add(self, GridPosition { x: x2, y: y2 }: Self) -> Self { + GridPosition { x: self.x + x2, y: self.y + y2 } + } +} + +fn init_pieces( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + commands.spawn(( + Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))), + MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))), + Transform::default(), + GridPosition::default(), + )); +} + +fn update_position( + mut query: Query<(&GridPosition, &mut Transform), Changed>, +) { + query.iter_mut().for_each(|(gp, mut t)| { + let tmp: Vec3 = gp.into(); + info!("Updating position {:?}", tmp); + + t.translation = gp.into(); + }); +} + +fn kb_movement(mut events: EventReader, mut query: Query<&mut GridPosition>) { + events.read().for_each(|KeyboardInput { key_code, state, .. }| { + if let ButtonState::Pressed = state { + let diff: GridPosition = match key_code { + KeyCode::ArrowUp => { + (0, 1) + }, + KeyCode::ArrowDown => { + (0, -1) + }, + KeyCode::ArrowLeft => { + (-1, 0) + }, + KeyCode::ArrowRight => { + (1, 0) + }, + _ => (0, 0) + }.into(); + query.iter_mut().for_each(|mut gp| { + info!("Moving by {:?}", diff); + *gp = *gp + diff; + }); + } + }); +}