Make exploration types generic.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Nolan Darilek 2022-07-12 15:22:44 -05:00
parent bff40340a8
commit 4b07388a56
2 changed files with 46 additions and 74 deletions

View File

@ -21,44 +21,6 @@ pub struct Explorable;
#[reflect(Component)]
pub struct ExplorationFocused;
#[allow(dead_code)]
#[derive(Component, Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Reflect)]
pub enum ExplorationType {
Portal = 0,
Item = 1,
Character = 2,
Ally = 3,
Enemy = 4,
}
// Doesn't make sense to create from a `String`.
#[allow(clippy::from_over_into)]
impl Into<String> for ExplorationType {
fn into(self) -> String {
match self {
ExplorationType::Portal => "Exit".into(),
ExplorationType::Item => "Item".into(),
ExplorationType::Character => "Character".into(),
ExplorationType::Ally => "Ally".into(),
ExplorationType::Enemy => "Enemy".into(),
}
}
}
// Likewise.
#[allow(clippy::from_over_into)]
impl Into<&str> for ExplorationType {
fn into(self) -> &'static str {
match self {
ExplorationType::Portal => "exit",
ExplorationType::Item => "item",
ExplorationType::Character => "character",
ExplorationType::Ally => "ally",
ExplorationType::Enemy => "enemy",
}
}
}
#[derive(Component, Clone, Copy, Debug, Default, Deref, DerefMut, Reflect)]
#[reflect(Component)]
pub struct Exploring(pub (f32, f32));
@ -66,20 +28,21 @@ pub struct Exploring(pub (f32, f32));
impl_pointlike_for_tuple_component!(Exploring);
#[derive(Component, Clone, Debug, Default, Deref, DerefMut)]
pub struct FocusedExplorationType(pub Option<ExplorationType>);
pub struct FocusedExplorationType<T>(pub Option<T>);
#[derive(Component, Clone, Copy, Debug, Default, Reflect)]
#[reflect(Component)]
pub struct Mappable;
fn exploration_type_change<S, A: 'static>(
fn exploration_type_change<T, S, A: 'static>(
config: Res<ExplorationConfig<S, A>>,
mut tts: ResMut<Tts>,
input: Res<InputMap<A>>,
mut explorers: Query<(&VisibleEntities, &mut FocusedExplorationType)>,
features: Query<&ExplorationType>,
mut explorers: Query<(&VisibleEntities, &mut FocusedExplorationType<T>)>,
features: Query<&T>,
) -> Result<(), Box<dyn Error>>
where
T: Component + Copy + Ord,
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
A: Hash + Eq + Clone + Send + Sync,
{
@ -93,7 +56,7 @@ where
return Ok(());
}
for (visible, mut focused) in explorers.iter_mut() {
let mut types: Vec<ExplorationType> = vec![];
let mut types: Vec<T> = vec![];
for e in visible.iter() {
if let Ok(t) = features.get(*e) {
types.push(*t);
@ -143,7 +106,7 @@ where
Ok(())
}
fn exploration_type_focus<S, A: 'static>(
fn exploration_type_focus<T, S, A>(
mut commands: Commands,
config: Res<ExplorationConfig<S, A>>,
input: Res<InputMap<A>>,
@ -151,14 +114,15 @@ fn exploration_type_focus<S, A: 'static>(
explorers: Query<(
Entity,
&VisibleEntities,
&FocusedExplorationType,
&FocusedExplorationType<T>,
Option<&Exploring>,
)>,
features: Query<(Entity, &Transform, &ExplorationType)>,
features: Query<(Entity, &Transform, &T)>,
) -> Result<(), Box<dyn Error>>
where
T: Component + PartialEq,
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
A: Hash + Eq + Clone + Send + Sync,
A: 'static + Hash + Eq + Clone + Send + Sync,
{
if let (Some(explore_focus_next), Some(explore_focus_prev)) = (
config.action_explore_focus_next.clone(),
@ -174,7 +138,7 @@ where
.iter()
.filter(|v| visible_entities.contains(&v.0))
.map(|v| (v.1.floor(), v.2))
.collect::<Vec<((f32, f32), &ExplorationType)>>();
.collect::<Vec<((f32, f32), &T)>>();
if features.is_empty() {
tts.speak("Nothing visible.", true)?;
return Ok(());
@ -183,7 +147,7 @@ where
if let Some(focused) = &focused_type.0 {
features.retain(|(_, t)| **t == *focused);
}
let mut target: Option<&((f32, f32), &ExplorationType)> = None;
let mut target: Option<&((f32, f32), &T)> = None;
if input.just_active(explore_focus_next.clone()) {
if let Some(exploring) = exploring {
target = features.iter().find(|(c, _)| *c > **exploring);
@ -214,16 +178,19 @@ where
Ok(())
}
fn exploration_type_changed_announcement(
fn exploration_type_changed_announcement<T>(
mut tts: ResMut<Tts>,
focused: Query<
(
&FocusedExplorationType,
ChangeTrackers<FocusedExplorationType>,
&FocusedExplorationType<T>,
ChangeTrackers<FocusedExplorationType<T>>,
),
Changed<FocusedExplorationType>,
Changed<FocusedExplorationType<T>>,
>,
) -> Result<(), Box<dyn Error>> {
) -> Result<(), Box<dyn Error>>
where
T: Component + Copy + Into<String>,
{
for (focused, changed) in focused.iter() {
if changed.is_added() {
return Ok(());
@ -320,7 +287,7 @@ fn navigate_to_explored<S, A: 'static, D: 'static + Clone + Default + Send + Syn
}
}
fn exploration_changed_announcement<D: 'static + Clone + Default + Send + Sync>(
fn exploration_changed_announcement<T, D>(
mut commands: Commands,
mut tts: ResMut<Tts>,
map: Query<(&Map<D>, &RevealedTiles)>,
@ -328,10 +295,14 @@ fn exploration_changed_announcement<D: 'static + Clone + Default + Send + Sync>(
focused: Query<Entity, With<ExplorationFocused>>,
explorable: Query<Entity, Or<(With<Visible>, With<Explorable>)>>,
names: Query<&Name>,
types: Query<&ExplorationType>,
types: Query<&T>,
mappables: Query<&Mappable>,
rapier_context: Res<RapierContext>,
) -> Result<(), Box<dyn Error>> {
) -> Result<(), Box<dyn Error>>
where
T: Component + Copy + Into<String>,
D: 'static + Clone + Default + Send + Sync,
{
if let Ok((coordinates, exploring, viewshed)) = explorer.get_single() {
let coordinates = coordinates.floor();
for (map, revealed_tiles) in map.iter() {
@ -342,7 +313,7 @@ fn exploration_changed_announcement<D: 'static + Clone + Default + Send + Sync>(
let visible = viewshed.is_point_visible(exploring);
let fog_of_war = known && !visible;
let description = if known {
let mut tokens: Vec<&str> = vec![];
let mut tokens: Vec<String> = vec![];
for entity in focused.iter() {
commands.entity(entity).remove::<ExplorationFocused>();
}
@ -356,7 +327,7 @@ fn exploration_changed_announcement<D: 'static + Clone + Default + Send + Sync>(
commands.entity(entity).insert(ExplorationFocused);
if visible || mappables.get(entity).is_ok() {
if let Ok(name) = names.get(entity) {
tokens.push(name.as_str());
tokens.push(name.to_string());
}
if tokens.is_empty() {
if let Ok(t) = types.get(entity) {
@ -437,20 +408,22 @@ impl<S, A> Default for ExplorationConfig<S, A> {
}
}
pub struct ExplorationPlugin<'a, S: 'static, A: 'static, D>(
pub struct ExplorationPlugin<'a, T, S: 'static, A: 'static, D>(
PhantomData<T>,
PhantomData<&'a S>,
PhantomData<&'static A>,
PhantomData<D>,
);
impl<S, A, D> Default for ExplorationPlugin<'static, S, A, D> {
impl<T, S, A, D> Default for ExplorationPlugin<'static, S, A, D, T> {
fn default() -> Self {
Self(PhantomData, PhantomData, PhantomData)
Self(PhantomData, PhantomData, PhantomData, PhantomData)
}
}
impl<S, A, D> Plugin for ExplorationPlugin<'static, S, A, D>
impl<T, S, A, D> Plugin for ExplorationPlugin<'static, T, S, A, D>
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,
@ -465,17 +438,16 @@ where
.unwrap()
.clone();
app.register_type::<ExplorationFocused>()
.register_type::<ExplorationType>()
.register_type::<Mappable>()
.add_system(exploration_changed_announcement::<D>.chain(error_handler));
.add_system(exploration_changed_announcement::<T, D>.chain(error_handler));
if config.exploration_control_states.is_empty() {
app.add_system(exploration_focus::<S, A, D>)
.add_system(exploration_type_focus::<S, A>.chain(error_handler))
.add_system(exploration_type_change::<S, A>.chain(error_handler))
.add_system(exploration_type_focus::<T, S, A>.chain(error_handler))
.add_system(exploration_type_change::<T, S, A>.chain(error_handler))
.add_system(navigate_to_explored::<S, A, D>)
.add_system_to_stage(
CoreStage::PostUpdate,
exploration_type_changed_announcement.chain(error_handler),
exploration_type_changed_announcement::<T>.chain(error_handler),
);
} else {
let states = config.exploration_control_states;
@ -483,10 +455,12 @@ where
app.add_system_set(
SystemSet::on_update(state.clone())
.with_system(exploration_focus::<S, A, D>)
.with_system(exploration_type_focus::<S, A>.chain(error_handler))
.with_system(exploration_type_change::<S, A>.chain(error_handler))
.with_system(exploration_type_focus::<T, S, A>.chain(error_handler))
.with_system(exploration_type_change::<T, S, A>.chain(error_handler))
.with_system(navigate_to_explored::<S, A, D>)
.with_system(exploration_type_changed_announcement.chain(error_handler)),
.with_system(
exploration_type_changed_announcement::<T>.chain(error_handler),
),
)
.add_system_set(SystemSet::on_exit(state).with_system(cleanup));
}

View File

@ -13,7 +13,7 @@ use rand::prelude::StdRng;
use crate::{
core::{Player, PointLike},
exploration::{ExplorationType, Mappable},
exploration::Mappable,
log::Log,
utils::target_and_other,
visibility::Visible,
@ -100,7 +100,6 @@ impl Default for MapConfig {
#[derive(Bundle)]
pub struct PortalBundle {
pub portal: Portal,
pub exploration_type: ExplorationType,
pub mappable: Mappable,
pub transform: Transform,
pub global_transform: GlobalTransform,
@ -110,7 +109,6 @@ impl Default for PortalBundle {
fn default() -> Self {
Self {
portal: Default::default(),
exploration_type: ExplorationType::Portal,
mappable: Default::default(),
transform: Default::default(),
global_transform: Default::default(),