Begin updating exploration plugin.

This commit is contained in:
Nolan Darilek 2021-09-21 15:57:47 -05:00
parent 4e9fcef178
commit e90a85641b

View File

@ -1,4 +1,4 @@
use std::{error::Error, hash::Hash, marker::PhantomData}; use std::{error::Error, fmt::Debug, hash::Hash, marker::PhantomData};
use bevy::prelude::*; use bevy::prelude::*;
use bevy_input_actionmap::InputMap; use bevy_input_actionmap::InputMap;
@ -70,14 +70,15 @@ pub struct FocusedExplorationType(pub Option<ExplorationType>);
#[reflect(Component)] #[reflect(Component)]
pub struct Mappable; pub struct Mappable;
fn exploration_type_change<A: 'static>( fn exploration_type_change<S, A: 'static>(
config: Res<ExplorationConfig<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<(&Player, &Viewshed, &mut FocusedExplorationType)>, mut explorers: Query<(&Player, &Viewshed, &mut FocusedExplorationType)>,
features: Query<(&Coordinates, &ExplorationType)>, features: Query<(&Coordinates, &ExplorationType)>,
) -> Result<(), Box<dyn Error>> ) -> Result<(), Box<dyn Error>>
where where
S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash,
A: Hash + Eq + Clone + Send + Sync, A: Hash + Eq + Clone + Send + Sync,
{ {
if let (Some(select_next_type), Some(select_prev_type)) = ( if let (Some(select_next_type), Some(select_prev_type)) = (
@ -143,9 +144,9 @@ where
Ok(()) Ok(())
} }
fn exploration_type_focus<A: 'static>( fn exploration_type_focus<S, A: 'static>(
mut commands: Commands, mut commands: Commands,
config: Res<ExplorationConfig<A>>, config: Res<ExplorationConfig<S, A>>,
input: Res<InputMap<A>>, input: Res<InputMap<A>>,
mut tts: ResMut<Tts>, mut tts: ResMut<Tts>,
explorers: Query<( explorers: Query<(
@ -158,6 +159,7 @@ fn exploration_type_focus<A: 'static>(
features: Query<(&Coordinates, &ExplorationType)>, features: Query<(&Coordinates, &ExplorationType)>,
) -> Result<(), Box<dyn Error>> ) -> Result<(), Box<dyn Error>>
where where
S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash,
A: Hash + Eq + Clone + Send + Sync, A: 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)) = (
@ -243,13 +245,14 @@ fn exploration_type_changed_announcement(
Ok(()) Ok(())
} }
fn exploration_focus<A: 'static>( fn exploration_focus<S, A: 'static>(
mut commands: Commands, mut commands: Commands,
config: Res<ExplorationConfig<A>>, config: Res<ExplorationConfig<S, A>>,
input: Res<InputMap<A>>, input: Res<InputMap<A>>,
map: Query<&Map>, map: Query<&Map>,
explorers: Query<(Entity, &Player, &Coordinates, Option<&Exploring>)>, explorers: Query<(Entity, &Coordinates, Option<&Exploring>), With<Player>>,
) where ) where
S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash,
A: Hash + Eq + Clone + Send + Sync, A: Hash + Eq + Clone + Send + Sync,
{ {
if let ( if let (
@ -263,10 +266,13 @@ fn exploration_focus<A: 'static>(
config.action_explore_left.clone(), config.action_explore_left.clone(),
config.action_explore_right.clone(), config.action_explore_right.clone(),
) { ) {
println!("Here1");
for map in map.iter() { for map in map.iter() {
for (entity, _, coordinates, exploring) in explorers.iter() { println!("Here2");
let coordinates = **coordinates; if let Ok((entity, coordinates, exploring)) = explorers.single() {
let coordinates = (coordinates.0.floor(), coordinates.1.floor()); println!("Here");
let coordinates = coordinates.i32();
let coordinates = (coordinates.x() + 0.5, coordinates.y() + 0.5);
let mut exploring = if let Some(exploring) = exploring { let mut exploring = if let Some(exploring) = exploring {
**exploring **exploring
} else { } else {
@ -295,13 +301,14 @@ fn exploration_focus<A: 'static>(
} }
} }
fn navigate_to_explored<A: 'static>( fn navigate_to_explored<S, A: 'static>(
mut commands: Commands, mut commands: Commands,
config: Res<ExplorationConfig<A>>, config: Res<ExplorationConfig<S, A>>,
input: Res<InputMap<A>>, input: Res<InputMap<A>>,
map: Query<(&Map, &RevealedTiles)>, map: Query<(&Map, &RevealedTiles)>,
explorers: Query<(Entity, &Exploring)>, explorers: Query<(Entity, &Exploring)>,
) where ) where
S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash,
A: Hash + Eq + Clone + Send + Sync, A: Hash + Eq + Clone + Send + Sync,
{ {
if let Some(navigate_to_explored) = config.action_navigate_to_explored.clone() { if let Some(navigate_to_explored) = config.action_navigate_to_explored.clone() {
@ -324,15 +331,15 @@ fn exploration_changed_announcement(
mut commands: Commands, mut commands: Commands,
mut tts: ResMut<Tts>, mut tts: ResMut<Tts>,
map: Query<(&Map, &RevealedTiles, &VisibleTiles)>, map: Query<(&Map, &RevealedTiles, &VisibleTiles)>,
explorers: Query<(&Coordinates, &Exploring), Changed<Exploring>>, explorer: Query<(&Coordinates, &Exploring), Changed<Exploring>>,
focused: Query<(Entity, &ExplorationFocused)>, focused: Query<Entity, With<ExplorationFocused>>,
names: Query<&Name>, names: Query<&Name>,
types: Query<&ExplorationType>, types: Query<&ExplorationType>,
mappables: Query<&Mappable>, mappables: Query<&Mappable>,
query_pipeline: Res<QueryPipeline>, query_pipeline: Res<QueryPipeline>,
collider_query: QueryPipelineColliderComponentsQuery, collider_query: QueryPipelineColliderComponentsQuery,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
for (coordinates, exploring) in explorers.iter() { if let Ok((coordinates, exploring)) = explorer.single() {
let collider_set = QueryPipelineColliderComponentsSet(&collider_query); let collider_set = QueryPipelineColliderComponentsSet(&collider_query);
let coordinates = **coordinates; let coordinates = **coordinates;
let coordinates = (coordinates.0.floor(), coordinates.1.floor()); let coordinates = (coordinates.0.floor(), coordinates.1.floor());
@ -345,7 +352,7 @@ fn exploration_changed_announcement(
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<&str> = vec![];
for (entity, _) in focused.iter() { for entity in focused.iter() {
commands.entity(entity).remove::<ExplorationFocused>(); commands.entity(entity).remove::<ExplorationFocused>();
} }
let shape_pos = (Vec2::new(exploring.x(), exploring.y()), 0.); let shape_pos = (Vec2::new(exploring.x(), exploring.y()), 0.);
@ -396,8 +403,22 @@ fn exploration_changed_announcement(
Ok(()) Ok(())
} }
fn cleanup(
mut commands: Commands,
explorers: Query<Entity, With<Exploring>>,
focus: Query<Entity, With<ExplorationFocused>>,
) {
for entity in explorers.iter() {
commands.entity(entity).remove::<Exploring>();
}
for entity in focus.iter() {
commands.entity(entity).despawn_recursive();
}
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct ExplorationConfig<A> { pub struct ExplorationConfig<S, A> {
pub exploration_control_states: Vec<S>,
pub action_explore_forward: Option<A>, pub action_explore_forward: Option<A>,
pub action_explore_backward: Option<A>, pub action_explore_backward: Option<A>,
pub action_explore_left: Option<A>, pub action_explore_left: Option<A>,
@ -409,9 +430,10 @@ pub struct ExplorationConfig<A> {
pub action_navigate_to_explored: Option<A>, pub action_navigate_to_explored: Option<A>,
} }
impl<A> Default for ExplorationConfig<A> { impl<S, A> Default for ExplorationConfig<S, A> {
fn default() -> Self { fn default() -> Self {
Self { Self {
exploration_control_states: vec![],
action_explore_forward: None, action_explore_forward: None,
action_explore_backward: None, action_explore_backward: None,
action_explore_left: None, action_explore_left: None,
@ -425,49 +447,82 @@ impl<A> Default for ExplorationConfig<A> {
} }
} }
pub struct ExplorationPlugin<'a, A>(PhantomData<&'a A>); pub struct ExplorationPlugin<'a, S, A>(PhantomData<&'a S>, PhantomData<&'a A>);
impl<'a, A> Default for ExplorationPlugin<'a, A> { impl<'a, S, A> Default for ExplorationPlugin<'a, S, A> {
fn default() -> Self { fn default() -> Self {
Self(PhantomData) Self(PhantomData, PhantomData)
} }
} }
impl<'a, A> Plugin for ExplorationPlugin<'a, A> impl<'a, S, A> Plugin for ExplorationPlugin<'a, S, A>
where where
S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash,
A: Hash + Eq + Clone + Send + Sync, A: Hash + Eq + Clone + Send + Sync,
'a: 'static, 'a: 'static,
{ {
fn build(&self, app: &mut AppBuilder) { fn build(&self, app: &mut AppBuilder) {
if !app.world().contains_resource::<ExplorationConfig<A>>() { if !app.world().contains_resource::<ExplorationConfig<S, A>>() {
app.insert_resource(ExplorationConfig::<A>::default()); app.insert_resource(ExplorationConfig::<S, A>::default());
} }
let config = app
.world()
.get_resource::<ExplorationConfig<S, A>>()
.unwrap()
.clone();
app.register_type::<ExplorationFocused>() app.register_type::<ExplorationFocused>()
.register_type::<ExplorationType>() .register_type::<ExplorationType>()
.register_type::<Mappable>() .register_type::<Mappable>()
.add_system(exploration_focus::<A>.system())
.add_system(
exploration_type_focus::<A>
.system()
.chain(error_handler.system()),
)
.add_system(
exploration_type_change::<A>
.system()
.chain(error_handler.system()),
)
.add_system(navigate_to_explored::<A>.system())
.add_system_to_stage(
CoreStage::PostUpdate,
exploration_type_changed_announcement
.system()
.chain(error_handler.system()),
)
.add_system_to_stage( .add_system_to_stage(
CoreStage::PostUpdate, CoreStage::PostUpdate,
exploration_changed_announcement exploration_changed_announcement
.system() .system()
.chain(error_handler.system()), .chain(error_handler.system()),
); );
if config.exploration_control_states.is_empty() {
app.add_system(exploration_focus::<S, A>.system())
.add_system(
exploration_type_focus::<S, A>
.system()
.chain(error_handler.system()),
)
.add_system(
exploration_type_change::<S, A>
.system()
.chain(error_handler.system()),
)
.add_system(navigate_to_explored::<S, A>.system())
.add_system_to_stage(
CoreStage::PostUpdate,
exploration_type_changed_announcement
.system()
.chain(error_handler.system()),
);
} else {
let states = config.exploration_control_states;
for state in states {
app.add_system_set(
SystemSet::on_update(state.clone())
.with_system(exploration_focus::<S, A>.system())
.with_system(
exploration_type_focus::<S, A>
.system()
.chain(error_handler.system()),
)
.with_system(
exploration_type_change::<S, A>
.system()
.chain(error_handler.system()),
)
.with_system(navigate_to_explored::<S, A>.system())
.with_system(
exploration_type_changed_announcement
.system()
.chain(error_handler.system()),
),
)
.add_system_set(SystemSet::on_exit(state).with_system(cleanup.system()));
}
}
} }
} }