ensuring we remove entities from the SpatialMap when they die

This commit is contained in:
Llywelwyn 2023-08-16 01:29:00 +01:00
parent 2ba7cfad8d
commit 012d61603a
3 changed files with 36 additions and 5 deletions

View file

@ -56,6 +56,10 @@ impl<'a> System<'a> for DamageSystem {
if let Some(xp_value) = gives_xp {
xp_gain += xp_value.amount;
}
if let Some(pos) = pos {
let idx = map.xy_idx(pos.x, pos.y);
crate::spatial::remove_entity(entity, idx);
}
}
}
}

View file

@ -1,19 +1,33 @@
use super::{spatial, BlocksTile, Map, Position};
use super::{spatial, BlocksTile, Map, Pools, Position};
use specs::prelude::*;
pub struct MapIndexingSystem {}
impl<'a> System<'a> for MapIndexingSystem {
type SystemData = (ReadExpect<'a, Map>, ReadStorage<'a, Position>, ReadStorage<'a, BlocksTile>, Entities<'a>);
type SystemData = (
ReadExpect<'a, Map>,
ReadStorage<'a, Position>,
ReadStorage<'a, BlocksTile>,
Entities<'a>,
ReadStorage<'a, Pools>,
);
fn run(&mut self, data: Self::SystemData) {
let (map, position, blockers, entities) = data;
let (map, position, blockers, entities, pools) = data;
spatial::clear();
spatial::populate_blocked_from_map(&*map);
for (entity, position) in (&entities, &position).join() {
let mut alive = true;
if let Some(pools) = pools.get(entity) {
if pools.hit_points.current < 1 {
alive = false;
}
}
if alive {
let idx = map.xy_idx(position.x, position.y);
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
}
}
}
}

View file

@ -56,6 +56,19 @@ pub fn index_entity(entity: Entity, idx: usize, blocks_tile: bool) {
}
}
/// Removes an entity from SpatialMap tilecontent.
pub fn remove_entity(entity: Entity, idx: usize) {
let mut lock = SPATIAL_MAP.lock().unwrap();
lock.tile_content[idx].retain(|(e, _)| *e != entity);
let mut from_blocked = false;
lock.tile_content[idx].iter().for_each(|(_, blocks)| {
if *blocks {
from_blocked = true;
}
});
lock.blocked[idx].1 = from_blocked;
}
/// Returns is_empty on a given tile content idx.
pub fn has_tile_content(idx: usize) -> bool {
let lock = SPATIAL_MAP.lock().unwrap();