2021-09-21 20:57:47 +00:00
|
|
|
use std::{error::Error, fmt::Debug, hash::Hash, marker::PhantomData};
|
2021-05-13 17:25:45 +00:00
|
|
|
|
|
|
|
use bevy::prelude::*;
|
|
|
|
use bevy_input_actionmap::InputMap;
|
2021-06-09 19:13:09 +00:00
|
|
|
use bevy_rapier2d::prelude::*;
|
2021-05-13 17:25:45 +00:00
|
|
|
use bevy_tts::Tts;
|
|
|
|
use derive_more::{Deref, DerefMut};
|
2021-07-13 17:22:50 +00:00
|
|
|
use mapgen::Tile;
|
2021-05-13 17:25:45 +00:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
core::{Coordinates, Player, PointLike},
|
|
|
|
error::error_handler,
|
|
|
|
map::Map,
|
|
|
|
pathfinding::Destination,
|
2021-09-27 18:45:52 +00:00
|
|
|
visibility::{RevealedTiles, Viewshed, Visible, VisibleEntities},
|
2021-05-13 17:25:45 +00:00
|
|
|
};
|
|
|
|
|
2022-01-10 19:50:52 +00:00
|
|
|
#[derive(Component, Clone, Copy, Debug, Default, PartialEq, Reflect)]
|
2021-09-27 15:37:15 +00:00
|
|
|
#[reflect(Component)]
|
|
|
|
pub struct Explorable;
|
|
|
|
|
2022-01-10 19:50:52 +00:00
|
|
|
#[derive(Component, Clone, Copy, Debug, Default, PartialEq, Reflect)]
|
2021-05-13 17:25:45 +00:00
|
|
|
#[reflect(Component)]
|
|
|
|
pub struct ExplorationFocused;
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2022-01-10 19:50:52 +00:00
|
|
|
#[derive(Component, Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Reflect)]
|
2021-05-13 17:25:45 +00:00
|
|
|
pub enum ExplorationType {
|
2021-05-20 19:54:13 +00:00
|
|
|
Portal = 0,
|
2021-05-13 17:25:45 +00:00
|
|
|
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 {
|
2021-09-23 18:36:39 +00:00
|
|
|
ExplorationType::Portal => "Exit".into(),
|
2021-05-13 17:25:45 +00:00
|
|
|
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 {
|
2021-05-20 19:54:13 +00:00
|
|
|
ExplorationType::Portal => "exit",
|
2021-05-13 17:25:45 +00:00
|
|
|
ExplorationType::Item => "item",
|
|
|
|
ExplorationType::Character => "character",
|
|
|
|
ExplorationType::Ally => "ally",
|
|
|
|
ExplorationType::Enemy => "enemy",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 19:50:52 +00:00
|
|
|
#[derive(Component, Clone, Copy, Debug, Default, Deref, DerefMut, Reflect)]
|
2021-05-13 17:25:45 +00:00
|
|
|
#[reflect(Component)]
|
|
|
|
pub struct Exploring(pub (f32, f32));
|
|
|
|
|
|
|
|
impl_pointlike_for_tuple_component!(Exploring);
|
|
|
|
|
2022-01-10 19:50:52 +00:00
|
|
|
#[derive(Component, Clone, Debug, Default, Deref, DerefMut)]
|
2021-05-13 17:25:45 +00:00
|
|
|
pub struct FocusedExplorationType(pub Option<ExplorationType>);
|
|
|
|
|
2022-01-10 19:50:52 +00:00
|
|
|
#[derive(Component, Clone, Copy, Debug, Default, Reflect)]
|
2021-05-13 17:25:45 +00:00
|
|
|
#[reflect(Component)]
|
|
|
|
pub struct Mappable;
|
|
|
|
|
2021-09-21 20:57:47 +00:00
|
|
|
fn exploration_type_change<S, A: 'static>(
|
|
|
|
config: Res<ExplorationConfig<S, A>>,
|
2021-05-13 17:25:45 +00:00
|
|
|
mut tts: ResMut<Tts>,
|
2021-05-26 22:30:16 +00:00
|
|
|
input: Res<InputMap<A>>,
|
2021-09-23 18:36:39 +00:00
|
|
|
mut explorers: Query<(&VisibleEntities, &mut FocusedExplorationType)>,
|
|
|
|
features: Query<&ExplorationType>,
|
2021-05-26 22:30:16 +00:00
|
|
|
) -> Result<(), Box<dyn Error>>
|
|
|
|
where
|
2021-09-21 20:57:47 +00:00
|
|
|
S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash,
|
2021-05-26 22:30:16 +00:00
|
|
|
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 {
|
|
|
|
return Ok(());
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
2021-09-23 18:36:39 +00:00
|
|
|
for (visible, mut focused) in explorers.iter_mut() {
|
2021-05-26 22:30:16 +00:00
|
|
|
let mut types: Vec<ExplorationType> = vec![];
|
2021-09-23 18:36:39 +00:00
|
|
|
for e in visible.iter() {
|
|
|
|
if let Ok(t) = features.get(*e) {
|
2021-05-26 22:30:16 +00:00
|
|
|
types.push(*t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
types.sort();
|
|
|
|
types.dedup();
|
|
|
|
if types.is_empty() {
|
|
|
|
tts.speak("Nothing visible.", true)?;
|
|
|
|
} else if input.just_active(select_prev_type.clone()) {
|
|
|
|
if let Some(t) = &focused.0 {
|
|
|
|
if let Some(i) = types.iter().position(|v| *v == *t) {
|
|
|
|
if i == 0 {
|
|
|
|
focused.0 = None;
|
|
|
|
} else {
|
|
|
|
let t = &types[i - 1];
|
|
|
|
focused.0 = Some(*t);
|
|
|
|
}
|
2021-05-13 17:25:45 +00:00
|
|
|
} else {
|
2021-05-26 22:30:16 +00:00
|
|
|
let t = types.last().unwrap();
|
2021-05-13 17:25:45 +00:00
|
|
|
focused.0 = Some(*t);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let t = types.last().unwrap();
|
|
|
|
focused.0 = Some(*t);
|
|
|
|
}
|
2021-05-26 22:30:16 +00:00
|
|
|
} else if input.just_active(select_next_type.clone()) {
|
|
|
|
if let Some(t) = &focused.0 {
|
|
|
|
if let Some(i) = types.iter().position(|v| *v == *t) {
|
|
|
|
if i == types.len() - 1 {
|
|
|
|
focused.0 = None;
|
|
|
|
} else {
|
|
|
|
let t = &types[i + 1];
|
|
|
|
focused.0 = Some(*t);
|
|
|
|
}
|
2021-05-13 17:25:45 +00:00
|
|
|
} else {
|
2021-05-26 22:30:16 +00:00
|
|
|
let t = types.first().unwrap();
|
2021-05-13 17:25:45 +00:00
|
|
|
focused.0 = Some(*t);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let t = types.first().unwrap();
|
2021-05-26 22:30:16 +00:00
|
|
|
focused.0 = Some(*t)
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-21 20:57:47 +00:00
|
|
|
fn exploration_type_focus<S, A: 'static>(
|
2021-05-13 17:25:45 +00:00
|
|
|
mut commands: Commands,
|
2021-09-21 20:57:47 +00:00
|
|
|
config: Res<ExplorationConfig<S, A>>,
|
2021-05-26 22:30:16 +00:00
|
|
|
input: Res<InputMap<A>>,
|
2021-05-13 17:25:45 +00:00
|
|
|
mut tts: ResMut<Tts>,
|
|
|
|
explorers: Query<(
|
|
|
|
Entity,
|
2021-09-23 18:36:39 +00:00
|
|
|
&VisibleEntities,
|
2021-05-13 17:25:45 +00:00
|
|
|
&FocusedExplorationType,
|
|
|
|
Option<&Exploring>,
|
|
|
|
)>,
|
2021-09-23 18:36:39 +00:00
|
|
|
features: Query<(Entity, &Coordinates, &ExplorationType)>,
|
2021-05-26 22:30:16 +00:00
|
|
|
) -> Result<(), Box<dyn Error>>
|
|
|
|
where
|
2021-09-21 20:57:47 +00:00
|
|
|
S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash,
|
2021-05-26 22:30:16 +00:00
|
|
|
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 {
|
|
|
|
return Ok(());
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
2021-09-27 18:18:45 +00:00
|
|
|
for (entity, visible_entities, focused_type, exploring) in explorers.iter() {
|
2021-05-26 22:30:16 +00:00
|
|
|
let mut features = features
|
|
|
|
.iter()
|
2021-09-23 18:36:39 +00:00
|
|
|
.filter(|v| visible_entities.contains(&v.0))
|
2021-09-27 18:18:45 +00:00
|
|
|
.map(|v| (v.1.floor(), v.2))
|
|
|
|
.collect::<Vec<((f32, f32), &ExplorationType)>>();
|
|
|
|
if features.is_empty() {
|
|
|
|
tts.speak("Nothing visible.", true)?;
|
|
|
|
return Ok(());
|
|
|
|
}
|
2021-05-26 22:30:16 +00:00
|
|
|
features.sort_by(|(c1, _), (c2, _)| c1.partial_cmp(c2).unwrap());
|
2021-09-27 18:18:45 +00:00
|
|
|
if let Some(focused) = &focused_type.0 {
|
2021-05-26 22:30:16 +00:00
|
|
|
features.retain(|(_, t)| **t == *focused);
|
|
|
|
}
|
2021-09-27 18:18:45 +00:00
|
|
|
let mut target: Option<&((f32, f32), &ExplorationType)> = None;
|
|
|
|
if input.just_active(explore_focus_next.clone()) {
|
|
|
|
if let Some(exploring) = exploring {
|
|
|
|
target = features.iter().find(|(c, _)| *c > **exploring);
|
|
|
|
if target.is_none() {
|
2021-05-13 17:25:45 +00:00
|
|
|
target = features.first();
|
|
|
|
}
|
2021-09-27 18:18:45 +00:00
|
|
|
} else {
|
|
|
|
target = features.first();
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
2021-09-27 18:18:45 +00:00
|
|
|
} else if input.just_active(explore_focus_prev.clone()) {
|
|
|
|
if let Some(exploring) = exploring {
|
|
|
|
features.reverse();
|
|
|
|
target = features.iter().find(|(c, _)| *c < **exploring);
|
|
|
|
if target.is_none() {
|
|
|
|
target = features.first();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
target = features.last();
|
2021-05-26 22:30:16 +00:00
|
|
|
}
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
2021-09-27 18:18:45 +00:00
|
|
|
if let Some((coordinates, _)) = target {
|
|
|
|
commands
|
|
|
|
.entity(entity)
|
|
|
|
.insert(Exploring(coordinates.floor()));
|
|
|
|
}
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exploration_type_changed_announcement(
|
|
|
|
mut tts: ResMut<Tts>,
|
|
|
|
focused: Query<
|
|
|
|
(
|
|
|
|
&FocusedExplorationType,
|
|
|
|
ChangeTrackers<FocusedExplorationType>,
|
|
|
|
),
|
|
|
|
Changed<FocusedExplorationType>,
|
|
|
|
>,
|
|
|
|
) -> Result<(), Box<dyn Error>> {
|
|
|
|
for (focused, changed) in focused.iter() {
|
|
|
|
if changed.is_added() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
match &focused.0 {
|
|
|
|
Some(v) => {
|
|
|
|
let v: String = (*v).into();
|
|
|
|
tts.speak(v, true)?;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
tts.speak("Everything", true)?;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-21 20:57:47 +00:00
|
|
|
fn exploration_focus<S, A: 'static>(
|
2021-05-13 17:25:45 +00:00
|
|
|
mut commands: Commands,
|
2021-09-21 20:57:47 +00:00
|
|
|
config: Res<ExplorationConfig<S, A>>,
|
2021-05-26 22:30:16 +00:00
|
|
|
input: Res<InputMap<A>>,
|
2021-05-13 17:25:45 +00:00
|
|
|
map: Query<&Map>,
|
2021-09-21 20:57:47 +00:00
|
|
|
explorers: Query<(Entity, &Coordinates, Option<&Exploring>), With<Player>>,
|
2021-05-26 22:30:16 +00:00
|
|
|
) where
|
2021-09-21 20:57:47 +00:00
|
|
|
S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash,
|
2021-05-26 22:30:16 +00:00
|
|
|
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() {
|
2022-02-01 14:41:27 +00:00
|
|
|
if let Ok((entity, coordinates, exploring)) = explorers.get_single() {
|
|
|
|
let coordinates = **coordinates;
|
|
|
|
let mut exploring = if let Some(exploring) = exploring {
|
|
|
|
**exploring
|
|
|
|
} else {
|
|
|
|
coordinates.floor()
|
|
|
|
};
|
|
|
|
let orig = exploring;
|
|
|
|
if input.just_active(explore_forward.clone()) {
|
|
|
|
exploring.1 += 1.;
|
|
|
|
} else if input.just_active(explore_backward.clone()) {
|
|
|
|
exploring.1 -= 1.;
|
|
|
|
} else if input.just_active(explore_left.clone()) {
|
|
|
|
exploring.0 -= 1.;
|
|
|
|
} else if input.just_active(explore_right.clone()) {
|
|
|
|
exploring.0 += 1.;
|
|
|
|
}
|
|
|
|
if orig != exploring
|
|
|
|
&& exploring.0 >= 0.
|
|
|
|
&& exploring.0 < map.width as f32
|
|
|
|
&& exploring.1 >= 0.
|
|
|
|
&& exploring.1 < map.height as f32
|
|
|
|
{
|
|
|
|
commands.entity(entity).insert(Exploring(exploring));
|
|
|
|
}
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 20:57:47 +00:00
|
|
|
fn navigate_to_explored<S, A: 'static>(
|
2021-05-13 17:25:45 +00:00
|
|
|
mut commands: Commands,
|
2021-09-21 20:57:47 +00:00
|
|
|
config: Res<ExplorationConfig<S, A>>,
|
2021-05-26 22:30:16 +00:00
|
|
|
input: Res<InputMap<A>>,
|
2021-05-13 17:25:45 +00:00
|
|
|
map: Query<(&Map, &RevealedTiles)>,
|
|
|
|
explorers: Query<(Entity, &Exploring)>,
|
2021-05-26 22:30:16 +00:00
|
|
|
) where
|
2021-09-21 20:57:47 +00:00
|
|
|
S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash,
|
2021-05-26 22:30:16 +00:00
|
|
|
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 (map, revealed_tiles) in map.iter() {
|
|
|
|
let point = **exploring;
|
2021-06-09 19:53:48 +00:00
|
|
|
let idx = point.to_index(map.width);
|
2021-05-26 22:30:16 +00:00
|
|
|
let known = revealed_tiles[idx];
|
|
|
|
if input.just_active(navigate_to_explored.clone()) && known {
|
|
|
|
commands
|
|
|
|
.entity(entity)
|
|
|
|
.insert(Destination((point.x_i32(), point.y_i32())));
|
|
|
|
}
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exploration_changed_announcement(
|
|
|
|
mut commands: Commands,
|
|
|
|
mut tts: ResMut<Tts>,
|
2021-09-27 18:45:52 +00:00
|
|
|
map: Query<(&Map, &RevealedTiles)>,
|
|
|
|
explorer: Query<(&Coordinates, &Exploring, &Viewshed), Changed<Exploring>>,
|
2021-09-21 20:57:47 +00:00
|
|
|
focused: Query<Entity, With<ExplorationFocused>>,
|
2021-09-27 15:37:15 +00:00
|
|
|
explorable: Query<Entity, Or<(With<Visible>, With<Explorable>)>>,
|
2021-05-13 17:25:45 +00:00
|
|
|
names: Query<&Name>,
|
|
|
|
types: Query<&ExplorationType>,
|
|
|
|
mappables: Query<&Mappable>,
|
2021-06-09 19:13:09 +00:00
|
|
|
query_pipeline: Res<QueryPipeline>,
|
|
|
|
collider_query: QueryPipelineColliderComponentsQuery,
|
2021-05-13 17:25:45 +00:00
|
|
|
) -> Result<(), Box<dyn Error>> {
|
2022-01-13 20:43:02 +00:00
|
|
|
if let Ok((coordinates, exploring, viewshed)) = explorer.get_single() {
|
|
|
|
let collider_set = QueryPipelineColliderComponentsSet(&collider_query);
|
|
|
|
let coordinates = coordinates.floor();
|
|
|
|
for (map, revealed_tiles) in map.iter() {
|
|
|
|
let point = **exploring;
|
|
|
|
let idx = point.to_index(map.width);
|
|
|
|
let shape = Cuboid::new(Vec2::new(0.49, 0.49).into());
|
|
|
|
let known = revealed_tiles[idx];
|
|
|
|
let visible = viewshed.is_point_visible(exploring);
|
|
|
|
let fog_of_war = known && !visible;
|
|
|
|
let description = if known {
|
|
|
|
let mut tokens: Vec<&str> = vec![];
|
|
|
|
for entity in focused.iter() {
|
|
|
|
commands.entity(entity).remove::<ExplorationFocused>();
|
|
|
|
}
|
|
|
|
let shape_pos = (Vec2::new(exploring.x(), exploring.y()), 0.);
|
|
|
|
query_pipeline.intersections_with_shape(
|
|
|
|
&collider_set,
|
|
|
|
&shape_pos.into(),
|
|
|
|
&shape,
|
|
|
|
InteractionGroups::all(),
|
|
|
|
Some(&|v| explorable.get(v.entity()).is_ok()),
|
|
|
|
|handle| {
|
|
|
|
let entity = handle.entity();
|
|
|
|
commands.entity(entity).insert(ExplorationFocused);
|
|
|
|
if visible || mappables.get(entity).is_ok() {
|
|
|
|
if let Ok(name) = names.get(entity) {
|
|
|
|
tokens.push(name.as_str());
|
|
|
|
}
|
|
|
|
if tokens.is_empty() {
|
|
|
|
if let Ok(t) = types.get(entity) {
|
|
|
|
tokens.push((*t).into());
|
|
|
|
}
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-13 20:43:02 +00:00
|
|
|
true
|
|
|
|
},
|
|
|
|
);
|
|
|
|
if tokens.is_empty() {
|
|
|
|
let tile = map.tiles[idx];
|
|
|
|
if tile.is_blocked() {
|
|
|
|
"wall".to_string()
|
|
|
|
} else {
|
|
|
|
"floor".to_string()
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-13 20:43:02 +00:00
|
|
|
tokens.join(": ")
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-13 20:43:02 +00:00
|
|
|
"Unknown".to_string()
|
|
|
|
};
|
|
|
|
let mut tokens: Vec<String> = vec![
|
|
|
|
description,
|
|
|
|
coordinates.direction_and_distance(exploring, None),
|
|
|
|
];
|
|
|
|
if fog_of_war {
|
|
|
|
tokens.push("in the fog of war".into());
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
2022-01-13 20:43:02 +00:00
|
|
|
tts.speak(tokens.join(", ").to_string(), true)?;
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-21 20:57:47 +00:00
|
|
|
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() {
|
2021-09-22 15:21:31 +00:00
|
|
|
commands.entity(entity).remove::<ExplorationFocused>();
|
2021-09-21 20:57:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 22:30:16 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2021-09-21 20:57:47 +00:00
|
|
|
pub struct ExplorationConfig<S, A> {
|
|
|
|
pub exploration_control_states: Vec<S>,
|
2021-05-26 22:30:16 +00:00
|
|
|
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>,
|
|
|
|
}
|
2021-05-13 17:25:45 +00:00
|
|
|
|
2021-09-21 20:57:47 +00:00
|
|
|
impl<S, A> Default for ExplorationConfig<S, A> {
|
2021-05-26 22:30:16 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2021-09-21 20:57:47 +00:00
|
|
|
exploration_control_states: vec![],
|
2021-05-26 22:30:16 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 20:57:47 +00:00
|
|
|
pub struct ExplorationPlugin<'a, S, A>(PhantomData<&'a S>, PhantomData<&'a A>);
|
2021-05-26 22:30:16 +00:00
|
|
|
|
2021-09-21 20:57:47 +00:00
|
|
|
impl<'a, S, A> Default for ExplorationPlugin<'a, S, A> {
|
2021-05-26 22:30:16 +00:00
|
|
|
fn default() -> Self {
|
2021-09-21 20:57:47 +00:00
|
|
|
Self(PhantomData, PhantomData)
|
2021-05-26 22:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 20:57:47 +00:00
|
|
|
impl<'a, S, A> Plugin for ExplorationPlugin<'a, S, A>
|
2021-05-26 22:30:16 +00:00
|
|
|
where
|
2021-09-21 20:57:47 +00:00
|
|
|
S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash,
|
2021-05-26 22:30:16 +00:00
|
|
|
A: Hash + Eq + Clone + Send + Sync,
|
|
|
|
'a: 'static,
|
|
|
|
{
|
2022-01-10 19:50:52 +00:00
|
|
|
fn build(&self, app: &mut App) {
|
2022-01-11 05:05:51 +00:00
|
|
|
if !app.world.contains_resource::<ExplorationConfig<S, A>>() {
|
2021-09-21 20:57:47 +00:00
|
|
|
app.insert_resource(ExplorationConfig::<S, A>::default());
|
2021-05-26 22:30:16 +00:00
|
|
|
}
|
2021-09-21 20:57:47 +00:00
|
|
|
let config = app
|
2022-01-11 05:05:51 +00:00
|
|
|
.world
|
2021-09-21 20:57:47 +00:00
|
|
|
.get_resource::<ExplorationConfig<S, A>>()
|
|
|
|
.unwrap()
|
|
|
|
.clone();
|
2021-05-13 17:25:45 +00:00
|
|
|
app.register_type::<ExplorationFocused>()
|
|
|
|
.register_type::<ExplorationType>()
|
|
|
|
.register_type::<Mappable>()
|
|
|
|
.add_system_to_stage(
|
|
|
|
CoreStage::PostUpdate,
|
2022-01-10 19:50:52 +00:00
|
|
|
exploration_changed_announcement.chain(error_handler),
|
2021-05-13 17:25:45 +00:00
|
|
|
);
|
2021-09-21 20:57:47 +00:00
|
|
|
if config.exploration_control_states.is_empty() {
|
2022-01-10 19:50:52 +00:00
|
|
|
app.add_system(exploration_focus::<S, A>)
|
|
|
|
.add_system(exploration_type_focus::<S, A>.chain(error_handler))
|
|
|
|
.add_system(exploration_type_change::<S, A>.chain(error_handler))
|
|
|
|
.add_system(navigate_to_explored::<S, A>)
|
2021-09-21 20:57:47 +00:00
|
|
|
.add_system_to_stage(
|
|
|
|
CoreStage::PostUpdate,
|
2022-01-10 19:50:52 +00:00
|
|
|
exploration_type_changed_announcement.chain(error_handler),
|
2021-09-21 20:57:47 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
let states = config.exploration_control_states;
|
|
|
|
for state in states {
|
|
|
|
app.add_system_set(
|
|
|
|
SystemSet::on_update(state.clone())
|
2022-01-10 19:50:52 +00:00
|
|
|
.with_system(exploration_focus::<S, A>)
|
|
|
|
.with_system(exploration_type_focus::<S, A>.chain(error_handler))
|
|
|
|
.with_system(exploration_type_change::<S, A>.chain(error_handler))
|
|
|
|
.with_system(navigate_to_explored::<S, A>)
|
|
|
|
.with_system(exploration_type_changed_announcement.chain(error_handler)),
|
2021-09-21 20:57:47 +00:00
|
|
|
)
|
2022-01-10 19:50:52 +00:00
|
|
|
.add_system_set(SystemSet::on_exit(state).with_system(cleanup));
|
2021-09-21 20:57:47 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
|
|
|
}
|