From f255239517b7c37f57e5f5090c8ec4ec1b948ea9 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 20 Dec 2022 09:15:09 -0600 Subject: [PATCH] Make many plugins own their own config. --- src/core.rs | 32 ++++++++++++------------ src/exploration.rs | 31 ++++++++--------------- src/map.rs | 32 ++++++++++++------------ src/navigation.rs | 61 +++++++++++++++------------------------------- src/pitch_shift.rs | 46 +++++++++++++++++----------------- src/sound/icon.rs | 17 ++++++------- src/visibility.rs | 18 +++++++------- 7 files changed, 101 insertions(+), 136 deletions(-) diff --git a/src/core.rs b/src/core.rs index fb446ec..08f271a 100644 --- a/src/core.rs +++ b/src/core.rs @@ -650,15 +650,6 @@ pub struct CoreConfig { pub pixels_per_unit: u8, } -impl Default for CoreConfig { - fn default() -> Self { - Self { - relative_direction_mode: RelativeDirectionMode::Directional, - pixels_per_unit: 1, - } - } -} - fn sync_config(config: Res) { if config.is_changed() { let mut mode = RELATIVE_DIRECTION_MODE.write().unwrap(); @@ -666,21 +657,30 @@ fn sync_config(config: Res) { } } -pub struct CorePlugin(PhantomData); +pub struct CorePlugin { + pub relative_direction_mode: RelativeDirectionMode, + pub pixels_per_unit: u8, + pub rapier_user_data: PhantomData, +} impl Default for CorePlugin { fn default() -> Self { - Self(PhantomData) + Self { + relative_direction_mode: RelativeDirectionMode::Directional, + pixels_per_unit: 1, + rapier_user_data: default(), + } } } impl Plugin for CorePlugin { fn build(&self, app: &mut App) { - if !app.world.contains_resource::() { - app.insert_resource(CoreConfig::default()); - } - let config = *app.world.get_resource::().unwrap(); - app.register_type::() + let config = CoreConfig { + relative_direction_mode: self.relative_direction_mode, + pixels_per_unit: self.pixels_per_unit, + }; + app.insert_resource(config) + .register_type::() .add_plugin(RapierPhysicsPlugin::::pixels_per_meter( config.pixels_per_unit as f32, )) diff --git a/src/exploration.rs b/src/exploration.rs index 53c4ebd..8f5359c 100644 --- a/src/exploration.rs +++ b/src/exploration.rs @@ -382,18 +382,11 @@ impl Default for ExplorationConfig { } } -pub struct ExplorationPlugin( - PhantomData, - PhantomData, - PhantomData, -); - -impl Default - for ExplorationPlugin -{ - fn default() -> Self { - Self(PhantomData, PhantomData, PhantomData) - } +#[derive(Resource, Clone, Default)] +pub struct ExplorationPlugin { + states: Vec, + exploration_type: PhantomData, + map_data: PhantomData, } impl Plugin for ExplorationPlugin @@ -403,15 +396,11 @@ where MapData: 'static + Clone + Default + Send + Sync, { fn build(&self, app: &mut App) { - if !app.world.contains_resource::>() { - app.insert_resource(ExplorationConfig::::default()); - } - let config = app - .world - .get_resource::>() - .unwrap() - .clone(); - app.register_type::() + let config = ExplorationConfig { + states: self.states.clone(), + }; + app.insert_resource(config.clone()) + .register_type::() .register_type::() .register_type::() .add_plugin(InputManagerPlugin::::default()) diff --git a/src/map.rs b/src/map.rs index add8b12..be119b6 100644 --- a/src/map.rs +++ b/src/map.rs @@ -78,11 +78,6 @@ impl ITileType for Tile { } } -#[derive(Resource, Clone, Debug, Default)] -pub struct MapConfig { - pub start_revealed: bool, -} - #[derive(Bundle, Default)] pub struct PortalBundle { pub portal: Portal, @@ -314,22 +309,27 @@ fn spawn_portal_colliders( } } -pub struct MapPlugin(PhantomData); +#[derive(Resource, Clone, Copy, Debug)] +pub struct MapPlugin { + pub start_revealed: bool, + pub map_data: PhantomData, +} -impl Default for MapPlugin { +impl Default for MapPlugin { fn default() -> Self { - Self(Default::default()) + Self { + start_revealed: default(), + map_data: default(), + } } } -impl Plugin for MapPlugin { +impl Plugin for MapPlugin { fn build(&self, app: &mut App) { - if !app.world.contains_resource::() { - app.insert_resource(MapConfig::default()); - } - app.register_type::() - .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::); + app.insert_resource(self.clone()) + .register_type::() + .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::); } } diff --git a/src/navigation.rs b/src/navigation.rs index 8c712a9..7acc451 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -1,7 +1,4 @@ -use std::{ - collections::HashMap, error::Error, f32::consts::PI, fmt::Debug, hash::Hash, - marker::PhantomData, -}; +use std::{collections::HashMap, error::Error, f32::consts::PI, fmt::Debug, hash::Hash}; use bevy::prelude::*; use bevy_rapier2d::prelude::*; @@ -61,9 +58,9 @@ impl Default for SnapTimer { #[derive(Resource, Default, Deref, DerefMut)] struct SnapTimers(HashMap); -fn movement_controls( +fn movement_controls( mut commands: Commands, - config: Res>, + config: Res>, snap_timers: Res, mut query: Query<( Entity, @@ -76,7 +73,7 @@ fn movement_controls( )>, exploration_focused: Query>, ) where - S: 'static + Clone + Debug + Eq + Hash + Send + Sync, + State: 'static + Clone + Debug + Eq + Hash + Send + Sync, { for (entity, mut actions, mut velocity, mut speed, max_speed, rotation_speed, transform) in &mut query @@ -295,15 +292,14 @@ fn remove_speed(removed: RemovedComponents, mut query: Query<&mut Velocit } } -fn log_area_descriptions( +fn log_area_descriptions( mut events: EventReader, areas: Query<(&Area, Option<&Name>)>, players: Query<&Player>, - config: Res>, + config: Res>, mut log: Query<&mut Log>, ) where - S: 'static + Send + Sync, - A: 'static + Send + Sync, + State: 'static + Send + Sync, { if !config.log_area_descriptions { return; @@ -340,52 +336,36 @@ fn log_area_descriptions( } #[derive(Resource, Clone, Debug)] -pub struct NavigationConfig { +pub struct NavigationPlugin { pub forward_movement_factor: f32, pub backward_movement_factor: f32, pub strafe_movement_factor: f32, pub sprint_movement_factor: f32, - pub movement_control_states: Vec, + pub states: Vec, pub describe_undescribed_areas: bool, pub log_area_descriptions: bool, } -impl Default for NavigationConfig { +impl Default for NavigationPlugin { fn default() -> Self { Self { forward_movement_factor: 1., backward_movement_factor: 1., strafe_movement_factor: 1., sprint_movement_factor: 3., - movement_control_states: vec![], + states: vec![], describe_undescribed_areas: false, log_area_descriptions: true, } } } -pub struct NavigationPlugin<'a, S, A>(PhantomData<&'a S>, PhantomData<&'a A>); - -impl<'a, S, A> Default for NavigationPlugin<'a, S, A> { - fn default() -> Self { - Self(PhantomData, PhantomData) - } -} - -impl Plugin for NavigationPlugin<'static, S, A> +impl Plugin for NavigationPlugin where - S: 'static + Clone + Copy + Debug + Eq + Hash + Send + Sync, - A: Hash + Eq + Copy + Send + Sync, + State: 'static + Clone + Copy + Debug + Eq + Hash + Send + Sync, { fn build(&self, app: &mut App) { - if !app.world.contains_resource::>() { - app.insert_resource(NavigationConfig::::default()); - } - let config = app - .world - .get_resource::>() - .unwrap() - .clone(); + app.insert_resource(self.clone()); app.init_resource::() .register_type::() .register_type::() @@ -398,21 +378,20 @@ where .add_system(add_speed) .add_system(limit_speed) .add_system_to_stage(CoreStage::PostUpdate, remove_speed) - .add_system_to_stage(CoreStage::PostUpdate, log_area_descriptions::); + .add_system_to_stage(CoreStage::PostUpdate, log_area_descriptions::); const MOVEMENT_CONTROLS: &str = "MOVEMENT_CONTROLS"; - if config.movement_control_states.is_empty() { + if self.states.is_empty() { app.add_system( - movement_controls:: + movement_controls:: .label(MOVEMENT_CONTROLS) .after(limit_speed), ) .add_system(snap.pipe(error_handler).before(MOVEMENT_CONTROLS)); } else { - let states = config.movement_control_states; - for state in states { + for state in &self.states { app.add_system_set( - SystemSet::on_update(state) - .with_system(movement_controls::.label(MOVEMENT_CONTROLS)) + SystemSet::on_update(*state) + .with_system(movement_controls::.label(MOVEMENT_CONTROLS)) .with_system(snap.pipe(error_handler).before(MOVEMENT_CONTROLS)), ); } diff --git a/src/pitch_shift.rs b/src/pitch_shift.rs index cdac35c..5eed9ca 100644 --- a/src/pitch_shift.rs +++ b/src/pitch_shift.rs @@ -12,24 +12,9 @@ use crate::{ #[reflect(Component)] struct Behind; -#[derive(Resource, Clone, Copy, Debug)] -pub struct PitchShiftConfig { - pub downshift_behind: bool, - pub downshift: f64, -} - -impl Default for PitchShiftConfig { - fn default() -> Self { - Self { - downshift_behind: false, - downshift: 0.95, - } - } -} - fn tag_behind( mut commands: Commands, - config: Res, + config: Res, sounds: Query<(Entity, &GlobalTransform, Option<&Sound>, Option<&SoundIcon>)>, listener: Query<&GlobalTransform, With>, behind: Query>, @@ -71,7 +56,7 @@ struct LastIconPitch(HashMap); struct LastSoundPitch(HashMap); fn behind_added( - config: Res, + config: Res, mut last_icon_pitch: ResMut, mut last_sound_pitch: ResMut, mut query: Query<(Entity, Option<&mut SoundIcon>, Option<&mut Sound>), Added>, @@ -88,7 +73,7 @@ fn behind_added( } fn behind_removed( - config: Res, + config: Res, mut last_icon_pitch: ResMut, mut last_sound_pitch: ResMut, removed: RemovedComponents, @@ -108,7 +93,7 @@ fn behind_removed( } fn sound_icon_changed( - config: Res, + config: Res, mut last_icon_pitch: ResMut, mut icons: Query<(Entity, &mut SoundIcon), (With, Changed)>, ) { @@ -126,7 +111,7 @@ fn sound_icon_changed( } fn sound_changed( - config: Res, + config: Res, mut last_sound_pitch: ResMut, mut sounds: Query<(Entity, &mut Sound), (With, Without, Changed)>, ) { @@ -145,7 +130,7 @@ fn sound_changed( fn sync_config( mut commands: Commands, - config: Res, + config: Res, behind: Query>, ) { if config.is_changed() { @@ -155,12 +140,25 @@ fn sync_config( } } -pub struct PitchShiftPlugin; +#[derive(Resource, Clone, Copy, Debug)] +pub struct PitchShiftPlugin { + pub downshift_behind: bool, + pub downshift: f64, +} + +impl Default for PitchShiftPlugin { + fn default() -> Self { + Self { + downshift_behind: false, + downshift: 0.95, + } + } +} impl Plugin for PitchShiftPlugin { fn build(&self, app: &mut App) { - app.register_type::() - .init_resource::() + app.insert_resource(*self) + .register_type::() .init_resource::() .init_resource::() .add_system_to_stage(CoreStage::PreUpdate, tag_behind) diff --git a/src/sound/icon.rs b/src/sound/icon.rs index 18ee721..129afae 100644 --- a/src/sound/icon.rs +++ b/src/sound/icon.rs @@ -58,7 +58,7 @@ fn added(mut commands: Commands, icons: Query<(Entity, &SoundIcon), Added( - config: Res>, + config: Res>, state: Res>, time: Res