Correctly sort coordinates when moving between features.

This commit is contained in:
Nolan Darilek 2021-09-27 13:18:45 -05:00
parent 0dc9a49c4e
commit b24a30a5d3
2 changed files with 35 additions and 33 deletions

View File

@ -350,6 +350,9 @@ pub trait PointLike {
(self.x_i32(), self.y_i32())
}
fn floor(&self) -> (f32, f32) {
(self.x().floor(), self.y().floor())
}
fn to_index(&self, width: usize) -> usize {
((self.y_i32() * width as i32) + self.x_i32()) as usize
}

View File

@ -171,23 +171,24 @@ where
if !changed {
return Ok(());
}
for (entity, visible_entities, focused, exploring) in explorers.iter() {
for (entity, visible_entities, focused_type, exploring) in explorers.iter() {
let mut features = features
.iter()
.filter(|v| visible_entities.contains(&v.0))
.map(|v| (v.1, v.2))
.collect::<Vec<(&Coordinates, &ExplorationType)>>();
features.sort_by(|(c1, _), (c2, _)| c1.partial_cmp(c2).unwrap());
if let Some(focused) = &focused.0 {
features.retain(|(_, t)| **t == *focused);
}
.map(|v| (v.1.floor(), v.2))
.collect::<Vec<((f32, f32), &ExplorationType)>>();
if features.is_empty() {
tts.speak("Nothing visible.", true)?;
} else {
let mut target: Option<&(&Coordinates, &ExplorationType)> = None;
return Ok(());
}
features.sort_by(|(c1, _), (c2, _)| c1.partial_cmp(c2).unwrap());
if let Some(focused) = &focused_type.0 {
features.retain(|(_, t)| **t == *focused);
}
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);
target = features.iter().find(|(c, _)| *c > **exploring);
if target.is_none() {
target = features.first();
}
@ -197,7 +198,7 @@ where
} else if input.just_active(explore_focus_prev.clone()) {
if let Some(exploring) = exploring {
features.reverse();
target = features.iter().find(|(c, _)| ***c < **exploring);
target = features.iter().find(|(c, _)| *c < **exploring);
if target.is_none() {
target = features.first();
}
@ -206,11 +207,9 @@ where
}
}
if let Some((coordinates, _)) = target {
commands.entity(entity).insert(Exploring((
coordinates.x().floor(),
coordinates.y().floor(),
)));
}
commands
.entity(entity)
.insert(Exploring(coordinates.floor()));
}
}
}
@ -271,7 +270,7 @@ fn exploration_focus<S, A: 'static>(
let mut exploring = if let Some(exploring) = exploring {
**exploring
} else {
(coordinates.x().floor(), coordinates.y().floor())
coordinates.floor()
};
let orig = exploring;
if input.just_active(explore_forward.clone()) {
@ -337,7 +336,7 @@ fn exploration_changed_announcement(
) -> Result<(), Box<dyn Error>> {
if let Ok((coordinates, exploring)) = explorer.single() {
let collider_set = QueryPipelineColliderComponentsSet(&collider_query);
let coordinates = (coordinates.x().floor(), coordinates.y().floor());
let coordinates = coordinates.floor();
for (map, revealed_tiles, visible_tiles) in map.iter() {
let point = **exploring;
let idx = point.to_index(map.width);