diff --git a/src/exploration.rs b/src/exploration.rs index 16022b3..9e5b271 100644 --- a/src/exploration.rs +++ b/src/exploration.rs @@ -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 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); +pub struct FocusedExplorationType(pub Option); #[derive(Component, Clone, Copy, Debug, Default, Reflect)] #[reflect(Component)] pub struct Mappable; -fn exploration_type_change( +fn exploration_type_change( config: Res>, mut tts: ResMut, input: Res>, - mut explorers: Query<(&VisibleEntities, &mut FocusedExplorationType)>, - features: Query<&ExplorationType>, + mut explorers: Query<(&VisibleEntities, &mut FocusedExplorationType)>, + features: Query<&T>, ) -> Result<(), Box> 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 = vec![]; + let mut types: Vec = 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( +fn exploration_type_focus( mut commands: Commands, config: Res>, input: Res>, @@ -151,14 +114,15 @@ fn exploration_type_focus( explorers: Query<( Entity, &VisibleEntities, - &FocusedExplorationType, + &FocusedExplorationType, Option<&Exploring>, )>, - features: Query<(Entity, &Transform, &ExplorationType)>, + features: Query<(Entity, &Transform, &T)>, ) -> Result<(), Box> 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::>(); + .collect::>(); 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( mut tts: ResMut, focused: Query< ( - &FocusedExplorationType, - ChangeTrackers, + &FocusedExplorationType, + ChangeTrackers>, ), - Changed, + Changed>, >, -) -> Result<(), Box> { +) -> Result<(), Box> +where + T: Component + Copy + Into, +{ for (focused, changed) in focused.iter() { if changed.is_added() { return Ok(()); @@ -320,7 +287,7 @@ fn navigate_to_explored( +fn exploration_changed_announcement( mut commands: Commands, mut tts: ResMut, map: Query<(&Map, &RevealedTiles)>, @@ -328,10 +295,14 @@ fn exploration_changed_announcement( focused: Query>, explorable: Query, With)>>, names: Query<&Name>, - types: Query<&ExplorationType>, + types: Query<&T>, mappables: Query<&Mappable>, rapier_context: Res, -) -> Result<(), Box> { +) -> Result<(), Box> +where + T: Component + Copy + Into, + 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( 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 = vec![]; for entity in focused.iter() { commands.entity(entity).remove::(); } @@ -356,7 +327,7 @@ fn exploration_changed_announcement( 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 Default for ExplorationConfig { } } -pub struct ExplorationPlugin<'a, S: 'static, A: 'static, D>( +pub struct ExplorationPlugin<'a, T, S: 'static, A: 'static, D>( + PhantomData, PhantomData<&'a S>, PhantomData<&'static A>, PhantomData, ); -impl Default for ExplorationPlugin<'static, S, A, D> { +impl Default for ExplorationPlugin<'static, S, A, D, T> { fn default() -> Self { - Self(PhantomData, PhantomData, PhantomData) + Self(PhantomData, PhantomData, PhantomData, PhantomData) } } -impl Plugin for ExplorationPlugin<'static, S, A, D> +impl Plugin for ExplorationPlugin<'static, T, S, A, D> 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, @@ -465,17 +438,16 @@ where .unwrap() .clone(); app.register_type::() - .register_type::() .register_type::() - .add_system(exploration_changed_announcement::.chain(error_handler)); + .add_system(exploration_changed_announcement::.chain(error_handler)); if config.exploration_control_states.is_empty() { app.add_system(exploration_focus::) - .add_system(exploration_type_focus::.chain(error_handler)) - .add_system(exploration_type_change::.chain(error_handler)) + .add_system(exploration_type_focus::.chain(error_handler)) + .add_system(exploration_type_change::.chain(error_handler)) .add_system(navigate_to_explored::) .add_system_to_stage( CoreStage::PostUpdate, - exploration_type_changed_announcement.chain(error_handler), + exploration_type_changed_announcement::.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::) - .with_system(exploration_type_focus::.chain(error_handler)) - .with_system(exploration_type_change::.chain(error_handler)) + .with_system(exploration_type_focus::.chain(error_handler)) + .with_system(exploration_type_change::.chain(error_handler)) .with_system(navigate_to_explored::) - .with_system(exploration_type_changed_announcement.chain(error_handler)), + .with_system( + exploration_type_changed_announcement::.chain(error_handler), + ), ) .add_system_set(SystemSet::on_exit(state).with_system(cleanup)); } diff --git a/src/map.rs b/src/map.rs index a99a087..d18b136 100644 --- a/src/map.rs +++ b/src/map.rs @@ -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(),