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)]
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.

View File

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

View File

@ -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,
);
}

View File

@ -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::<SimpleMarker<SerializeMe>>()
.build();
}

View File

@ -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<Entity> = 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();
}