Cleanup of exploration plugin.

This commit is contained in:
Nolan Darilek 2022-12-20 07:56:43 -06:00
parent 1e9860e3a1
commit 4718d51624

View File

@ -49,21 +49,21 @@ where
#[reflect(Component)] #[reflect(Component)]
pub struct Mappable; pub struct Mappable;
fn exploration_type_change<T, S>( fn exploration_type_change<ExplorationType, State>(
mut tts: ResMut<Tts>, mut tts: ResMut<Tts>,
mut explorers: Query<( mut explorers: Query<(
&ActionState<ExplorationAction>, &ActionState<ExplorationAction>,
&VisibleEntities, &VisibleEntities,
&mut FocusedExplorationType<T>, &mut FocusedExplorationType<ExplorationType>,
)>, )>,
features: Query<&T>, features: Query<&ExplorationType>,
) -> Result<(), Box<dyn Error>> ) -> Result<(), Box<dyn Error>>
where where
T: Component + Default + Copy + Ord, ExplorationType: Component + Default + Copy + Ord,
S: 'static + Clone + Debug + Eq + Hash + Send + Sync, State: 'static + Clone + Debug + Eq + Hash + Send + Sync,
{ {
for (actions, visible, mut focused) in explorers.iter_mut() { for (actions, visible, mut focused) in explorers.iter_mut() {
let mut types: Vec<T> = vec![]; let mut types: Vec<ExplorationType> = vec![];
for e in visible.iter() { for e in visible.iter() {
if let Ok(t) = features.get(*e) { if let Ok(t) = features.get(*e) {
types.push(*t); types.push(*t);
@ -112,28 +112,28 @@ where
Ok(()) Ok(())
} }
fn exploration_type_focus<T, S>( fn exploration_type_focus<ExplorationType, State>(
mut commands: Commands, mut commands: Commands,
mut tts: ResMut<Tts>, mut tts: ResMut<Tts>,
explorers: Query<( explorers: Query<(
Entity, Entity,
&ActionState<ExplorationAction>, &ActionState<ExplorationAction>,
&VisibleEntities, &VisibleEntities,
&FocusedExplorationType<T>, &FocusedExplorationType<ExplorationType>,
Option<&Exploring>, Option<&Exploring>,
)>, )>,
features: Query<(Entity, &Transform, &T)>, features: Query<(Entity, &Transform, &ExplorationType)>,
) -> Result<(), Box<dyn Error>> ) -> Result<(), Box<dyn Error>>
where where
T: Component + Default + PartialEq, ExplorationType: Component + Default + PartialEq,
S: 'static + Clone + Debug + Eq + Hash + Send + Sync, State: 'static + Clone + Debug + Eq + Hash + Send + Sync,
{ {
for (entity, actions, visible_entities, focused_type, exploring) in explorers.iter() { for (entity, actions, visible_entities, focused_type, exploring) in explorers.iter() {
let mut features = features let mut features = features
.iter() .iter()
.filter(|v| visible_entities.contains(&v.0)) .filter(|v| visible_entities.contains(&v.0))
.map(|v| (v.1.floor(), v.2)) .map(|v| (v.1.floor(), v.2))
.collect::<Vec<((f32, f32), &T)>>(); .collect::<Vec<((f32, f32), &ExplorationType)>>();
if features.is_empty() { if features.is_empty() {
tts.speak("Nothing visible.", true)?; tts.speak("Nothing visible.", true)?;
return Ok(()); return Ok(());
@ -142,7 +142,7 @@ where
if let Some(focused) = &focused_type.0 { if let Some(focused) = &focused_type.0 {
features.retain(|(_, t)| **t == *focused); 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 actions.just_pressed(ExplorationAction::FocusNext) {
if let Some(exploring) = exploring { if let Some(exploring) = exploring {
target = features.iter().find(|(c, _)| *c > **exploring); target = features.iter().find(|(c, _)| *c > **exploring);
@ -172,18 +172,18 @@ where
Ok(()) Ok(())
} }
fn exploration_type_changed_announcement<T>( fn exploration_type_changed_announcement<ExplorationType>(
mut tts: ResMut<Tts>, mut tts: ResMut<Tts>,
focused: Query< focused: Query<
( (
&FocusedExplorationType<T>, &FocusedExplorationType<ExplorationType>,
ChangeTrackers<FocusedExplorationType<T>>, ChangeTrackers<FocusedExplorationType<ExplorationType>>,
), ),
Changed<FocusedExplorationType<T>>, Changed<FocusedExplorationType<ExplorationType>>,
>, >,
) -> Result<(), Box<dyn Error>> ) -> Result<(), Box<dyn Error>>
where where
T: Component + Default + Copy + Into<String>, ExplorationType: Component + Default + Copy + Into<String>,
{ {
for (focused, changed) in focused.iter() { for (focused, changed) in focused.iter() {
if changed.is_added() { if changed.is_added() {
@ -202,9 +202,9 @@ where
Ok(()) Ok(())
} }
fn exploration_focus<S, D: 'static + Clone + Default + Send + Sync>( fn exploration_focus<State, MapData>(
mut commands: Commands, mut commands: Commands,
map: Query<&Map<D>>, map: Query<&Map<MapData>>,
explorers: Query< explorers: Query<
( (
Entity, Entity,
@ -215,7 +215,8 @@ fn exploration_focus<S, D: 'static + Clone + Default + Send + Sync>(
With<Player>, With<Player>,
>, >,
) where ) 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() { for (entity, actions, transform, exploring) in explorers.iter() {
let coordinates = transform.translation; let coordinates = transform.translation;
@ -251,12 +252,13 @@ fn exploration_focus<S, D: 'static + Clone + Default + Send + Sync>(
} }
} }
fn navigate_to_explored<S, D: 'static + Clone + Default + Send + Sync>( fn navigate_to_explored<State, MapData>(
mut commands: Commands, mut commands: Commands,
map: Query<(&Map<D>, &RevealedTiles)>, map: Query<(&Map<MapData>, &RevealedTiles)>,
explorers: Query<(Entity, &ActionState<ExplorationAction>, &Exploring)>, explorers: Query<(Entity, &ActionState<ExplorationAction>, &Exploring)>,
) where ) 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 (entity, actions, exploring) in explorers.iter() {
for (map, revealed_tiles) in map.iter() { for (map, revealed_tiles) in map.iter() {
@ -272,21 +274,21 @@ fn navigate_to_explored<S, D: 'static + Clone + Default + Send + Sync>(
} }
} }
fn exploration_changed_announcement<T, D>( fn exploration_changed_announcement<ExplorationType, MapData>(
mut commands: Commands, mut commands: Commands,
mut tts: ResMut<Tts>, mut tts: ResMut<Tts>,
map: Query<(&Map<D>, &RevealedTiles)>, map: Query<(&Map<MapData>, &RevealedTiles)>,
explorer: Query<(&Transform, &Exploring, &Viewshed), Changed<Exploring>>, explorer: Query<(&Transform, &Exploring, &Viewshed), Changed<Exploring>>,
focused: Query<Entity, With<ExplorationFocused>>, focused: Query<Entity, With<ExplorationFocused>>,
explorable: Query<Entity, Or<(With<Visible>, With<Explorable>)>>, explorable: Query<Entity, Or<(With<Visible>, With<Explorable>)>>,
names: Query<&Name>, names: Query<&Name>,
types: Query<&T>, types: Query<&ExplorationType>,
mappables: Query<&Mappable>, mappables: Query<&Mappable>,
rapier_context: Res<RapierContext>, rapier_context: Res<RapierContext>,
) -> Result<(), Box<dyn Error>> ) -> Result<(), Box<dyn Error>>
where where
T: Component + Copy + Into<String>, ExplorationType: Component + Copy + Into<String>,
D: 'static + Clone + Default + Send + Sync, MapData: 'static + Clone + Default + Send + Sync,
{ {
if let Ok((coordinates, exploring, viewshed)) = explorer.get_single() { if let Ok((coordinates, exploring, viewshed)) = explorer.get_single() {
let coordinates = coordinates.floor(); let coordinates = coordinates.floor();
@ -370,72 +372,77 @@ fn cleanup(
} }
#[derive(Resource, Clone, Debug)] #[derive(Resource, Clone, Debug)]
pub struct ExplorationConfig<S> { pub struct ExplorationConfig<State> {
pub exploration_control_states: Vec<S>, pub states: Vec<State>,
} }
impl<S> Default for ExplorationConfig<S> { impl<State> Default for ExplorationConfig<State> {
fn default() -> Self { fn default() -> Self {
Self { Self { states: vec![] }
exploration_control_states: vec![],
}
} }
} }
pub struct ExplorationPlugin<'a, T, S: 'static, A: 'static, D>( pub struct ExplorationPlugin<ExplorationType, State, MapData>(
PhantomData<T>, PhantomData<ExplorationType>,
PhantomData<&'a S>, PhantomData<State>,
PhantomData<&'static A>, PhantomData<MapData>,
PhantomData<D>,
); );
impl<T, S, A, D> Default for ExplorationPlugin<'static, S, A, D, T> { impl<ExplorationType, State, MapData> Default
for ExplorationPlugin<State, MapData, ExplorationType>
{
fn default() -> Self { fn default() -> Self {
Self(PhantomData, PhantomData, PhantomData, PhantomData) Self(PhantomData, PhantomData, PhantomData)
} }
} }
impl<T, S, A, D> Plugin for ExplorationPlugin<'static, T, S, A, D> impl<ExplorationType, State, MapData> Plugin for ExplorationPlugin<ExplorationType, State, MapData>
where where
T: 'static + Component + Default + Copy + Ord + PartialEq + Into<String>, ExplorationType: 'static + Component + Default + Copy + Ord + PartialEq + Into<String>,
S: Clone + Debug + Eq + Hash + Send + Sync, State: 'static + Clone + Debug + Eq + Hash + Send + Sync,
A: Hash + Eq + Clone + Send + Sync, MapData: 'static + Clone + Default + Send + Sync,
D: 'static + Clone + Default + Send + Sync,
{ {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
if !app.world.contains_resource::<ExplorationConfig<S>>() { if !app.world.contains_resource::<ExplorationConfig<State>>() {
app.insert_resource(ExplorationConfig::<S>::default()); app.insert_resource(ExplorationConfig::<State>::default());
} }
let config = app let config = app
.world .world
.get_resource::<ExplorationConfig<S>>() .get_resource::<ExplorationConfig<State>>()
.unwrap() .unwrap()
.clone(); .clone();
app.register_type::<ExplorationFocused>() app.register_type::<ExplorationFocused>()
.register_type::<Mappable>() .register_type::<Mappable>()
.register_type::<Explorable>() .register_type::<Explorable>()
.add_plugin(InputManagerPlugin::<ExplorationAction>::default()) .add_plugin(InputManagerPlugin::<ExplorationAction>::default())
.add_system(exploration_changed_announcement::<T, D>.pipe(error_handler)); .add_system(
if config.exploration_control_states.is_empty() { exploration_changed_announcement::<ExplorationType, MapData>.pipe(error_handler),
app.add_system(exploration_focus::<S, D>) );
.add_system(exploration_type_focus::<T, S>.pipe(error_handler)) if config.states.is_empty() {
.add_system(exploration_type_change::<T, S>.pipe(error_handler)) app.add_system(exploration_focus::<State, MapData>)
.add_system(navigate_to_explored::<S, D>) .add_system(exploration_type_focus::<ExplorationType, State>.pipe(error_handler))
.add_system(exploration_type_change::<ExplorationType, State>.pipe(error_handler))
.add_system(navigate_to_explored::<State, MapData>)
.add_system_to_stage( .add_system_to_stage(
CoreStage::PostUpdate, CoreStage::PostUpdate,
exploration_type_changed_announcement::<T>.pipe(error_handler), exploration_type_changed_announcement::<ExplorationType>.pipe(error_handler),
); );
} else { } else {
let states = config.exploration_control_states; let states = config.states;
for state in states { for state in states {
app.add_system_set( app.add_system_set(
SystemSet::on_update(state.clone()) SystemSet::on_update(state.clone())
.with_system(exploration_focus::<S, D>) .with_system(exploration_focus::<State, MapData>)
.with_system(exploration_type_focus::<T, S>.pipe(error_handler))
.with_system(exploration_type_change::<T, S>.pipe(error_handler))
.with_system(navigate_to_explored::<S, D>)
.with_system( .with_system(
exploration_type_changed_announcement::<T>.pipe(error_handler), exploration_type_focus::<ExplorationType, State>.pipe(error_handler),
)
.with_system(
exploration_type_change::<ExplorationType, State>.pipe(error_handler),
)
.with_system(navigate_to_explored::<State, MapData>)
.with_system(
exploration_type_changed_announcement::<ExplorationType>
.pipe(error_handler),
), ),
) )
.add_system_set(SystemSet::on_exit(state).with_system(cleanup)); .add_system_set(SystemSet::on_exit(state).with_system(cleanup));