Make single-activation traps work

This commit is contained in:
Timothy Warren 2021-12-01 09:46:34 -05:00
parent 804904dd4b
commit 4011e7d7d3
5 changed files with 24 additions and 1 deletions

View File

@ -208,6 +208,9 @@ pub struct EntryTrigger {}
#[derive(Component, Debug, Serialize, Deserialize, Clone)] #[derive(Component, Debug, Serialize, Deserialize, Clone)]
pub struct EntityMoved {} 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 // Serialization helper code. We need to implement ConvertSaveLoad for each type that contains an
// Entity. // Entity.

View File

@ -520,6 +520,7 @@ fn main() -> rltk::BError {
Hidden, Hidden,
EntryTrigger, EntryTrigger,
EntityMoved, EntityMoved,
SingleActivation,
); );
gs.ecs.insert(SimpleMarkerAllocator::<SerializeMe>::new()); gs.ecs.insert(SimpleMarkerAllocator::<SerializeMe>::new());

View File

@ -82,6 +82,7 @@ pub fn save_game(ecs: &mut World) {
Hidden, Hidden,
EntryTrigger, EntryTrigger,
EntityMoved, EntityMoved,
SingleActivation,
); );
} }
@ -170,6 +171,7 @@ pub fn load_game(ecs: &mut World) {
Hidden, Hidden,
EntryTrigger, EntryTrigger,
EntityMoved, EntityMoved,
SingleActivation,
); );
} }

View File

@ -334,6 +334,7 @@ fn bear_trap(ecs: &mut World, x: i32, y: i32) {
.with(Hidden {}) .with(Hidden {})
.with(EntryTrigger {}) .with(EntryTrigger {})
.with(InflictsDamage { damage: 6 }) .with(InflictsDamage { damage: 6 })
.with(SingleActivation {})
.marked::<SimpleMarker<SerializeMe>>() .marked::<SimpleMarker<SerializeMe>>()
.build(); .build();
} }

View File

@ -1,5 +1,6 @@
use crate::components::{ 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 crate::{game_log::GameLog, particle_system::ParticleBuilder, Map};
use specs::prelude::*; use specs::prelude::*;
@ -20,6 +21,7 @@ impl<'a> System<'a> for TriggerSystem {
ReadStorage<'a, InflictsDamage>, ReadStorage<'a, InflictsDamage>,
WriteExpect<'a, ParticleBuilder>, WriteExpect<'a, ParticleBuilder>,
WriteStorage<'a, SufferDamage>, WriteStorage<'a, SufferDamage>,
ReadStorage<'a, SingleActivation>,
); );
fn run(&mut self, data: Self::SystemData) { fn run(&mut self, data: Self::SystemData) {
@ -35,9 +37,11 @@ impl<'a> System<'a> for TriggerSystem {
inflicts_damage, inflicts_damage,
mut particle_builder, mut particle_builder,
mut inflict_damage, mut inflict_damage,
single_activation,
) = data; ) = data;
// Iterate the entities that moved and their final position // Iterate the entities that moved and their final position
let mut remove_entities: Vec<Entity> = Vec::new();
for (entity, mut _entity_moved, pos) in (&entities, &mut entity_moved, &position).join() { for (entity, mut _entity_moved, pos) in (&entities, &mut entity_moved, &position).join() {
let idx = map.xy_idx(pos.x, pos.y); let idx = map.xy_idx(pos.x, pos.y);
@ -72,12 +76,24 @@ impl<'a> System<'a> for TriggerSystem {
damage.damage, 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 // Remove all entity movement markers
entity_moved.clear(); entity_moved.clear();
} }