From 4011e7d7d3d8de85a9bc392a2588cb1311b44468 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 1 Dec 2021 09:46:34 -0500 Subject: [PATCH] Make single-activation traps work --- src/components.rs | 3 +++ src/main.rs | 1 + src/saveload_system.rs | 2 ++ src/spawner.rs | 1 + src/trigger_system.rs | 18 +++++++++++++++++- 5 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/components.rs b/src/components.rs index 446f6dc..348ce99 100644 --- a/src/components.rs +++ b/src/components.rs @@ -208,6 +208,9 @@ pub struct EntryTrigger {} #[derive(Component, Debug, Serialize, Deserialize, Clone)] pub struct EntityMoved {} +#[derive(Component, Debug, Serialize, Deserialize, Clone)] +pub struct SingleActivation {} + // Serialization helper code. We need to implement ConvertSaveLoad for each type that contains an // Entity. diff --git a/src/main.rs b/src/main.rs index aadd33b..e45700b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -520,6 +520,7 @@ fn main() -> rltk::BError { Hidden, EntryTrigger, EntityMoved, + SingleActivation, ); gs.ecs.insert(SimpleMarkerAllocator::::new()); diff --git a/src/saveload_system.rs b/src/saveload_system.rs index 8d47df6..95595ec 100644 --- a/src/saveload_system.rs +++ b/src/saveload_system.rs @@ -82,6 +82,7 @@ pub fn save_game(ecs: &mut World) { Hidden, EntryTrigger, EntityMoved, + SingleActivation, ); } @@ -170,6 +171,7 @@ pub fn load_game(ecs: &mut World) { Hidden, EntryTrigger, EntityMoved, + SingleActivation, ); } diff --git a/src/spawner.rs b/src/spawner.rs index 59a122c..30d9d99 100644 --- a/src/spawner.rs +++ b/src/spawner.rs @@ -334,6 +334,7 @@ fn bear_trap(ecs: &mut World, x: i32, y: i32) { .with(Hidden {}) .with(EntryTrigger {}) .with(InflictsDamage { damage: 6 }) + .with(SingleActivation {}) .marked::>() .build(); } diff --git a/src/trigger_system.rs b/src/trigger_system.rs index d5dce58..46dccb0 100644 --- a/src/trigger_system.rs +++ b/src/trigger_system.rs @@ -1,5 +1,6 @@ use crate::components::{ - EntityMoved, EntryTrigger, Hidden, InflictsDamage, Name, Position, SufferDamage, + EntityMoved, EntryTrigger, Hidden, InflictsDamage, Name, Position, SingleActivation, + SufferDamage, }; use crate::{game_log::GameLog, particle_system::ParticleBuilder, Map}; use specs::prelude::*; @@ -20,6 +21,7 @@ impl<'a> System<'a> for TriggerSystem { ReadStorage<'a, InflictsDamage>, WriteExpect<'a, ParticleBuilder>, WriteStorage<'a, SufferDamage>, + ReadStorage<'a, SingleActivation>, ); fn run(&mut self, data: Self::SystemData) { @@ -35,9 +37,11 @@ impl<'a> System<'a> for TriggerSystem { inflicts_damage, mut particle_builder, mut inflict_damage, + single_activation, ) = data; // Iterate the entities that moved and their final position + let mut remove_entities: Vec = Vec::new(); for (entity, mut _entity_moved, pos) in (&entities, &mut entity_moved, &position).join() { let idx = map.xy_idx(pos.x, pos.y); @@ -72,12 +76,24 @@ impl<'a> System<'a> for TriggerSystem { damage.damage, ); } + + // If it is single activation, it needs to be removed + if let Some(_sa) = single_activation.get(*entity_id) { + remove_entities.push(*entity_id); + } } } } } } + // Remove any single activation traps + for trap in remove_entities.iter() { + entities + .delete(*trap) + .expect("Unable to remove single activation entity"); + } + // Remove all entity movement markers entity_moved.clear(); }