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)]
pub struct Mappable;
fn exploration_type_change<T, S>(
fn exploration_type_change<ExplorationType, State>(
mut tts: ResMut<Tts>,
mut explorers: Query<(
&ActionState<ExplorationAction>,
&VisibleEntities,
&mut FocusedExplorationType<T>,
&mut FocusedExplorationType<ExplorationType>,
)>,
features: Query<&T>,
features: Query<&ExplorationType>,
) -> Result<(), Box<dyn Error>>
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<T> = vec![];
let mut types: Vec<ExplorationType> = 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<T, S>(
fn exploration_type_focus<ExplorationType, State>(
mut commands: Commands,
mut tts: ResMut<Tts>,
explorers: Query<(
Entity,
&ActionState<ExplorationAction>,
&VisibleEntities,
&FocusedExplorationType<T>,
&FocusedExplorationType<ExplorationType>,
Option<&Exploring>,
)>,
features: Query<(Entity, &Transform, &T)>,
features: Query<(Entity, &Transform, &ExplorationType)>,
) -> Result<(), Box<dyn Error>>
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::<Vec<((f32, f32), &T)>>();
.collect::<Vec<((f32, f32), &ExplorationType)>>();
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<T>(
fn exploration_type_changed_announcement<ExplorationType>(
mut tts: ResMut<Tts>,
focused: Query<
(
&FocusedExplorationType<T>,
ChangeTrackers<FocusedExplorationType<T>>,
&FocusedExplorationType<ExplorationType>,
ChangeTrackers<FocusedExplorationType<ExplorationType>>,
),
Changed<FocusedExplorationType<T>>,
Changed<FocusedExplorationType<ExplorationType>>,
>,
) -> Result<(), Box<dyn Error>>
where
T: Component + Default + Copy + Into<String>,
ExplorationType: Component + Default + Copy + Into<String>,
{
for (focused, changed) in focused.iter() {
if changed.is_added() {
@ -202,9 +202,9 @@ where
Ok(())
}
fn exploration_focus<S, D: 'static + Clone + Default + Send + Sync>(
fn exploration_focus<State, MapData>(
mut commands: Commands,
map: Query<&Map<D>>,
map: Query<&Map<MapData>>,
explorers: Query<
(
Entity,
@ -215,7 +215,8 @@ fn exploration_focus<S, D: 'static + Clone + Default + Send + Sync>(
With<Player>,
>,
) 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<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,
map: Query<(&Map<D>, &RevealedTiles)>,
map: Query<(&Map<MapData>, &RevealedTiles)>,
explorers: Query<(Entity, &ActionState<ExplorationAction>, &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<S, D: 'static + Clone + Default + Send + Sync>(
}
}
fn exploration_changed_announcement<T, D>(
fn exploration_changed_announcement<ExplorationType, MapData>(
mut commands: Commands,
mut tts: ResMut<Tts>,
map: Query<(&Map<D>, &RevealedTiles)>,
map: Query<(&Map<MapData>, &RevealedTiles)>,
explorer: Query<(&Transform, &Exploring, &Viewshed), Changed<Exploring>>,
focused: Query<Entity, With<ExplorationFocused>>,
explorable: Query<Entity, Or<(With<Visible>, With<Explorable>)>>,
names: Query<&Name>,
types: Query<&T>,
types: Query<&ExplorationType>,
mappables: Query<&Mappable>,
rapier_context: Res<RapierContext>,
) -> Result<(), Box<dyn Error>>
where
T: Component + Copy + Into<String>,
D: 'static + Clone + Default + Send + Sync,
ExplorationType: Component + Copy + Into<String>,
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<S> {
pub exploration_control_states: Vec<S>,
pub struct ExplorationConfig<State> {
pub states: Vec<State>,
}
impl<S> Default for ExplorationConfig<S> {
impl<State> Default for ExplorationConfig<State> {
fn default() -> Self {
Self {
exploration_control_states: vec![],
}
Self { states: vec![] }
}
}
pub struct ExplorationPlugin<'a, T, S: 'static, A: 'static, D>(
PhantomData<T>,
PhantomData<&'a S>,
PhantomData<&'static A>,
PhantomData<D>,
pub struct ExplorationPlugin<ExplorationType, State, MapData>(
PhantomData<ExplorationType>,
PhantomData<State>,
PhantomData<MapData>,
);
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 {
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
T: 'static + Component + Default + Copy + Ord + PartialEq + Into<String>,
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<String>,
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::<ExplorationConfig<S>>() {
app.insert_resource(ExplorationConfig::<S>::default());
if !app.world.contains_resource::<ExplorationConfig<State>>() {
app.insert_resource(ExplorationConfig::<State>::default());
}
let config = app
.world
.get_resource::<ExplorationConfig<S>>()
.get_resource::<ExplorationConfig<State>>()
.unwrap()
.clone();
app.register_type::<ExplorationFocused>()
.register_type::<Mappable>()
.register_type::<Explorable>()
.add_plugin(InputManagerPlugin::<ExplorationAction>::default())
.add_system(exploration_changed_announcement::<T, D>.pipe(error_handler));
if config.exploration_control_states.is_empty() {
app.add_system(exploration_focus::<S, D>)
.add_system(exploration_type_focus::<T, S>.pipe(error_handler))
.add_system(exploration_type_change::<T, S>.pipe(error_handler))
.add_system(navigate_to_explored::<S, D>)
.add_system(
exploration_changed_announcement::<ExplorationType, MapData>.pipe(error_handler),
);
if config.states.is_empty() {
app.add_system(exploration_focus::<State, MapData>)
.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(
CoreStage::PostUpdate,
exploration_type_changed_announcement::<T>.pipe(error_handler),
exploration_type_changed_announcement::<ExplorationType>.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::<S, D>)
.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(exploration_focus::<State, MapData>)
.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));