roguelike-game/src/trigger_system.rs

105 lines
3.6 KiB
Rust
Raw Normal View History

2021-12-24 10:38:44 -05:00
use ::specs::prelude::*;
2021-12-10 20:16:48 -05:00
2021-11-29 16:00:07 -05:00
use crate::components::{
2021-12-01 09:46:34 -05:00
EntityMoved, EntryTrigger, Hidden, InflictsDamage, Name, Position, SingleActivation,
SufferDamage,
2021-11-29 16:00:07 -05:00
};
2021-12-10 20:16:48 -05:00
use crate::game_log::GameLog;
use crate::particle_system::ParticleBuilder;
use crate::{colors, spatial, Map};
2021-11-29 16:00:07 -05:00
pub struct TriggerSystem {}
impl<'a> System<'a> for TriggerSystem {
#[allow(clippy::type_complexity)]
type SystemData = (
ReadExpect<'a, Map>,
WriteStorage<'a, EntityMoved>,
ReadStorage<'a, Position>,
ReadStorage<'a, EntryTrigger>,
WriteStorage<'a, Hidden>,
ReadStorage<'a, Name>,
Entities<'a>,
WriteExpect<'a, GameLog>,
ReadStorage<'a, InflictsDamage>,
WriteExpect<'a, ParticleBuilder>,
WriteStorage<'a, SufferDamage>,
2021-12-01 09:46:34 -05:00
ReadStorage<'a, SingleActivation>,
2021-11-29 16:00:07 -05:00
);
fn run(&mut self, data: Self::SystemData) {
let (
map,
mut entity_moved,
position,
entry_trigger,
mut hidden,
names,
entities,
mut log,
inflicts_damage,
mut particle_builder,
mut inflict_damage,
2021-12-01 09:46:34 -05:00
single_activation,
2021-11-29 16:00:07 -05:00
) = data;
// Iterate the entities that moved and their final position
2021-12-01 09:46:34 -05:00
let mut remove_entities: Vec<Entity> = Vec::new();
2021-11-29 16:00:07 -05:00
for (entity, mut _entity_moved, pos) in (&entities, &mut entity_moved, &position).join() {
let idx = map.xy_idx(pos.x, pos.y);
spatial::for_each_tile_content(idx, |entity_id| {
2021-11-29 16:00:07 -05:00
// Do not bother to check yourself for being a trap!
if entity != entity_id {
match entry_trigger.get(entity_id) {
2021-11-29 16:00:07 -05:00
None => {}
Some(_trigger) => {
// We triggered it
if let Some(name) = names.get(entity_id) {
2021-11-29 16:00:07 -05:00
log.append(format!("{} triggers!", &name.name));
}
// The trap is no longer hidden
hidden.remove(entity_id);
2021-11-29 16:00:07 -05:00
// If the trap is damage inflicting, do it
if let Some(damage) = inflicts_damage.get(entity_id) {
2021-11-29 16:00:07 -05:00
particle_builder.request(
pos.x,
pos.y,
colors::ORANGE,
colors::BLACK,
2021-11-29 16:00:07 -05:00
rltk::to_cp437('‼'),
200.0,
);
SufferDamage::new_damage(
&mut inflict_damage,
entity,
damage.damage,
false,
2021-11-29 16:00:07 -05:00
);
}
2021-12-01 09:46:34 -05:00
// If it is single activation, it needs to be removed
if single_activation.get(entity_id).is_some() {
remove_entities.push(entity_id);
2021-12-01 09:46:34 -05:00
}
2021-11-29 16:00:07 -05:00
}
}
}
});
2021-11-29 16:00:07 -05:00
}
2021-12-01 09:46:34 -05:00
// Remove any single activation traps
for trap in remove_entities.iter() {
entities
.delete(*trap)
.expect("Unable to remove single activation entity");
}
2021-11-29 16:00:07 -05:00
// Remove all entity movement markers
entity_moved.clear();
}
}