Migrate area description logger to new areas.

This commit is contained in:
Nolan Darilek 2021-06-29 10:36:59 -05:00
parent 80f163f65f
commit 5ff4a92c44
3 changed files with 45 additions and 37 deletions

View File

@ -20,4 +20,5 @@ pub mod navigation;
pub mod pathfinding; pub mod pathfinding;
pub use rand; pub use rand;
pub mod sound; pub mod sound;
pub mod utils;
pub mod visibility; pub mod visibility;

View File

@ -12,6 +12,7 @@ use crate::{
core::{Area, Coordinates, Player, PointLike}, core::{Area, Coordinates, Player, PointLike},
exploration::{ExplorationType, Mappable}, exploration::{ExplorationType, Mappable},
log::Log, log::Log,
utils::target_and_other,
visibility::BlocksVisibility, visibility::BlocksVisibility,
}; };
@ -296,50 +297,38 @@ fn portal_spawner(
} }
fn area_description( fn area_description(
mut events: EventReader<IntersectionEvent>,
areas: Query<&AreaTag>,
players: Query<&Player>,
names: Query<&Name>,
config: Res<MapConfig>, config: Res<MapConfig>,
mut prev_area: Local<Option<Area>>, shapes: Query<&ColliderShape>,
query: Query<(&Player, &Coordinates), Changed<Coordinates>>,
map: Query<(&Map, &Areas)>,
mut log: Query<&mut Log>, mut log: Query<&mut Log>,
) { ) {
for (_, coordinates) in query.iter() { for event in events.iter() {
for (_, areas) in map.iter() { if event.intersecting {
let mut should_describe_area = false; if let Some((area, other)) =
let mut current_area: Option<Area> = None; target_and_other(event.collider1.entity(), event.collider2.entity(), &|v| {
for area in areas.iter() { areas.get(v).is_ok()
if area.contains(&*coordinates) { })
current_area = Some(area.clone()); {
if let Some(prev_area) = &*prev_area { if players.get(other).is_ok() {
if prev_area != area { if let Ok(mut log) = log.single_mut() {
should_describe_area = true; if let Ok(name) = names.get(area) {
} log.push(name.to_string());
} else { } else if config.describe_undescribed_areas {
should_describe_area = true; if let Ok(shape) = shapes.get(area) {
} let aabb = shape.compute_local_aabb();
break; log.push(format!(
} "{}-by-{} area",
} aabb.extents().x,
if should_describe_area { aabb.extents().y
if let Some(ref area) = current_area { ));
let description = if area.description.is_some() { }
area.description.clone()
} else if config.describe_undescribed_areas {
Some(format!(
"{} by {} area.",
area.rect.width(),
area.rect.height()
))
} else {
None
};
if let Some(description) = description {
for mut log in log.iter_mut() {
log.push(description.clone());
} }
} }
} }
} }
*prev_area = current_area;
} }
} }
} }

18
src/utils.rs Normal file
View File

@ -0,0 +1,18 @@
use bevy::prelude::*;
pub fn target_and_other<F>(
entity1: Entity,
entity2: Entity,
mut predecate: F,
) -> Option<(Entity, Entity)>
where
F: FnMut(Entity) -> bool,
{
if predecate(entity1) {
Some((entity1, entity2))
} else if predecate(entity2) {
Some((entity2, entity1))
} else {
None
}
}