From 01b796062e7bf9770d3378407a326f8610cbac5e Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 28 Mar 2023 11:57:37 -0500 Subject: [PATCH] Initial port to Bevy 0.10. --- Cargo.toml | 13 +++++---- src/core.rs | 27 ++++++------------- src/exploration.rs | 59 ++++++++++++++++++----------------------- src/log.rs | 6 ++++- src/map.rs | 11 +++++--- src/navigation.rs | 37 ++++++++++++++------------ src/pathfinding.rs | 25 +++++++---------- src/pitch_shift.rs | 13 ++++----- src/sound/footstep.rs | 11 ++++---- src/sound/icon.rs | 39 ++++++++++++++------------- src/sound/mod.rs | 4 +-- src/sound/volumetric.rs | 13 ++++----- src/visibility.rs | 26 ++++++++++-------- 13 files changed, 138 insertions(+), 146 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5686f16..7647300 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,12 +13,11 @@ speech_dispatcher_0_10 = ["bevy_tts/speech_dispatcher_0_10"] speech_dispatcher_0_11 = ["bevy_tts/speech_dispatcher_0_11"] [dependencies.bevy] -version = "0.9" +version = "0.10" default-features = false features = [ "bevy_gilrs", "bevy_winit", - "render", "x11", "wayland", "serialize", @@ -26,14 +25,14 @@ features = [ [dependencies] backtrace = "0.3" -bevy_rapier2d = "0.20" -bevy_synthizer = "0.1" -bevy_tts = { version = "0.4", default-features = false, features = ["tolk"] } +bevy_rapier2d = "0.21" +bevy_synthizer = "0.2" +bevy_tts = { version = "0.5", default-features = false, features = ["tolk"] } coord_2d = "0.3" futures-lite = "1" gilrs = "0.10" -here_be_dragons = "0.2" -leafwing-input-manager = { git = "https://github.com/ndarilek/leafwing-input-manager" } +here_be_dragons = { version = "0.3", features = ["serde"] } +leafwing-input-manager = "0.9" maze_generator = "2" once_cell = "1" pathfinding = "4" diff --git a/src/core.rs b/src/core.rs index 4ab55b5..d7e7dca 100644 --- a/src/core.rs +++ b/src/core.rs @@ -2,12 +2,11 @@ use std::{ cmp::{max, min}, f32::consts::PI, fmt::Display, - marker::PhantomData, ops::Sub, sync::RwLock, }; -use bevy::{app::PluginGroupBuilder, ecs::query::WorldQuery, prelude::*, utils::FloatOrd}; +use bevy::{app::PluginGroupBuilder, prelude::*, utils::FloatOrd}; use bevy_rapier2d::{ parry::query::{closest_points, distance, ClosestPoints}, prelude::*, @@ -657,23 +656,21 @@ fn sync_config(config: Res) { } } -pub struct CorePlugin { +pub struct CorePlugin { pub relative_direction_mode: RelativeDirectionMode, pub pixels_per_unit: u8, - pub rapier_user_data: PhantomData, } -impl Default for CorePlugin { +impl Default for CorePlugin { fn default() -> Self { Self { relative_direction_mode: RelativeDirectionMode::Directional, pixels_per_unit: 1, - rapier_user_data: default(), } } } -impl Plugin for CorePlugin { +impl Plugin for CorePlugin { fn build(&self, app: &mut App) { let config = CoreConfig { relative_direction_mode: self.relative_direction_mode, @@ -681,7 +678,7 @@ impl Plugin for CorePlugin() - .add_plugin(RapierPhysicsPlugin::::pixels_per_meter( + .add_plugin(RapierPhysicsPlugin::::pixels_per_meter( config.pixels_per_unit as f32, )) .add_startup_system(setup) @@ -689,21 +686,13 @@ impl Plugin for CorePlugin(PhantomData); +pub struct CorePlugins; -impl Default for CorePlugins { - fn default() -> Self { - Self(PhantomData) - } -} - -impl PluginGroup - for CorePlugins -{ +impl PluginGroup for CorePlugins { fn build(self) -> PluginGroupBuilder { PluginGroupBuilder::start::() .add(crate::bevy_tts::TtsPlugin) .add(crate::bevy_synthizer::SynthizerPlugin::default()) - .add(CorePlugin::::default()) + .add(CorePlugin::default()) } } diff --git a/src/exploration.rs b/src/exploration.rs index f4e7453..1dfff30 100644 --- a/src/exploration.rs +++ b/src/exploration.rs @@ -177,7 +177,7 @@ fn exploration_type_changed_announcement( focused: Query< ( &FocusedExplorationType, - ChangeTrackers>, + Ref>, ), Changed>, >, @@ -185,7 +185,7 @@ fn exploration_type_changed_announcement( where ExplorationType: Component + Default + Copy + Into, { - for (focused, changed) in focused.iter() { + for (focused, changed) in &focused { if changed.is_added() { return Ok(()); } @@ -371,17 +371,11 @@ fn cleanup( } } -#[derive(Resource, Clone, Debug)] +#[derive(Resource, Clone, Debug, Default)] struct ExplorationConfig { states: Vec, } -impl Default for ExplorationConfig { - fn default() -> Self { - Self { states: vec![] } - } -} - #[derive(Resource, Clone, Default)] pub struct ExplorationPlugin { pub states: Vec, @@ -392,7 +386,7 @@ pub struct ExplorationPlugin { impl Plugin for ExplorationPlugin where ExplorationType: 'static + Component + Default + Copy + Ord + PartialEq + Into, - State: 'static + Clone + Debug + Eq + Hash + Send + Sync, + State: States, MapData: 'static + Clone + Default + Send + Sync, { fn build(&self, app: &mut App) { @@ -408,33 +402,32 @@ where exploration_changed_announcement::.pipe(error_handler), ); if config.states.is_empty() { - app.add_system(exploration_focus::) - .add_system(exploration_type_focus::.pipe(error_handler)) - .add_system(exploration_type_change::.pipe(error_handler)) - .add_system(navigate_to_explored::) - .add_system_to_stage( - CoreStage::PostUpdate, - exploration_type_changed_announcement::.pipe(error_handler), - ); + app.add_systems(( + exploration_focus::, + exploration_type_focus::.pipe(error_handler), + exploration_type_change::.pipe(error_handler), + navigate_to_explored::, + )) + .add_system( + exploration_type_changed_announcement:: + .pipe(error_handler) + .in_base_set(CoreSet::PostUpdate), + ); } else { let states = config.states; for state in states { - app.add_system_set( - SystemSet::on_update(state.clone()) - .with_system(exploration_focus::) - .with_system( - exploration_type_focus::.pipe(error_handler), - ) - .with_system( - exploration_type_change::.pipe(error_handler), - ) - .with_system(navigate_to_explored::) - .with_system( - exploration_type_changed_announcement:: - .pipe(error_handler), - ), + app.add_systems( + ( + exploration_focus::, + exploration_type_focus::.pipe(error_handler), + exploration_type_change::.pipe(error_handler), + navigate_to_explored::, + exploration_type_changed_announcement:: + .pipe(error_handler), + ) + .in_set(OnUpdate(state.clone())), ) - .add_system_set(SystemSet::on_exit(state).with_system(cleanup)); + .add_system(cleanup.in_schedule(OnExit(state))); } } } diff --git a/src/log.rs b/src/log.rs index db0d110..74647f2 100644 --- a/src/log.rs +++ b/src/log.rs @@ -61,6 +61,10 @@ impl Plugin for LogPlugin { fn build(&self, app: &mut App) { app.register_type::() .add_startup_system(setup) - .add_system_to_stage(CoreStage::PostUpdate, read_log.pipe(error_handler)); + .add_system( + read_log + .pipe(error_handler) + .in_base_set(CoreSet::PostUpdate), + ); } } diff --git a/src/map.rs b/src/map.rs index be119b6..f50087a 100644 --- a/src/map.rs +++ b/src/map.rs @@ -328,8 +328,13 @@ impl Plugin for MapPlugin() - .add_system_to_stage(CoreStage::PreUpdate, spawn_colliders::) - .add_system_to_stage(CoreStage::PreUpdate, spawn_portals::) - .add_system_to_stage(CoreStage::PreUpdate, spawn_portal_colliders::); + .add_systems( + ( + spawn_colliders::, + spawn_portals::, + spawn_portal_colliders::, + ) + .in_base_set(CoreSet::PreUpdate), + ); } } diff --git a/src/navigation.rs b/src/navigation.rs index 9325ec5..c2da403 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -304,10 +304,10 @@ fn update_direction( fn remove_direction( mut commands: Commands, - removed: RemovedComponents, + mut removed: RemovedComponents, directions: Query<&CardinalDirection>, ) { - for entity in removed.iter() { + for entity in &mut removed { if directions.contains(entity) { commands.run_if_exists(entity, |mut entity| { entity.remove::(); @@ -319,7 +319,7 @@ fn remove_direction( fn speak_direction( mut tts: ResMut, player: Query< - (&CardinalDirection, ChangeTrackers), + (&CardinalDirection, Ref), (With, Changed), >, ) -> Result<(), Box> { @@ -384,7 +384,8 @@ fn log_area_descriptions( } } -pub const MOVEMENT_CONTROLS: &str = "MOVEMENT_CONTROLS"; +#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)] +pub struct MovementControls; #[derive(Resource, Clone, Debug)] pub struct NavigationPlugin { @@ -405,7 +406,7 @@ impl Default for NavigationPlugin { impl Plugin for NavigationPlugin where - State: 'static + Clone + Copy + Debug + Eq + Hash + Send + Sync, + State: States, { fn build(&self, app: &mut App) { app.insert_resource(self.clone()); @@ -416,21 +417,23 @@ where .register_type::() .register_type::() .add_plugin(InputManagerPlugin::::default()) - .add_system_to_stage(CoreStage::PreUpdate, update_direction) - .add_system_to_stage(CoreStage::PostUpdate, remove_direction) - .add_system(tick_snap_timers) - .add_system(speak_direction.pipe(error_handler)) - .add_system(add_speed) - .add_system_to_stage(CoreStage::PostUpdate, log_area_descriptions::); + .add_system(update_direction.in_base_set(CoreSet::PreUpdate)) + .add_systems( + (remove_direction, log_area_descriptions::).in_base_set(CoreSet::PostUpdate), + ) + .add_systems(( + tick_snap_timers, + speak_direction.pipe(error_handler), + add_speed, + )); if self.states.is_empty() { - app.add_system(controls.label(MOVEMENT_CONTROLS)) - .add_system(snap.pipe(error_handler).before(MOVEMENT_CONTROLS)); + app.add_systems((controls.in_set(MovementControls), snap.pipe(error_handler)).chain()); } else { for state in &self.states { - app.add_system_set( - SystemSet::on_update(*state) - .with_system(controls.label(MOVEMENT_CONTROLS)) - .with_system(snap.pipe(error_handler).before(MOVEMENT_CONTROLS)), + app.add_systems( + (controls.in_set(MovementControls), snap.pipe(error_handler)) + .chain() + .in_set(OnUpdate(state.clone())), ); } } diff --git a/src/pathfinding.rs b/src/pathfinding.rs index d9a9f2a..ce5c047 100644 --- a/src/pathfinding.rs +++ b/src/pathfinding.rs @@ -261,9 +261,9 @@ fn poll_tasks(mut commands: Commands, mut query: Query<(Entity, &mut Calculating fn remove_destination( mut commands: Commands, entities: &Entities, - removed: RemovedComponents, + mut removed: RemovedComponents, ) { - for entity in removed.iter() { + for entity in &mut removed { if entities.contains(entity) { commands.entity(entity).remove::(); } @@ -324,7 +324,7 @@ fn negotiate_path( start, transform.yaw().radians(), direction, - &*collider, + collider, rapier_context.integration_parameters.dt, QueryFilter::new() .exclude_sensors() @@ -408,18 +408,13 @@ impl Plugin for PathfindingPlugin { .register_type::() .register_type::() .register_type::() - .add_system_to_stage(CoreStage::PreUpdate, calculate_path) - .add_system_to_stage(CoreStage::PostUpdate, remove_destination) - .add_system_to_stage(CoreStage::PreUpdate, poll_tasks) - .add_system_to_stage( - CoreStage::PreUpdate, - negotiate_path.after(InputManagerSystem::Tick), + .add_systems((negotiate_path, poll_tasks).in_base_set(CoreSet::PreUpdate)) + .add_systems( + (actions, calculate_path) + .chain() + .in_base_set(CoreSet::PreUpdate) + .after(InputManagerSystem::Tick), ) - .add_system_to_stage( - CoreStage::PreUpdate, - actions - .after(InputManagerSystem::Update) - .before(calculate_path), - ); + .add_system(remove_destination.in_base_set(CoreSet::PostUpdate)); } } diff --git a/src/pitch_shift.rs b/src/pitch_shift.rs index 5eed9ca..8c20f69 100644 --- a/src/pitch_shift.rs +++ b/src/pitch_shift.rs @@ -76,12 +76,12 @@ fn behind_removed( config: Res, mut last_icon_pitch: ResMut, mut last_sound_pitch: ResMut, - removed: RemovedComponents, + mut removed: RemovedComponents, mut icons: Query<&mut SoundIcon>, mut sounds: Query<&mut Sound>, ) { let downshift = 1. / config.downshift; - for entity in removed.iter() { + for entity in &mut removed { if let Ok(mut icon) = icons.get_mut(entity) { icon.pitch *= downshift; last_icon_pitch.remove(&entity); @@ -161,11 +161,8 @@ impl Plugin for PitchShiftPlugin { .register_type::() .init_resource::() .init_resource::() - .add_system_to_stage(CoreStage::PreUpdate, tag_behind) - .add_system(behind_added) - .add_system_to_stage(CoreStage::PostUpdate, behind_removed) - .add_system(sound_icon_changed) - .add_system(sound_changed) - .add_system(sync_config); + .add_system(tag_behind.in_base_set(CoreSet::PreUpdate)) + .add_systems((behind_added, sound_icon_changed, sound_changed, sync_config)) + .add_system(behind_removed.in_base_set(CoreSet::PostUpdate)); } } diff --git a/src/sound/footstep.rs b/src/sound/footstep.rs index 1042e38..625c8b3 100644 --- a/src/sound/footstep.rs +++ b/src/sound/footstep.rs @@ -33,7 +33,7 @@ fn added(mut commands: Commands, footsteps: Query<(Entity, &Footstep), Added() - .add_system_to_stage(CoreStage::PreUpdate, added) - .add_system_to_stage( - CoreStage::PostUpdate, - update.after(TransformSystem::TransformPropagate), + .add_system(added.in_base_set(CoreSet::PreUpdate)) + .add_system( + update + .after(TransformSystem::TransformPropagate) + .in_base_set(CoreSet::PostUpdate), ); } } diff --git a/src/sound/icon.rs b/src/sound/icon.rs index 129afae..f0f6711 100644 --- a/src/sound/icon.rs +++ b/src/sound/icon.rs @@ -1,7 +1,7 @@ -use std::{fmt::Debug, hash::Hash, time::Duration}; +use std::{fmt::Debug, time::Duration}; use bevy::prelude::*; -use bevy_synthizer::{Buffer, Sound}; +use bevy_synthizer::{Audio, Buffer, Sound}; use rand::random; @@ -46,7 +46,7 @@ fn added(mut commands: Commands, icons: Query<(Entity, &SoundIcon), Added( &mut Sound, )>, ) where - S: 'static + Clone + Debug + Eq + Hash + Send + Sync, + S: States, { - if !config.states.is_empty() && !config.states.contains(state.current()) { + if !config.states.is_empty() && !config.states.contains(&state.0) { return; } for visible in viewers.iter() { @@ -97,8 +97,9 @@ fn update( } } let buffer = icon.buffer.clone(); - if sound.buffer != buffer { - sound.buffer = buffer; + let audio: Audio = buffer.into(); + if sound.audio != audio { + sound.audio = audio; } sound.gain = icon.gain; sound.pitch = icon.pitch; @@ -132,12 +133,12 @@ fn exploration_focus_changed( } fn exploration_focus_removed( - removed: RemovedComponents, + mut removed: RemovedComponents, mut query: Query<&mut SoundIcon>, children: Query<&Children>, ) { const ICON_GAIN: f64 = 3.; - for entity in removed.iter() { + for entity in &mut removed { if let Ok(mut icon) = query.get_mut(entity) { icon.gain /= ICON_GAIN; } @@ -196,21 +197,23 @@ impl Default for SoundIconPlugin { impl Plugin for SoundIconPlugin where - S: 'static + Clone + Debug + Eq + Hash + Send + Sync, + S: States, { fn build(&self, app: &mut App) { app.insert_resource(self.clone()) .register_type::() .add_system(added) - .add_system_to_stage(CoreStage::PostUpdate, update::) - .add_system_to_stage( - CoreStage::PostUpdate, - exploration_focus_changed.after(update::), + .add_system(update::.in_base_set(CoreSet::PostUpdate)) + .add_system( + exploration_focus_changed + .in_base_set(CoreSet::PostUpdate) + .after(update::), ) - .add_system_to_stage( - CoreStage::PostUpdate, - exploration_focus_removed.after(exploration_focus_changed), + .add_system( + exploration_focus_removed + .in_base_set(CoreSet::PostUpdate) + .after(exploration_focus_changed), ) - .add_system_to_stage(CoreStage::PostUpdate, reset_timer_on_visibility_gain); + .add_system(reset_timer_on_visibility_gain.in_base_set(CoreSet::PostUpdate)); } } diff --git a/src/sound/mod.rs b/src/sound/mod.rs index 9944630..fccd4c2 100644 --- a/src/sound/mod.rs +++ b/src/sound/mod.rs @@ -1,5 +1,3 @@ -use std::{fmt::Debug, hash::Hash}; - use bevy::{app::PluginGroupBuilder, prelude::*}; pub mod footstep; @@ -30,7 +28,7 @@ impl<'a, S> Default for SoundPlugins<'a, S> { impl PluginGroup for SoundPlugins<'static, S> where - S: 'static + Clone + Debug + Eq + Hash + Send + Sync, + S: States, { fn build(self) -> PluginGroupBuilder { PluginGroupBuilder::start::() diff --git a/src/sound/volumetric.rs b/src/sound/volumetric.rs index 8a6231b..35361d5 100644 --- a/src/sound/volumetric.rs +++ b/src/sound/volumetric.rs @@ -39,8 +39,8 @@ fn update( } } -fn removed(mut commands: Commands, removed: RemovedComponents) { - for entity in removed.iter() { +fn removed(mut commands: Commands, mut removed: RemovedComponents) { + for entity in &mut removed { commands.run_if_exists(entity, |mut entity| { entity.insert(TransformBundle::default()); }); @@ -52,10 +52,11 @@ pub struct VolumetricPlugin; impl Plugin for VolumetricPlugin { fn build(&self, app: &mut App) { app.register_type::() - .add_system_to_stage( - CoreStage::PostUpdate, - update.before(TransformSystem::TransformPropagate), + .add_system( + update + .in_base_set(CoreSet::PostUpdate) + .before(TransformSystem::TransformPropagate), ) - .add_system_to_stage(CoreStage::PostUpdate, removed); + .add_system(removed.in_base_set(CoreSet::PostUpdate)); } } diff --git a/src/visibility.rs b/src/visibility.rs index 5e23886..c4dfe1f 100644 --- a/src/visibility.rs +++ b/src/visibility.rs @@ -262,11 +262,11 @@ fn add_visibility_indices( } fn viewshed_removed( - query: RemovedComponents, + mut query: RemovedComponents, visible_entities: Query<&VisibleEntities>, mut events: EventWriter, ) { - for entity in query.iter() { + for entity in &mut query { if let Ok(visible) = visible_entities.get(entity) { for e in visible.iter() { events.send(VisibilityChanged::Lost { @@ -330,7 +330,7 @@ fn update_viewshed( } fn remove_visible( - removed: RemovedComponents, + mut removed: RemovedComponents, mut viewers: Query<( Entity, &mut Viewshed, @@ -342,9 +342,9 @@ fn remove_visible( obstructions: Query<&MapObstruction>, mut changed: EventWriter, ) { - if removed.iter().len() != 0 { + if !removed.is_empty() { let mut cache = HashMap::new(); - for removed in removed.iter() { + for removed in &mut removed { for (viewer_entity, mut viewshed, mut visible_entities, start) in viewers.iter_mut() { if !visible_entities.contains(&removed) { continue; @@ -441,11 +441,15 @@ impl Default for VisibilityPlu impl Plugin for VisibilityPlugin { fn build(&self, app: &mut App) { app.add_event::() - .add_system_to_stage(CoreStage::PreUpdate, add_visibility_indices::) - .add_system_to_stage(CoreStage::PreUpdate, update_viewshed) - .add_system_to_stage(CoreStage::PostUpdate, viewshed_removed) - .add_system_to_stage(CoreStage::PostUpdate, remove_visible) - .add_system_to_stage(CoreStage::PreUpdate, update_revealed_tiles::) - .add_system_to_stage(CoreStage::PreUpdate, log_visible); + .add_systems( + ( + add_visibility_indices::, + update_viewshed, + update_revealed_tiles::, + log_visible, + ) + .in_base_set(CoreSet::PreUpdate), + ) + .add_systems((viewshed_removed, remove_visible).in_base_set(CoreSet::PostUpdate)); } }