diff --git a/src/bin/tetris/main.rs b/src/bin/tetris/main.rs index 76aab0c..28b8c4a 100644 --- a/src/bin/tetris/main.rs +++ b/src/bin/tetris/main.rs @@ -3,18 +3,7 @@ // Bevy basically forces "complex types" with Querys #![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 itertools::Itertools; #[cfg(test)] mod test; @@ -30,7 +19,7 @@ fn main() { App::new() .add_plugins(BaseGamePlugin { name: "falling-block-adventure".into(), - target_resolution: (640.0, 480.0).into(), + target_resolution: (640, 480).into(), game_type: GameType::Two, ..default() }) @@ -510,7 +499,7 @@ fn init_ui(mut commands: Commands, output_images: Res, images: Res border: UiRect::all(Val::Px(5.0)), ..default() }, - BorderColor(WHITE), + BorderColor::all(WHITE), )) .with_children(|parent| { let img = images.get(&output_images.tetris).unwrap(); @@ -536,7 +525,7 @@ fn init_ui(mut commands: Commands, output_images: Res, images: Res border: UiRect::all(Val::Px(5.0)), ..default() }, - BorderColor(WHITE), + BorderColor::all(WHITE), )) .with_children(|parent| { let img = images.get(&output_images.battler).unwrap(); @@ -848,22 +837,22 @@ fn kb_input( // Up arrow should rotate if in falling mode // Only move up if in falling::off mode KeyCode::ArrowUp => { - commands.entity(e).event(Movement::Rotate); + commands.entity(e).trigger(|entity| Movement { entity, direction: MovementDirection::Rotate }); } KeyCode::ArrowDown => { - commands.entity(e).event(Movement::Down); + commands.entity(e).trigger(|entity| Movement { entity, direction: MovementDirection::Down }); } KeyCode::ArrowLeft => { - commands.entity(e).event(Movement::Left); + commands.entity(e).trigger(|entity| Movement { entity, direction: MovementDirection::Left }); } KeyCode::ArrowRight => { - commands.entity(e).event(Movement::Right); + commands.entity(e).trigger(|entity| Movement { entity, direction: MovementDirection::Right }); } KeyCode::Enter => { - commands.entity(e).event(Movement::Skip); + commands.entity(e).trigger(|entity| Movement { entity, direction: MovementDirection::Skip }); } KeyCode::Space => { - commands.entity(e).event(Swap); + commands.entity(e).trigger(|entity| Swap { entity }); } KeyCode::Escape => next.set(match curr.get() { GameState::Falling => GameState::Pause, @@ -887,7 +876,7 @@ fn kb_input( fn falling(mut shape: Query>, mut commands: Commands) { shape.iter_mut().for_each(|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 -#[derive(Message, Copy, Clone, PartialEq)] -struct Swap; +#[derive(EntityEvent, Message, Copy, Clone, PartialEq)] +struct Swap { + entity: Entity +} /// Movement events evented on the piece -#[derive(Message, Copy, Clone, PartialEq)] -enum Movement { +#[derive(Message, EntityEvent, Copy, Clone, PartialEq)] +struct Movement { + entity: Entity, + direction: MovementDirection, +} + +#[derive(Clone, Copy, Debug, PartialEq)] +enum MovementDirection { Down, Left, Right, @@ -1031,18 +1028,18 @@ fn movement( shape.get_mut(event.entity), grid_positions.get(event.entity), ) { - let new_positions = match event.event() { - Movement::Down => vec![center.with_offset(0, -1)], - Movement::Left => vec![center.with_offset(-1, 0)], - Movement::Right => vec![center.with_offset(1, 0)], - Movement::Rotate => vec![Ok(*center)], - Movement::Skip => (1..=center.y) + let new_positions = match event.event().direction { + MovementDirection::Down => vec![center.with_offset(0, -1)], + MovementDirection::Left => vec![center.with_offset(-1, 0)], + MovementDirection::Right => vec![center.with_offset(1, 0)], + MovementDirection::Rotate => vec![Ok(*center)], + MovementDirection::Skip => (1..=center.y) .map(|i| center.with_offset(0, -(i as isize))) .collect(), }; - let new_shape = match event.event() { - Movement::Down | Movement::Left | Movement::Right | Movement::Skip => *this_shape, - Movement::Rotate => this_shape.rotated(), + let new_shape = match event.event().direction { + MovementDirection::Down | MovementDirection::Left | MovementDirection::Right | MovementDirection::Skip => *this_shape, + MovementDirection::Rotate => this_shape.rotated(), }; debug!( "Proposed change: {:?}\n{}", @@ -1073,7 +1070,7 @@ fn movement( // If there would be a collision between blocks if gp == *other_gp { // And we are moving down - if *event.event() == Movement::Down { + if event.event().direction == MovementDirection::Down { // De-activate this piece commands.entity(event.entity).remove::(); } @@ -1209,8 +1206,9 @@ fn sync_health( }) } -#[derive(Message)] +#[derive(Message, EntityEvent)] struct Damage { + entity: Entity, quantity: f32 } @@ -1228,7 +1226,7 @@ fn damage_on_place_shape( ) { events.read().for_each(|_| { 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(|_| { 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>, mut commands: Commands, ) { - commands.entity(*protagonist).event(Damage { quantity: 1.0 }); + commands.entity(*protagonist).trigger(|entity| Damage { entity, quantity: 1.0 }); } diff --git a/src/lib.rs b/src/lib.rs index 103b4da..c3728a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,10 +19,14 @@ pub use std::fmt::Display; // Community libraries pub use bevy::{ - asset::{AssetLoader, AssetMetaCheck, LoadContext, LoadState, LoadedFolder, io::Reader}, - camera::primitives::*, + asset::{AssetLoader, AssetMetaCheck, LoadContext, LoadState, LoadedFolder, io::Reader, RenderAssetUsages}, + math::{FloatOrd}, + camera::{*, primitives::*, visibility::*}, color::palettes::css::*, gizmos::{aabb::AabbGizmoPlugin, light::LightGizmoPlugin}, + render::{ + render_resource::{Extent3d, TextureDimension, TextureFormat, TextureUsages}, + }, input::{ ButtonState, common_conditions::{input_just_pressed, input_just_released, input_pressed}, @@ -39,6 +43,7 @@ pub use bevy::{ }; pub use serde::Deserialize; pub use thiserror::Error; +pub use itertools::Itertools; // Internal modules pub use base_game::*;