Fix some panics when removing regions

This commit is contained in:
Imbris
2019-10-06 15:38:54 -04:00
parent b09bddda79
commit 10f9f10cb3

View File

@ -176,9 +176,6 @@ impl RegionMap {
regions_to_remove.push(i); regions_to_remove.push(i);
} }
} }
for index in regions_to_remove {
self.remove_index(index);
}
// Mutate // Mutate
// Note entity moving is outside the whole loop so that the same entity is not checked twice (this may be fine though...) // Note entity moving is outside the whole loop so that the same entity is not checked twice (this may be fine though...)
@ -196,6 +193,14 @@ impl RegionMap {
.remove(id, None); .remove(id, None);
self.tracked_entities.remove(id); self.tracked_entities.remove(id);
} }
for index in regions_to_remove {
let (k, r) = self.regions.get_index(index).unwrap();
// Check that the region is still removable
if r.removable() {
// Note we have to use key's here since the index can change when others are removed
self.remove(*k);
}
}
} }
pub fn add(&mut self, entity: EcsEntity, pos: Vec3<f32>) { pub fn add(&mut self, entity: EcsEntity, pos: Vec3<f32>) {
self.add_entity(entity.id(), pos.map(|e| e as i32), None); self.add_entity(entity.id(), pos.map(|e| e as i32), None);
@ -220,9 +225,9 @@ impl RegionMap {
pub fn key_pos(key: Vec2<i32>) -> Vec2<i32> { pub fn key_pos(key: Vec2<i32>) -> Vec2<i32> {
key.map(|e| e << REGION_LOG2) key.map(|e| e << REGION_LOG2)
} }
//fn key_index(&self, key: Vec2<i32>) -> Option<usize> { fn key_index(&self, key: Vec2<i32>) -> Option<usize> {
// self.regions.get_full(&key).map(|(i, _, _)| i) self.regions.get_full(&key).map(|(i, _, _)| i)
//} }
fn index_key(&self, index: usize) -> Option<Vec2<i32>> { fn index_key(&self, index: usize) -> Option<Vec2<i32>> {
self.regions.get_index(index).map(|(k, _)| k).copied() self.regions.get_index(index).map(|(k, _)| k).copied()
} }
@ -253,11 +258,11 @@ impl RegionMap {
index index
} }
/// Remove a region using its key /// Remove a region using its key
//fn remove(&mut self, key: Vec2<i32>) { fn remove(&mut self, key: Vec2<i32>) {
// if let Some(index) = self.key_index(key) { if let Some(index) = self.key_index(key) {
// self.remove_index(index); self.remove_index(index);
// } }
//} }
/// Add a region using its key /// Add a region using its key
fn remove_index(&mut self, index: usize) { fn remove_index(&mut self, index: usize) {
// Remap neighbor indices for neighbors of the region that will be moved from the end of the index map // Remap neighbor indices for neighbors of the region that will be moved from the end of the index map