From b3a06abf0fb31fc1422708b4f88cd73a85425c76 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 18 May 2022 10:28:42 -0500 Subject: [PATCH] Various visibilityu/exploration tweaks and consistency fixes. --- src/exploration.rs | 2 +- src/visibility.rs | 36 ++++++++++++++---------------------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/exploration.rs b/src/exploration.rs index cb4b1cd..78797dc 100644 --- a/src/exploration.rs +++ b/src/exploration.rs @@ -337,7 +337,7 @@ fn exploration_changed_announcement( for (map, revealed_tiles) in map.iter() { let point = **exploring; let idx = point.to_index(map.width); - let shape = Collider::cuboid(0.49, 0.49); + let shape = Collider::cuboid(0.5 - f32::EPSILON, 0.5 - f32::EPSILON); let known = revealed_tiles[idx]; let visible = viewshed.is_point_visible(exploring); let fog_of_war = known && !visible; diff --git a/src/visibility.rs b/src/visibility.rs index 8fa7d1f..24fdca9 100644 --- a/src/visibility.rs +++ b/src/visibility.rs @@ -4,10 +4,7 @@ use std::{ marker::PhantomData, }; -use bevy::{ - core::{FixedTimestep, FloatOrd}, - prelude::*, -}; +use bevy::{core::FixedTimestep, prelude::*}; use bevy_rapier2d::na::{self, Point2}; use coord_2d::{Coord, Size}; use shadowcast::{vision_distance, Context, InputGrid}; @@ -61,23 +58,21 @@ impl Viewshed { map: &Map, visible_query: &Query<&Visible>, events: &mut EventWriter, - cache: &mut HashMap<(FloatOrd, FloatOrd), (u8, HashSet)>, + cache: &mut HashMap)>, ) { let mut context: Context = Context::default(); let vision_distance = vision_distance::Circle::new(self.range); - let shape = Collider::cuboid(0.5, 0.5); - let origin = Point2::new(start.x(), start.y()); - let coord = Coord::new(start.x_i32(), start.y_i32()); + let shape = Collider::cuboid(0.5 - f32::EPSILON, 0.5 - f32::EPSILON); + let origin = Point2::new(start.x().trunc(), start.y().trunc()); let mut new_visible_entities = HashSet::new(); let visibility_grid = VisibilityGrid( map, RefCell::new(Box::new(|coord: Coord| { - let dest = Point2::new(coord.x as f32 + 0.5, coord.y as f32 + 0.5); - if na::distance(&origin, &dest) > self.range as f32 { + let dest = Point2::new(coord.x as f32, coord.y as f32); + if na::distance(&origin, &dest) >= self.range as f32 { return u8::MAX; } - if let Some((opacity, entities)) = cache.get(&(FloatOrd(dest.x), FloatOrd(dest.y))) - { + if let Some((opacity, entities)) = cache.get(&coord) { for e in entities { new_visible_entities.insert(*e); } @@ -89,15 +84,12 @@ impl Viewshed { } let tile = map.at(coord.x as usize, coord.y as usize); if tile.blocks_visibility() { - cache.insert( - (FloatOrd(dest.x), FloatOrd(dest.y)), - (u8::MAX, HashSet::new()), - ); + cache.insert(coord, (u8::MAX, HashSet::new())); return u8::MAX; } let mut opacity = 0; let mut entities = HashSet::new(); - let shape_pos = Vec2::new(dest.x, dest.y); + let shape_pos = Vec2::new(dest.x + 0.5, dest.y + 0.5); rapier_context.intersections_with_shape( shape_pos, 0., @@ -118,14 +110,14 @@ impl Viewshed { if entities.contains(&viewer_entity) { 0 } else { - cache.insert((FloatOrd(dest.x), FloatOrd(dest.y)), (opacity, entities)); + cache.insert(coord, (opacity, entities)); opacity } })), ); let mut new_visible = HashSet::new(); context.for_each_visible( - coord, + Coord::new(start.x_i32(), start.y_i32()), &visibility_grid, &visibility_grid, vision_distance, @@ -370,10 +362,10 @@ fn remove_visible( fn update_revealed_tiles( mut map: Query<(&Map, &mut RevealedTiles)>, - viewers: Query<&Viewshed, With>, + viewers: Query<&Viewshed, (With, Changed)>, ) { - for (map, mut revealed_tiles) in map.iter_mut() { - for viewshed in viewers.iter() { + for viewshed in viewers.iter() { + for (map, mut revealed_tiles) in map.iter_mut() { for v in viewshed.visible_points.iter() { let idx = v.to_index(map.width); if idx >= revealed_tiles.len() {