From 012d61603ae00ed88faa24f23c4e5f91753b7b73 Mon Sep 17 00:00:00 2001 From: Llywelwyn Date: Wed, 16 Aug 2023 01:29:00 +0100 Subject: [PATCH] ensuring we remove entities from the SpatialMap when they die --- src/damage_system.rs | 4 ++++ src/map_indexing_system.rs | 24 +++++++++++++++++++----- src/spatial/mod.rs | 13 +++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/damage_system.rs b/src/damage_system.rs index d44e379..e39ee88 100644 --- a/src/damage_system.rs +++ b/src/damage_system.rs @@ -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); + } } } } diff --git a/src/map_indexing_system.rs b/src/map_indexing_system.rs index 90ea6ec..d9e42f7 100644 --- a/src/map_indexing_system.rs +++ b/src/map_indexing_system.rs @@ -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 idx = map.xy_idx(position.x, position.y); - spatial::index_entity(entity, idx, blockers.get(entity).is_some()); + 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()); + } } } } diff --git a/src/spatial/mod.rs b/src/spatial/mod.rs index cbce0d8..385d65e 100644 --- a/src/spatial/mod.rs +++ b/src/spatial/mod.rs @@ -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();