Tetris works on 0.17

main
Elijah Voigt 1 month ago
parent 1e2d37ed60
commit 602d6a923e

@ -3,18 +3,7 @@
// Bevy basically forces "complex types" with Querys // Bevy basically forces "complex types" with Querys
#![allow(clippy::type_complexity)] #![allow(clippy::type_complexity)]
use bevy::{
asset::RenderAssetUsages,
math::{FloatOrd},
render::{
mesh::MeshAabb,
camera::{ImageRenderTarget, RenderTarget},
render_resource::{Extent3d, TextureDimension, TextureFormat, TextureUsages},
view::RenderLayers,
},
};
use games::*; use games::*;
use itertools::Itertools;
#[cfg(test)] #[cfg(test)]
mod test; mod test;
@ -30,7 +19,7 @@ fn main() {
App::new() App::new()
.add_plugins(BaseGamePlugin { .add_plugins(BaseGamePlugin {
name: "falling-block-adventure".into(), name: "falling-block-adventure".into(),
target_resolution: (640.0, 480.0).into(), target_resolution: (640, 480).into(),
game_type: GameType::Two, game_type: GameType::Two,
..default() ..default()
}) })
@ -510,7 +499,7 @@ fn init_ui(mut commands: Commands, output_images: Res<OutputImages>, images: Res
border: UiRect::all(Val::Px(5.0)), border: UiRect::all(Val::Px(5.0)),
..default() ..default()
}, },
BorderColor(WHITE), BorderColor::all(WHITE),
)) ))
.with_children(|parent| { .with_children(|parent| {
let img = images.get(&output_images.tetris).unwrap(); let img = images.get(&output_images.tetris).unwrap();
@ -536,7 +525,7 @@ fn init_ui(mut commands: Commands, output_images: Res<OutputImages>, images: Res
border: UiRect::all(Val::Px(5.0)), border: UiRect::all(Val::Px(5.0)),
..default() ..default()
}, },
BorderColor(WHITE), BorderColor::all(WHITE),
)) ))
.with_children(|parent| { .with_children(|parent| {
let img = images.get(&output_images.battler).unwrap(); let img = images.get(&output_images.battler).unwrap();
@ -848,22 +837,22 @@ fn kb_input(
// Up arrow should rotate if in falling mode // Up arrow should rotate if in falling mode
// Only move up if in falling::off mode // Only move up if in falling::off mode
KeyCode::ArrowUp => { KeyCode::ArrowUp => {
commands.entity(e).event(Movement::Rotate); commands.entity(e).trigger(|entity| Movement { entity, direction: MovementDirection::Rotate });
} }
KeyCode::ArrowDown => { KeyCode::ArrowDown => {
commands.entity(e).event(Movement::Down); commands.entity(e).trigger(|entity| Movement { entity, direction: MovementDirection::Down });
} }
KeyCode::ArrowLeft => { KeyCode::ArrowLeft => {
commands.entity(e).event(Movement::Left); commands.entity(e).trigger(|entity| Movement { entity, direction: MovementDirection::Left });
} }
KeyCode::ArrowRight => { KeyCode::ArrowRight => {
commands.entity(e).event(Movement::Right); commands.entity(e).trigger(|entity| Movement { entity, direction: MovementDirection::Right });
} }
KeyCode::Enter => { KeyCode::Enter => {
commands.entity(e).event(Movement::Skip); commands.entity(e).trigger(|entity| Movement { entity, direction: MovementDirection::Skip });
} }
KeyCode::Space => { KeyCode::Space => {
commands.entity(e).event(Swap); commands.entity(e).trigger(|entity| Swap { entity });
} }
KeyCode::Escape => next.set(match curr.get() { KeyCode::Escape => next.set(match curr.get() {
GameState::Falling => GameState::Pause, GameState::Falling => GameState::Pause,
@ -887,7 +876,7 @@ fn kb_input(
fn falling(mut shape: Query<Entity, With<Shape>>, mut commands: Commands) { fn falling(mut shape: Query<Entity, With<Shape>>, mut commands: Commands) {
shape.iter_mut().for_each(|e| { shape.iter_mut().for_each(|e| {
debug!("Making {:?} fall", e); debug!("Making {:?} fall", e);
commands.entity(e).event(Movement::Down); commands.entity(e).trigger(|entity| Movement { entity, direction: MovementDirection::Down });
}); });
} }
@ -1003,12 +992,20 @@ fn adjust_block_lines(
} }
/// Swap the current piece out /// Swap the current piece out
#[derive(Message, Copy, Clone, PartialEq)] #[derive(EntityEvent, Message, Copy, Clone, PartialEq)]
struct Swap; struct Swap {
entity: Entity
}
/// Movement events evented on the piece /// Movement events evented on the piece
#[derive(Message, Copy, Clone, PartialEq)] #[derive(Message, EntityEvent, Copy, Clone, PartialEq)]
enum Movement { struct Movement {
entity: Entity,
direction: MovementDirection,
}
#[derive(Clone, Copy, Debug, PartialEq)]
enum MovementDirection {
Down, Down,
Left, Left,
Right, Right,
@ -1031,18 +1028,18 @@ fn movement(
shape.get_mut(event.entity), shape.get_mut(event.entity),
grid_positions.get(event.entity), grid_positions.get(event.entity),
) { ) {
let new_positions = match event.event() { let new_positions = match event.event().direction {
Movement::Down => vec![center.with_offset(0, -1)], MovementDirection::Down => vec![center.with_offset(0, -1)],
Movement::Left => vec![center.with_offset(-1, 0)], MovementDirection::Left => vec![center.with_offset(-1, 0)],
Movement::Right => vec![center.with_offset(1, 0)], MovementDirection::Right => vec![center.with_offset(1, 0)],
Movement::Rotate => vec![Ok(*center)], MovementDirection::Rotate => vec![Ok(*center)],
Movement::Skip => (1..=center.y) MovementDirection::Skip => (1..=center.y)
.map(|i| center.with_offset(0, -(i as isize))) .map(|i| center.with_offset(0, -(i as isize)))
.collect(), .collect(),
}; };
let new_shape = match event.event() { let new_shape = match event.event().direction {
Movement::Down | Movement::Left | Movement::Right | Movement::Skip => *this_shape, MovementDirection::Down | MovementDirection::Left | MovementDirection::Right | MovementDirection::Skip => *this_shape,
Movement::Rotate => this_shape.rotated(), MovementDirection::Rotate => this_shape.rotated(),
}; };
debug!( debug!(
"Proposed change: {:?}\n{}", "Proposed change: {:?}\n{}",
@ -1073,7 +1070,7 @@ fn movement(
// If there would be a collision between blocks // If there would be a collision between blocks
if gp == *other_gp { if gp == *other_gp {
// And we are moving down // And we are moving down
if *event.event() == Movement::Down { if event.event().direction == MovementDirection::Down {
// De-activate this piece // De-activate this piece
commands.entity(event.entity).remove::<Shape>(); commands.entity(event.entity).remove::<Shape>();
} }
@ -1209,8 +1206,9 @@ fn sync_health(
}) })
} }
#[derive(Message)] #[derive(Message, EntityEvent)]
struct Damage { struct Damage {
entity: Entity,
quantity: f32 quantity: f32
} }
@ -1228,7 +1226,7 @@ fn damage_on_place_shape(
) { ) {
events.read().for_each(|_| { events.read().for_each(|_| {
enemies.iter().for_each(|e| { enemies.iter().for_each(|e| {
commands.entity(e).event(Damage { quantity: 1.0 }); commands.entity(e).trigger(|entity| Damage { entity, quantity: 1.0 });
}); });
}); });
} }
@ -1240,7 +1238,7 @@ fn damage_on_clear_line(
) { ) {
events.read().for_each(|_| { events.read().for_each(|_| {
enemies.iter().for_each(|e| { enemies.iter().for_each(|e| {
commands.entity(e).event(Damage { quantity: 1.0 }); commands.entity(e).trigger(|entity| Damage { entity, quantity: 1.0 });
}); });
}); });
} }
@ -1249,5 +1247,5 @@ fn damage_over_time(
protagonist: Single<Entity, With<Protagonist>>, protagonist: Single<Entity, With<Protagonist>>,
mut commands: Commands, mut commands: Commands,
) { ) {
commands.entity(*protagonist).event(Damage { quantity: 1.0 }); commands.entity(*protagonist).trigger(|entity| Damage { entity, quantity: 1.0 });
} }

@ -19,10 +19,14 @@ pub use std::fmt::Display;
// Community libraries // Community libraries
pub use bevy::{ pub use bevy::{
asset::{AssetLoader, AssetMetaCheck, LoadContext, LoadState, LoadedFolder, io::Reader}, asset::{AssetLoader, AssetMetaCheck, LoadContext, LoadState, LoadedFolder, io::Reader, RenderAssetUsages},
camera::primitives::*, math::{FloatOrd},
camera::{*, primitives::*, visibility::*},
color::palettes::css::*, color::palettes::css::*,
gizmos::{aabb::AabbGizmoPlugin, light::LightGizmoPlugin}, gizmos::{aabb::AabbGizmoPlugin, light::LightGizmoPlugin},
render::{
render_resource::{Extent3d, TextureDimension, TextureFormat, TextureUsages},
},
input::{ input::{
ButtonState, ButtonState,
common_conditions::{input_just_pressed, input_just_released, input_pressed}, common_conditions::{input_just_pressed, input_just_released, input_pressed},
@ -39,6 +43,7 @@ pub use bevy::{
}; };
pub use serde::Deserialize; pub use serde::Deserialize;
pub use thiserror::Error; pub use thiserror::Error;
pub use itertools::Itertools;
// Internal modules // Internal modules
pub use base_game::*; pub use base_game::*;

Loading…
Cancel
Save