From e90a85641bd52ea2dafe0c08efce447f2ebd5c88 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 21 Sep 2021 15:57:47 -0500 Subject: [PATCH] Begin updating exploration plugin. --- src/exploration.rs | 141 +++++++++++++++++++++++++++++++-------------- 1 file changed, 98 insertions(+), 43 deletions(-) diff --git a/src/exploration.rs b/src/exploration.rs index a70e45c..22129cd 100644 --- a/src/exploration.rs +++ b/src/exploration.rs @@ -1,4 +1,4 @@ -use std::{error::Error, hash::Hash, marker::PhantomData}; +use std::{error::Error, fmt::Debug, hash::Hash, marker::PhantomData}; use bevy::prelude::*; use bevy_input_actionmap::InputMap; @@ -70,14 +70,15 @@ pub struct FocusedExplorationType(pub Option); #[reflect(Component)] pub struct Mappable; -fn exploration_type_change( - config: Res>, +fn exploration_type_change( + config: Res>, mut tts: ResMut, input: Res>, mut explorers: Query<(&Player, &Viewshed, &mut FocusedExplorationType)>, features: Query<(&Coordinates, &ExplorationType)>, ) -> Result<(), Box> where + S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash, A: Hash + Eq + Clone + Send + Sync, { if let (Some(select_next_type), Some(select_prev_type)) = ( @@ -143,9 +144,9 @@ where Ok(()) } -fn exploration_type_focus( +fn exploration_type_focus( mut commands: Commands, - config: Res>, + config: Res>, input: Res>, mut tts: ResMut, explorers: Query<( @@ -158,6 +159,7 @@ fn exploration_type_focus( features: Query<(&Coordinates, &ExplorationType)>, ) -> Result<(), Box> where + S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash, A: Hash + Eq + Clone + Send + Sync, { if let (Some(explore_focus_next), Some(explore_focus_prev)) = ( @@ -243,13 +245,14 @@ fn exploration_type_changed_announcement( Ok(()) } -fn exploration_focus( +fn exploration_focus( mut commands: Commands, - config: Res>, + config: Res>, input: Res>, map: Query<&Map>, - explorers: Query<(Entity, &Player, &Coordinates, Option<&Exploring>)>, + explorers: Query<(Entity, &Coordinates, Option<&Exploring>), With>, ) where + S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash, A: Hash + Eq + Clone + Send + Sync, { if let ( @@ -263,10 +266,13 @@ fn exploration_focus( config.action_explore_left.clone(), config.action_explore_right.clone(), ) { + println!("Here1"); for map in map.iter() { - for (entity, _, coordinates, exploring) in explorers.iter() { - let coordinates = **coordinates; - let coordinates = (coordinates.0.floor(), coordinates.1.floor()); + println!("Here2"); + if let Ok((entity, coordinates, exploring)) = explorers.single() { + println!("Here"); + let coordinates = coordinates.i32(); + let coordinates = (coordinates.x() + 0.5, coordinates.y() + 0.5); let mut exploring = if let Some(exploring) = exploring { **exploring } else { @@ -295,13 +301,14 @@ fn exploration_focus( } } -fn navigate_to_explored( +fn navigate_to_explored( mut commands: Commands, - config: Res>, + config: Res>, input: Res>, map: Query<(&Map, &RevealedTiles)>, explorers: Query<(Entity, &Exploring)>, ) where + S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash, A: Hash + Eq + Clone + Send + Sync, { if let Some(navigate_to_explored) = config.action_navigate_to_explored.clone() { @@ -324,15 +331,15 @@ fn exploration_changed_announcement( mut commands: Commands, mut tts: ResMut, map: Query<(&Map, &RevealedTiles, &VisibleTiles)>, - explorers: Query<(&Coordinates, &Exploring), Changed>, - focused: Query<(Entity, &ExplorationFocused)>, + explorer: Query<(&Coordinates, &Exploring), Changed>, + focused: Query>, names: Query<&Name>, types: Query<&ExplorationType>, mappables: Query<&Mappable>, query_pipeline: Res, collider_query: QueryPipelineColliderComponentsQuery, ) -> Result<(), Box> { - for (coordinates, exploring) in explorers.iter() { + if let Ok((coordinates, exploring)) = explorer.single() { let collider_set = QueryPipelineColliderComponentsSet(&collider_query); let coordinates = **coordinates; let coordinates = (coordinates.0.floor(), coordinates.1.floor()); @@ -345,7 +352,7 @@ fn exploration_changed_announcement( let fog_of_war = known && !visible; let description = if known { let mut tokens: Vec<&str> = vec![]; - for (entity, _) in focused.iter() { + for entity in focused.iter() { commands.entity(entity).remove::(); } let shape_pos = (Vec2::new(exploring.x(), exploring.y()), 0.); @@ -396,8 +403,22 @@ fn exploration_changed_announcement( Ok(()) } +fn cleanup( + mut commands: Commands, + explorers: Query>, + focus: Query>, +) { + for entity in explorers.iter() { + commands.entity(entity).remove::(); + } + for entity in focus.iter() { + commands.entity(entity).despawn_recursive(); + } +} + #[derive(Clone, Debug)] -pub struct ExplorationConfig { +pub struct ExplorationConfig { + pub exploration_control_states: Vec, pub action_explore_forward: Option, pub action_explore_backward: Option, pub action_explore_left: Option, @@ -409,9 +430,10 @@ pub struct ExplorationConfig { pub action_navigate_to_explored: Option, } -impl Default for ExplorationConfig { +impl Default for ExplorationConfig { fn default() -> Self { Self { + exploration_control_states: vec![], action_explore_forward: None, action_explore_backward: None, action_explore_left: None, @@ -425,49 +447,82 @@ impl Default for ExplorationConfig { } } -pub struct ExplorationPlugin<'a, A>(PhantomData<&'a A>); +pub struct ExplorationPlugin<'a, S, A>(PhantomData<&'a S>, PhantomData<&'a A>); -impl<'a, A> Default for ExplorationPlugin<'a, A> { +impl<'a, S, A> Default for ExplorationPlugin<'a, S, A> { fn default() -> Self { - Self(PhantomData) + Self(PhantomData, PhantomData) } } -impl<'a, A> Plugin for ExplorationPlugin<'a, A> +impl<'a, S, A> Plugin for ExplorationPlugin<'a, S, A> where + S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash, A: Hash + Eq + Clone + Send + Sync, 'a: 'static, { fn build(&self, app: &mut AppBuilder) { - 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::>() + .unwrap() + .clone(); app.register_type::() .register_type::() .register_type::() - .add_system(exploration_focus::.system()) - .add_system( - exploration_type_focus:: - .system() - .chain(error_handler.system()), - ) - .add_system( - exploration_type_change:: - .system() - .chain(error_handler.system()), - ) - .add_system(navigate_to_explored::.system()) - .add_system_to_stage( - CoreStage::PostUpdate, - exploration_type_changed_announcement - .system() - .chain(error_handler.system()), - ) .add_system_to_stage( CoreStage::PostUpdate, exploration_changed_announcement .system() .chain(error_handler.system()), ); + if config.exploration_control_states.is_empty() { + app.add_system(exploration_focus::.system()) + .add_system( + exploration_type_focus:: + .system() + .chain(error_handler.system()), + ) + .add_system( + exploration_type_change:: + .system() + .chain(error_handler.system()), + ) + .add_system(navigate_to_explored::.system()) + .add_system_to_stage( + CoreStage::PostUpdate, + exploration_type_changed_announcement + .system() + .chain(error_handler.system()), + ); + } else { + let states = config.exploration_control_states; + for state in states { + app.add_system_set( + SystemSet::on_update(state.clone()) + .with_system(exploration_focus::.system()) + .with_system( + exploration_type_focus:: + .system() + .chain(error_handler.system()), + ) + .with_system( + exploration_type_change:: + .system() + .chain(error_handler.system()), + ) + .with_system(navigate_to_explored::.system()) + .with_system( + exploration_type_changed_announcement + .system() + .chain(error_handler.system()), + ), + ) + .add_system_set(SystemSet::on_exit(state).with_system(cleanup.system())); + } + } } }