effects from dead entities are now removed from queue without running

This commit is contained in:
Llywelwyn 2023-08-17 10:15:54 +01:00
parent 66f5a8d826
commit 71f69e8fe4
2 changed files with 15 additions and 0 deletions

View file

@ -106,6 +106,7 @@ pub fn bloodstain(ecs: &mut World, target: usize) {
} }
pub fn entity_death(ecs: &mut World, effect: &EffectSpawner, target: Entity) { pub fn entity_death(ecs: &mut World, effect: &EffectSpawner, target: Entity) {
super::DEAD_ENTITIES.lock().unwrap().push_back(target);
let mut xp_gain = 0; let mut xp_gain = 0;
let mut pools = ecs.write_storage::<Pools>(); let mut pools = ecs.write_storage::<Pools>();
let attributes = ecs.read_storage::<Attributes>(); let attributes = ecs.read_storage::<Attributes>();

View file

@ -17,6 +17,10 @@ lazy_static! {
pub static ref EFFECT_QUEUE: Mutex<VecDeque<EffectSpawner>> = Mutex::new(VecDeque::new()); pub static ref EFFECT_QUEUE: Mutex<VecDeque<EffectSpawner>> = Mutex::new(VecDeque::new());
} }
lazy_static! {
pub static ref DEAD_ENTITIES: Mutex<VecDeque<Entity>> = Mutex::new(VecDeque::new());
}
pub enum EffectType { pub enum EffectType {
Damage { amount: i32 }, Damage { amount: i32 },
Healing { amount: i32 }, Healing { amount: i32 },
@ -51,6 +55,16 @@ pub fn add_effect(source: Option<Entity>, effect_type: EffectType, target: Targe
/// Iterates through the effects queue, applying each effect to their target. /// Iterates through the effects queue, applying each effect to their target.
pub fn run_effects_queue(ecs: &mut World) { pub fn run_effects_queue(ecs: &mut World) {
// First removes any effect in the EFFECT_QUEUE with a dead entity as its source.
loop {
let dead_entity: Option<Entity> = DEAD_ENTITIES.lock().unwrap().pop_front();
if let Some(dead_entity) = dead_entity {
EFFECT_QUEUE.lock().unwrap().retain(|x| x.source != Some(dead_entity));
} else {
break;
}
}
// Then runs every effect that remains in the queue.
loop { loop {
let effect: Option<EffectSpawner> = EFFECT_QUEUE.lock().unwrap().pop_front(); let effect: Option<EffectSpawner> = EFFECT_QUEUE.lock().unwrap().pop_front();
if let Some(effect) = effect { if let Some(effect) = effect {