Remove entity from old index before recalculating collision/visibility of tile.

This commit is contained in:
Nolan Darilek 2021-06-01 12:14:27 -05:00
parent a2cf420963
commit e28f21c351
2 changed files with 21 additions and 12 deletions

View File

@ -302,10 +302,11 @@ fn remove_blocks_motion(
) { ) {
for entity in removed.iter() { for entity in removed.iter() {
for (map, mut motion_blocked) in map.iter_mut() { for (map, mut motion_blocked) in map.iter_mut() {
if let Some(prev) = prev_index.get(&entity) { let prev = prev_index.get(&entity).cloned();
set_motion_blocked(&map, *prev, &blocks_motion, &mut motion_blocked)
}
prev_index.remove(&entity); prev_index.remove(&entity);
if let Some(prev) = prev {
set_motion_blocked(&map, prev, &blocks_motion, &mut motion_blocked)
}
if let Ok(coordinates) = coordinates.get(entity) { if let Ok(coordinates) = coordinates.get(entity) {
let idx = coordinates.to_index(map.width()); let idx = coordinates.to_index(map.width());
set_motion_blocked(&map, idx, &blocks_motion, &mut motion_blocked) set_motion_blocked(&map, idx, &blocks_motion, &mut motion_blocked)
@ -372,15 +373,16 @@ fn remove_monitors_collisions(
for entity in removed.iter() { for entity in removed.iter() {
for (map, mut collisions_monitored) in map.iter_mut() { for (map, mut collisions_monitored) in map.iter_mut() {
if let Ok(coordinates) = coordinates.get_component::<Coordinates>(entity) { if let Ok(coordinates) = coordinates.get_component::<Coordinates>(entity) {
if let Some(prev) = prev_index.get(&entity) { let prev = prev_index.get(&entity).cloned();
prev_index.remove(&entity);
if let Some(prev) = prev {
set_monitors_collisions( set_monitors_collisions(
&map, &map,
*prev, prev,
&monitors_collisions, &monitors_collisions,
&mut collisions_monitored, &mut collisions_monitored,
); );
} }
prev_index.remove(&entity);
let idx = coordinates.to_index(map.width()); let idx = coordinates.to_index(map.width());
set_monitors_collisions(&map, idx, &monitors_collisions, &mut collisions_monitored); set_monitors_collisions(&map, idx, &monitors_collisions, &mut collisions_monitored);
} }

View File

@ -144,15 +144,16 @@ fn remove_blocks_visibility(
for entity in removed.iter() { for entity in removed.iter() {
for (map, mut visibility_blocked) in map.iter_mut() { for (map, mut visibility_blocked) in map.iter_mut() {
if let Ok(coordinates) = coordinates.get_component::<Coordinates>(entity) { if let Ok(coordinates) = coordinates.get_component::<Coordinates>(entity) {
if let Some(prev) = prev_index.get(&entity) { let prev = prev_index.get(&entity).cloned();
if let Some(prev) = prev {
prev_index.remove(&entity);
set_visibility_blocked( set_visibility_blocked(
&map, &map,
*prev, prev,
&blocks_visibility, &blocks_visibility,
&mut visibility_blocked, &mut visibility_blocked,
); );
} };
prev_index.remove(&entity);
let idx = coordinates.to_index(map.width()); let idx = coordinates.to_index(map.width());
set_visibility_blocked(&map, idx, &blocks_visibility, &mut visibility_blocked); set_visibility_blocked(&map, idx, &blocks_visibility, &mut visibility_blocked);
prev_index.insert(entity, idx); prev_index.insert(entity, idx);
@ -220,13 +221,19 @@ fn update_viewshed_for_coordinates(
#[derive(Clone, Debug, Default, Deref, DerefMut)] #[derive(Clone, Debug, Default, Deref, DerefMut)]
struct PrevVisibilityBlocked(Vec<bool>); struct PrevVisibilityBlocked(Vec<bool>);
fn visibility_blocked_added(mut prev_visibility_blocked: ResMut<PrevVisibilityBlocked>, added: Query<&VisibilityBlocked, Added<VisibilityBlocked>>) { fn visibility_blocked_added(
mut prev_visibility_blocked: ResMut<PrevVisibilityBlocked>,
added: Query<&VisibilityBlocked, Added<VisibilityBlocked>>,
) {
for visibility_blocked in added.iter() { for visibility_blocked in added.iter() {
**prev_visibility_blocked = visibility_blocked.to_vec(); **prev_visibility_blocked = visibility_blocked.to_vec();
} }
} }
fn visibility_blocked_removed(mut prev_visibility_blocked: ResMut<PrevVisibilityBlocked>, removed: RemovedComponents<VisibilityBlocked>) { fn visibility_blocked_removed(
mut prev_visibility_blocked: ResMut<PrevVisibilityBlocked>,
removed: RemovedComponents<VisibilityBlocked>,
) {
for _ in removed.iter() { for _ in removed.iter() {
prev_visibility_blocked.clear(); prev_visibility_blocked.clear();
} }