Make exploration types generic.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
bff40340a8
commit
4b07388a56
|
@ -21,44 +21,6 @@ pub struct Explorable;
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct ExplorationFocused;
|
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)]
|
#[derive(Component, Clone, Copy, Debug, Default, Deref, DerefMut, Reflect)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct Exploring(pub (f32, f32));
|
pub struct Exploring(pub (f32, f32));
|
||||||
|
@ -66,20 +28,21 @@ pub struct Exploring(pub (f32, f32));
|
||||||
impl_pointlike_for_tuple_component!(Exploring);
|
impl_pointlike_for_tuple_component!(Exploring);
|
||||||
|
|
||||||
#[derive(Component, Clone, Debug, Default, Deref, DerefMut)]
|
#[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)]
|
#[derive(Component, Clone, Copy, Debug, Default, Reflect)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct Mappable;
|
pub struct Mappable;
|
||||||
|
|
||||||
fn exploration_type_change<S, A: 'static>(
|
fn exploration_type_change<T, S, A: 'static>(
|
||||||
config: Res<ExplorationConfig<S, A>>,
|
config: Res<ExplorationConfig<S, A>>,
|
||||||
mut tts: ResMut<Tts>,
|
mut tts: ResMut<Tts>,
|
||||||
input: Res<InputMap<A>>,
|
input: Res<InputMap<A>>,
|
||||||
mut explorers: Query<(&VisibleEntities, &mut FocusedExplorationType)>,
|
mut explorers: Query<(&VisibleEntities, &mut FocusedExplorationType<T>)>,
|
||||||
features: Query<&ExplorationType>,
|
features: Query<&T>,
|
||||||
) -> Result<(), Box<dyn Error>>
|
) -> Result<(), Box<dyn Error>>
|
||||||
where
|
where
|
||||||
|
T: Component + Copy + Ord,
|
||||||
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
|
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
|
||||||
A: Hash + Eq + Clone + Send + Sync,
|
A: Hash + Eq + Clone + Send + Sync,
|
||||||
{
|
{
|
||||||
|
@ -93,7 +56,7 @@ where
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
for (visible, mut focused) in explorers.iter_mut() {
|
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() {
|
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);
|
||||||
|
@ -143,7 +106,7 @@ where
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exploration_type_focus<S, A: 'static>(
|
fn exploration_type_focus<T, S, A>(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
config: Res<ExplorationConfig<S, A>>,
|
config: Res<ExplorationConfig<S, A>>,
|
||||||
input: Res<InputMap<A>>,
|
input: Res<InputMap<A>>,
|
||||||
|
@ -151,14 +114,15 @@ fn exploration_type_focus<S, A: 'static>(
|
||||||
explorers: Query<(
|
explorers: Query<(
|
||||||
Entity,
|
Entity,
|
||||||
&VisibleEntities,
|
&VisibleEntities,
|
||||||
&FocusedExplorationType,
|
&FocusedExplorationType<T>,
|
||||||
Option<&Exploring>,
|
Option<&Exploring>,
|
||||||
)>,
|
)>,
|
||||||
features: Query<(Entity, &Transform, &ExplorationType)>,
|
features: Query<(Entity, &Transform, &T)>,
|
||||||
) -> Result<(), Box<dyn Error>>
|
) -> Result<(), Box<dyn Error>>
|
||||||
where
|
where
|
||||||
|
T: Component + PartialEq,
|
||||||
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
|
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)) = (
|
if let (Some(explore_focus_next), Some(explore_focus_prev)) = (
|
||||||
config.action_explore_focus_next.clone(),
|
config.action_explore_focus_next.clone(),
|
||||||
|
@ -174,7 +138,7 @@ where
|
||||||
.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), &ExplorationType)>>();
|
.collect::<Vec<((f32, f32), &T)>>();
|
||||||
if features.is_empty() {
|
if features.is_empty() {
|
||||||
tts.speak("Nothing visible.", true)?;
|
tts.speak("Nothing visible.", true)?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
@ -183,7 +147,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), &ExplorationType)> = None;
|
let mut target: Option<&((f32, f32), &T)> = None;
|
||||||
if input.just_active(explore_focus_next.clone()) {
|
if input.just_active(explore_focus_next.clone()) {
|
||||||
if let Some(exploring) = exploring {
|
if let Some(exploring) = exploring {
|
||||||
target = features.iter().find(|(c, _)| *c > **exploring);
|
target = features.iter().find(|(c, _)| *c > **exploring);
|
||||||
|
@ -214,16 +178,19 @@ where
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exploration_type_changed_announcement(
|
fn exploration_type_changed_announcement<T>(
|
||||||
mut tts: ResMut<Tts>,
|
mut tts: ResMut<Tts>,
|
||||||
focused: Query<
|
focused: Query<
|
||||||
(
|
(
|
||||||
&FocusedExplorationType,
|
&FocusedExplorationType<T>,
|
||||||
ChangeTrackers<FocusedExplorationType>,
|
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() {
|
for (focused, changed) in focused.iter() {
|
||||||
if changed.is_added() {
|
if changed.is_added() {
|
||||||
return Ok(());
|
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 commands: Commands,
|
||||||
mut tts: ResMut<Tts>,
|
mut tts: ResMut<Tts>,
|
||||||
map: Query<(&Map<D>, &RevealedTiles)>,
|
map: Query<(&Map<D>, &RevealedTiles)>,
|
||||||
|
@ -328,10 +295,14 @@ fn exploration_changed_announcement<D: 'static + Clone + Default + Send + Sync>(
|
||||||
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<&ExplorationType>,
|
types: Query<&T>,
|
||||||
mappables: Query<&Mappable>,
|
mappables: Query<&Mappable>,
|
||||||
rapier_context: Res<RapierContext>,
|
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() {
|
if let Ok((coordinates, exploring, viewshed)) = explorer.get_single() {
|
||||||
let coordinates = coordinates.floor();
|
let coordinates = coordinates.floor();
|
||||||
for (map, revealed_tiles) in map.iter() {
|
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 visible = viewshed.is_point_visible(exploring);
|
||||||
let fog_of_war = known && !visible;
|
let fog_of_war = known && !visible;
|
||||||
let description = if known {
|
let description = if known {
|
||||||
let mut tokens: Vec<&str> = vec![];
|
let mut tokens: Vec<String> = vec![];
|
||||||
for entity in focused.iter() {
|
for entity in focused.iter() {
|
||||||
commands.entity(entity).remove::<ExplorationFocused>();
|
commands.entity(entity).remove::<ExplorationFocused>();
|
||||||
}
|
}
|
||||||
|
@ -356,7 +327,7 @@ fn exploration_changed_announcement<D: 'static + Clone + Default + Send + Sync>(
|
||||||
commands.entity(entity).insert(ExplorationFocused);
|
commands.entity(entity).insert(ExplorationFocused);
|
||||||
if visible || mappables.get(entity).is_ok() {
|
if visible || mappables.get(entity).is_ok() {
|
||||||
if let Ok(name) = names.get(entity) {
|
if let Ok(name) = names.get(entity) {
|
||||||
tokens.push(name.as_str());
|
tokens.push(name.to_string());
|
||||||
}
|
}
|
||||||
if tokens.is_empty() {
|
if tokens.is_empty() {
|
||||||
if let Ok(t) = types.get(entity) {
|
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<&'a S>,
|
||||||
PhantomData<&'static A>,
|
PhantomData<&'static A>,
|
||||||
PhantomData<D>,
|
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 {
|
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
|
where
|
||||||
|
T: 'static + Component + Default + Copy + Ord + PartialEq + Into<String>,
|
||||||
S: Clone + Debug + Eq + Hash + Send + Sync,
|
S: Clone + Debug + Eq + Hash + Send + Sync,
|
||||||
A: Hash + Eq + Clone + Send + Sync,
|
A: Hash + Eq + Clone + Send + Sync,
|
||||||
D: 'static + Clone + Default + Send + Sync,
|
D: 'static + Clone + Default + Send + Sync,
|
||||||
|
@ -465,17 +438,16 @@ where
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.clone();
|
.clone();
|
||||||
app.register_type::<ExplorationFocused>()
|
app.register_type::<ExplorationFocused>()
|
||||||
.register_type::<ExplorationType>()
|
|
||||||
.register_type::<Mappable>()
|
.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() {
|
if config.exploration_control_states.is_empty() {
|
||||||
app.add_system(exploration_focus::<S, A, D>)
|
app.add_system(exploration_focus::<S, A, D>)
|
||||||
.add_system(exploration_type_focus::<S, A>.chain(error_handler))
|
.add_system(exploration_type_focus::<T, S, A>.chain(error_handler))
|
||||||
.add_system(exploration_type_change::<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(navigate_to_explored::<S, A, D>)
|
||||||
.add_system_to_stage(
|
.add_system_to_stage(
|
||||||
CoreStage::PostUpdate,
|
CoreStage::PostUpdate,
|
||||||
exploration_type_changed_announcement.chain(error_handler),
|
exploration_type_changed_announcement::<T>.chain(error_handler),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
let states = config.exploration_control_states;
|
let states = config.exploration_control_states;
|
||||||
|
@ -483,10 +455,12 @@ where
|
||||||
app.add_system_set(
|
app.add_system_set(
|
||||||
SystemSet::on_update(state.clone())
|
SystemSet::on_update(state.clone())
|
||||||
.with_system(exploration_focus::<S, A, D>)
|
.with_system(exploration_focus::<S, A, D>)
|
||||||
.with_system(exploration_type_focus::<S, A>.chain(error_handler))
|
.with_system(exploration_type_focus::<T, S, A>.chain(error_handler))
|
||||||
.with_system(exploration_type_change::<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(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));
|
.add_system_set(SystemSet::on_exit(state).with_system(cleanup));
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ use rand::prelude::StdRng;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
core::{Player, PointLike},
|
core::{Player, PointLike},
|
||||||
exploration::{ExplorationType, Mappable},
|
exploration::Mappable,
|
||||||
log::Log,
|
log::Log,
|
||||||
utils::target_and_other,
|
utils::target_and_other,
|
||||||
visibility::Visible,
|
visibility::Visible,
|
||||||
|
@ -100,7 +100,6 @@ impl Default for MapConfig {
|
||||||
#[derive(Bundle)]
|
#[derive(Bundle)]
|
||||||
pub struct PortalBundle {
|
pub struct PortalBundle {
|
||||||
pub portal: Portal,
|
pub portal: Portal,
|
||||||
pub exploration_type: ExplorationType,
|
|
||||||
pub mappable: Mappable,
|
pub mappable: Mappable,
|
||||||
pub transform: Transform,
|
pub transform: Transform,
|
||||||
pub global_transform: GlobalTransform,
|
pub global_transform: GlobalTransform,
|
||||||
|
@ -110,7 +109,6 @@ impl Default for PortalBundle {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
portal: Default::default(),
|
portal: Default::default(),
|
||||||
exploration_type: ExplorationType::Portal,
|
|
||||||
mappable: Default::default(),
|
mappable: Default::default(),
|
||||||
transform: Default::default(),
|
transform: Default::default(),
|
||||||
global_transform: Default::default(),
|
global_transform: Default::default(),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user