From 4718d51624cddd67d60e7a77c30aa25dcb759ead Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 20 Dec 2022 07:56:43 -0600 Subject: [PATCH] Cleanup of exploration plugin. --- src/exploration.rs | 133 ++++++++++++++++++++++++--------------------- 1 file changed, 70 insertions(+), 63 deletions(-) diff --git a/src/exploration.rs b/src/exploration.rs index e487ede..53c4ebd 100644 --- a/src/exploration.rs +++ b/src/exploration.rs @@ -49,21 +49,21 @@ where #[reflect(Component)] pub struct Mappable; -fn exploration_type_change( +fn exploration_type_change( mut tts: ResMut, mut explorers: Query<( &ActionState, &VisibleEntities, - &mut FocusedExplorationType, + &mut FocusedExplorationType, )>, - features: Query<&T>, + features: Query<&ExplorationType>, ) -> Result<(), Box> where - T: Component + Default + Copy + Ord, - S: 'static + Clone + Debug + Eq + Hash + Send + Sync, + ExplorationType: Component + Default + Copy + Ord, + State: 'static + Clone + Debug + Eq + Hash + Send + Sync, { for (actions, visible, mut focused) in explorers.iter_mut() { - let mut types: Vec = vec![]; + let mut types: Vec = vec![]; for e in visible.iter() { if let Ok(t) = features.get(*e) { types.push(*t); @@ -112,28 +112,28 @@ where Ok(()) } -fn exploration_type_focus( +fn exploration_type_focus( mut commands: Commands, mut tts: ResMut, explorers: Query<( Entity, &ActionState, &VisibleEntities, - &FocusedExplorationType, + &FocusedExplorationType, Option<&Exploring>, )>, - features: Query<(Entity, &Transform, &T)>, + features: Query<(Entity, &Transform, &ExplorationType)>, ) -> Result<(), Box> where - T: Component + Default + PartialEq, - S: 'static + Clone + Debug + Eq + Hash + Send + Sync, + ExplorationType: Component + Default + PartialEq, + State: 'static + Clone + Debug + Eq + Hash + Send + Sync, { for (entity, actions, visible_entities, focused_type, exploring) in explorers.iter() { let mut features = features .iter() .filter(|v| visible_entities.contains(&v.0)) .map(|v| (v.1.floor(), v.2)) - .collect::>(); + .collect::>(); if features.is_empty() { tts.speak("Nothing visible.", true)?; return Ok(()); @@ -142,7 +142,7 @@ where if let Some(focused) = &focused_type.0 { features.retain(|(_, t)| **t == *focused); } - let mut target: Option<&((f32, f32), &T)> = None; + let mut target: Option<&((f32, f32), &ExplorationType)> = None; if actions.just_pressed(ExplorationAction::FocusNext) { if let Some(exploring) = exploring { target = features.iter().find(|(c, _)| *c > **exploring); @@ -172,18 +172,18 @@ where Ok(()) } -fn exploration_type_changed_announcement( +fn exploration_type_changed_announcement( mut tts: ResMut, focused: Query< ( - &FocusedExplorationType, - ChangeTrackers>, + &FocusedExplorationType, + ChangeTrackers>, ), - Changed>, + Changed>, >, ) -> Result<(), Box> where - T: Component + Default + Copy + Into, + ExplorationType: Component + Default + Copy + Into, { for (focused, changed) in focused.iter() { if changed.is_added() { @@ -202,9 +202,9 @@ where Ok(()) } -fn exploration_focus( +fn exploration_focus( mut commands: Commands, - map: Query<&Map>, + map: Query<&Map>, explorers: Query< ( Entity, @@ -215,7 +215,8 @@ fn exploration_focus( With, >, ) where - S: 'static + Clone + Debug + Eq + Hash + Send + Sync, + State: 'static + Clone + Debug + Eq + Hash + Send + Sync, + MapData: 'static + Clone + Default + Send + Sync, { for (entity, actions, transform, exploring) in explorers.iter() { let coordinates = transform.translation; @@ -251,12 +252,13 @@ fn exploration_focus( } } -fn navigate_to_explored( +fn navigate_to_explored( mut commands: Commands, - map: Query<(&Map, &RevealedTiles)>, + map: Query<(&Map, &RevealedTiles)>, explorers: Query<(Entity, &ActionState, &Exploring)>, ) where - S: 'static + Clone + Debug + Eq + Hash + Send + Sync, + State: 'static + Clone + Debug + Eq + Hash + Send + Sync, + MapData: 'static + Clone + Default + Send + Sync, { for (entity, actions, exploring) in explorers.iter() { for (map, revealed_tiles) in map.iter() { @@ -272,21 +274,21 @@ fn navigate_to_explored( } } -fn exploration_changed_announcement( +fn exploration_changed_announcement( mut commands: Commands, mut tts: ResMut, - map: Query<(&Map, &RevealedTiles)>, + map: Query<(&Map, &RevealedTiles)>, explorer: Query<(&Transform, &Exploring, &Viewshed), Changed>, focused: Query>, explorable: Query, With)>>, names: Query<&Name>, - types: Query<&T>, + types: Query<&ExplorationType>, mappables: Query<&Mappable>, rapier_context: Res, ) -> Result<(), Box> where - T: Component + Copy + Into, - D: 'static + Clone + Default + Send + Sync, + ExplorationType: Component + Copy + Into, + MapData: 'static + Clone + Default + Send + Sync, { if let Ok((coordinates, exploring, viewshed)) = explorer.get_single() { let coordinates = coordinates.floor(); @@ -370,72 +372,77 @@ fn cleanup( } #[derive(Resource, Clone, Debug)] -pub struct ExplorationConfig { - pub exploration_control_states: Vec, +pub struct ExplorationConfig { + pub states: Vec, } -impl Default for ExplorationConfig { +impl Default for ExplorationConfig { fn default() -> Self { - Self { - exploration_control_states: vec![], - } + Self { states: vec![] } } } -pub struct ExplorationPlugin<'a, T, S: 'static, A: 'static, D>( - PhantomData, - PhantomData<&'a S>, - PhantomData<&'static A>, - PhantomData, +pub struct ExplorationPlugin( + PhantomData, + PhantomData, + PhantomData, ); -impl Default for ExplorationPlugin<'static, S, A, D, T> { +impl Default + for ExplorationPlugin +{ fn default() -> Self { - Self(PhantomData, PhantomData, PhantomData, PhantomData) + Self(PhantomData, PhantomData, PhantomData) } } -impl Plugin for ExplorationPlugin<'static, T, S, A, D> +impl Plugin for ExplorationPlugin where - T: 'static + Component + Default + Copy + Ord + PartialEq + Into, - S: Clone + Debug + Eq + Hash + Send + Sync, - A: Hash + Eq + Clone + Send + Sync, - D: 'static + Clone + Default + Send + Sync, + ExplorationType: 'static + Component + Default + Copy + Ord + PartialEq + Into, + State: 'static + Clone + Debug + Eq + Hash + Send + Sync, + MapData: 'static + Clone + Default + Send + Sync, { fn build(&self, app: &mut App) { - if !app.world.contains_resource::>() { - app.insert_resource(ExplorationConfig::::default()); + if !app.world.contains_resource::>() { + app.insert_resource(ExplorationConfig::::default()); } let config = app .world - .get_resource::>() + .get_resource::>() .unwrap() .clone(); app.register_type::() .register_type::() .register_type::() .add_plugin(InputManagerPlugin::::default()) - .add_system(exploration_changed_announcement::.pipe(error_handler)); - if config.exploration_control_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( + 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), + exploration_type_changed_announcement::.pipe(error_handler), ); } else { - let states = config.exploration_control_states; + 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_focus::) .with_system( - exploration_type_changed_announcement::.pipe(error_handler), + 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), ), ) .add_system_set(SystemSet::on_exit(state).with_system(cleanup));