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()) (self.x_i32(), self.y_i32())
} }
fn floor(&self) -> (f32, f32) {
(self.x().floor(), self.y().floor())
}
fn to_index(&self, width: usize) -> usize { fn to_index(&self, width: usize) -> usize {
((self.y_i32() * width as i32) + self.x_i32()) as usize ((self.y_i32() * width as i32) + self.x_i32()) as usize
} }

View File

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