Make action type generic for ExplorationPlugin.

This commit is contained in:
Nolan Darilek 2021-05-26 17:30:16 -05:00
parent 4982838815
commit d5c580f165

View File

@ -1,4 +1,4 @@
use std::error::Error; use std::{error::Error, hash::Hash, marker::PhantomData};
use bevy::prelude::*; use bevy::prelude::*;
use bevy_input_actionmap::InputMap; use bevy_input_actionmap::InputMap;
@ -69,24 +69,22 @@ pub struct FocusedExplorationType(pub Option<ExplorationType>);
#[reflect(Component)] #[reflect(Component)]
pub struct Mappable; pub struct Mappable;
pub const ACTION_EXPLORE_FORWARD: &str = "explore_forward"; fn exploration_type_change<A: 'static>(
pub const ACTION_EXPLORE_BACKWARD: &str = "explore_backward"; config: Res<ExplorationConfig<A>>,
pub const ACTION_EXPLORE_LEFT: &str = "explore_left";
pub const ACTION_EXPLORE_RIGHT: &str = "explore_right";
pub const ACTION_EXPLORE_FOCUS_NEXT: &str = "explore_focus_next";
pub const ACTION_EXPLORE_FOCUS_PREV: &str = "explore_focus_prev";
pub const ACTION_EXPLORE_SELECT_NEXT_TYPE: &str = "explore_select_next_type";
pub const ACTION_EXPLORE_SELECT_PREV_TYPE: &str = "explore_select_prev_type";
pub const ACTION_NAVIGATE_TO_EXPLORED: &str = "navigate_to";
fn exploration_type_change(
mut tts: ResMut<Tts>, mut tts: ResMut<Tts>,
input: Res<InputMap<String>>, 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>>
let changed = input.just_active(ACTION_EXPLORE_SELECT_NEXT_TYPE) where
|| input.just_active(ACTION_EXPLORE_SELECT_PREV_TYPE); A: Hash + Eq + Clone + Send + Sync,
{
if let (Some(select_next_type), Some(select_prev_type)) = (
config.action_explore_select_next_type.clone(),
config.action_explore_select_prev_type.clone(),
) {
let changed = input.just_active(select_next_type.clone())
|| input.just_active(select_prev_type.clone());
if !changed { if !changed {
return Ok(()); return Ok(());
} }
@ -104,7 +102,7 @@ fn exploration_type_change(
types.dedup(); types.dedup();
if types.is_empty() { if types.is_empty() {
tts.speak("Nothing visible.", true)?; tts.speak("Nothing visible.", true)?;
} else if input.just_active(ACTION_EXPLORE_SELECT_PREV_TYPE) { } else if input.just_active(select_prev_type.clone()) {
if let Some(t) = &focused.0 { if let Some(t) = &focused.0 {
if let Some(i) = types.iter().position(|v| *v == *t) { if let Some(i) = types.iter().position(|v| *v == *t) {
if i == 0 { if i == 0 {
@ -121,7 +119,7 @@ fn exploration_type_change(
let t = types.last().unwrap(); let t = types.last().unwrap();
focused.0 = Some(*t); focused.0 = Some(*t);
} }
} else if input.just_active(ACTION_EXPLORE_SELECT_NEXT_TYPE) { } else if input.just_active(select_next_type.clone()) {
if let Some(t) = &focused.0 { if let Some(t) = &focused.0 {
if let Some(i) = types.iter().position(|v| *v == *t) { if let Some(i) = types.iter().position(|v| *v == *t) {
if i == types.len() - 1 { if i == types.len() - 1 {
@ -140,12 +138,14 @@ fn exploration_type_change(
} }
} }
} }
}
Ok(()) Ok(())
} }
fn exploration_type_focus( fn exploration_type_focus<A: 'static>(
mut commands: Commands, mut commands: Commands,
input: Res<InputMap<String>>, config: Res<ExplorationConfig<A>>,
input: Res<InputMap<A>>,
mut tts: ResMut<Tts>, mut tts: ResMut<Tts>,
explorers: Query<( explorers: Query<(
Entity, Entity,
@ -155,9 +155,16 @@ fn exploration_type_focus(
Option<&Exploring>, Option<&Exploring>,
)>, )>,
features: Query<(&Coordinates, &ExplorationType)>, features: Query<(&Coordinates, &ExplorationType)>,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>>
let changed = input.just_active(ACTION_EXPLORE_FOCUS_NEXT) where
|| input.just_active(ACTION_EXPLORE_FOCUS_PREV); A: Hash + Eq + Clone + Send + Sync,
{
if let (Some(explore_focus_next), Some(explore_focus_prev)) = (
config.action_explore_focus_next.clone(),
config.action_explore_focus_prev.clone(),
) {
let changed = input.just_active(explore_focus_next.clone())
|| input.just_active(explore_focus_prev.clone());
if !changed { if !changed {
return Ok(()); return Ok(());
} }
@ -179,7 +186,7 @@ fn exploration_type_focus(
tts.speak("Nothing visible.", true)?; tts.speak("Nothing visible.", true)?;
} else { } else {
let mut target: Option<&(&Coordinates, &ExplorationType)> = None; let mut target: Option<&(&Coordinates, &ExplorationType)> = None;
if input.just_active(ACTION_EXPLORE_FOCUS_NEXT) { 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);
if target.is_none() { if target.is_none() {
@ -188,7 +195,7 @@ fn exploration_type_focus(
} else { } else {
target = features.first(); target = features.first();
} }
} else if input.just_active(ACTION_EXPLORE_FOCUS_PREV) { } else if input.just_active(explore_focus_prev.clone()) {
if let Some(exploring) = exploring { if let Some(exploring) = exploring {
features.reverse(); features.reverse();
target = features.iter().find(|(c, _)| ***c < **exploring); target = features.iter().find(|(c, _)| ***c < **exploring);
@ -204,6 +211,7 @@ fn exploration_type_focus(
} }
} }
} }
}
Ok(()) Ok(())
} }
@ -234,11 +242,25 @@ fn exploration_type_changed_announcement(
Ok(()) Ok(())
} }
fn exploration_focus( fn exploration_focus<A: 'static>(
mut commands: Commands, mut commands: Commands,
input: Res<InputMap<String>>, config: Res<ExplorationConfig<A>>,
input: Res<InputMap<A>>,
map: Query<&Map>, map: Query<&Map>,
explorers: Query<(Entity, &Player, &Coordinates, Option<&Exploring>)>, explorers: Query<(Entity, &Player, &Coordinates, Option<&Exploring>)>,
) where
A: Hash + Eq + Clone + Send + Sync,
{
if let (
Some(explore_forward),
Some(explore_backward),
Some(explore_left),
Some(explore_right),
) = (
config.action_explore_forward.clone(),
config.action_explore_backward.clone(),
config.action_explore_left.clone(),
config.action_explore_right.clone(),
) { ) {
for map in map.iter() { for map in map.iter() {
for (entity, _, coordinates, exploring) in explorers.iter() { for (entity, _, coordinates, exploring) in explorers.iter() {
@ -250,13 +272,13 @@ fn exploration_focus(
coordinates coordinates
}; };
let orig = exploring; let orig = exploring;
if input.just_active(ACTION_EXPLORE_FORWARD) { if input.just_active(explore_forward.clone()) {
exploring.1 += 1.; exploring.1 += 1.;
} else if input.just_active(ACTION_EXPLORE_BACKWARD) { } else if input.just_active(explore_backward.clone()) {
exploring.1 -= 1.; exploring.1 -= 1.;
} else if input.just_active(ACTION_EXPLORE_LEFT) { } else if input.just_active(explore_left.clone()) {
exploring.0 -= 1.; exploring.0 -= 1.;
} else if input.just_active(ACTION_EXPLORE_RIGHT) { } else if input.just_active(explore_right.clone()) {
exploring.0 += 1.; exploring.0 += 1.;
} }
if orig != exploring if orig != exploring
@ -270,19 +292,24 @@ fn exploration_focus(
} }
} }
} }
}
fn navigate_to_explored( fn navigate_to_explored<A: 'static>(
mut commands: Commands, mut commands: Commands,
input: Res<InputMap<String>>, config: Res<ExplorationConfig<A>>,
input: Res<InputMap<A>>,
map: Query<(&Map, &RevealedTiles)>, map: Query<(&Map, &RevealedTiles)>,
explorers: Query<(Entity, &Exploring)>, explorers: Query<(Entity, &Exploring)>,
) { ) where
A: Hash + Eq + Clone + Send + Sync,
{
if let Some(navigate_to_explored) = config.action_navigate_to_explored.clone() {
for (entity, exploring) in explorers.iter() { for (entity, exploring) in explorers.iter() {
for (map, revealed_tiles) in map.iter() { for (map, revealed_tiles) in map.iter() {
let point = **exploring; let point = **exploring;
let idx = point.to_index(map.width()); let idx = point.to_index(map.width());
let known = revealed_tiles[idx]; let known = revealed_tiles[idx];
if input.just_active(ACTION_NAVIGATE_TO_EXPLORED) && known { if input.just_active(navigate_to_explored.clone()) && known {
commands commands
.entity(entity) .entity(entity)
.insert(Destination((point.x_i32(), point.y_i32()))); .insert(Destination((point.x_i32(), point.y_i32())));
@ -290,6 +317,7 @@ fn navigate_to_explored(
} }
} }
} }
}
fn exploration_changed_announcement( fn exploration_changed_announcement(
mut commands: Commands, mut commands: Commands,
@ -351,25 +379,67 @@ fn exploration_changed_announcement(
Ok(()) Ok(())
} }
pub struct ExplorationPlugin; #[derive(Clone, Debug)]
pub struct ExplorationConfig<A> {
pub action_explore_forward: Option<A>,
pub action_explore_backward: Option<A>,
pub action_explore_left: Option<A>,
pub action_explore_right: Option<A>,
pub action_explore_focus_next: Option<A>,
pub action_explore_focus_prev: Option<A>,
pub action_explore_select_next_type: Option<A>,
pub action_explore_select_prev_type: Option<A>,
pub action_navigate_to_explored: Option<A>,
}
impl Plugin for ExplorationPlugin { impl<A> Default for ExplorationConfig<A> {
fn default() -> Self {
Self {
action_explore_forward: None,
action_explore_backward: None,
action_explore_left: None,
action_explore_right: None,
action_explore_focus_next: None,
action_explore_focus_prev: None,
action_explore_select_next_type: None,
action_explore_select_prev_type: None,
action_navigate_to_explored: None,
}
}
}
pub struct ExplorationPlugin<'a, A>(PhantomData<&'a A>);
impl<'a, A> Default for ExplorationPlugin<'a, A> {
fn default() -> Self {
Self(PhantomData)
}
}
impl<'a, A> Plugin for ExplorationPlugin<'a, A>
where
A: Hash + Eq + Clone + Send + Sync,
'a: 'static,
{
fn build(&self, app: &mut AppBuilder) { fn build(&self, app: &mut AppBuilder) {
if !app.world().contains_resource::<ExplorationConfig<A>>() {
app.insert_resource(ExplorationConfig::<A>::default());
}
app.register_type::<ExplorationFocused>() app.register_type::<ExplorationFocused>()
.register_type::<ExplorationType>() .register_type::<ExplorationType>()
.register_type::<Mappable>() .register_type::<Mappable>()
.add_system(exploration_focus.system()) .add_system(exploration_focus::<A>.system())
.add_system( .add_system(
exploration_type_focus exploration_type_focus::<A>
.system() .system()
.chain(error_handler.system()), .chain(error_handler.system()),
) )
.add_system( .add_system(
exploration_type_change exploration_type_change::<A>
.system() .system()
.chain(error_handler.system()), .chain(error_handler.system()),
) )
.add_system(navigate_to_explored.system()) .add_system(navigate_to_explored::<A>.system())
.add_system_to_stage( .add_system_to_stage(
CoreStage::PostUpdate, CoreStage::PostUpdate,
exploration_type_changed_announcement exploration_type_changed_announcement