2021-12-24 10:38:44 -05:00
|
|
|
use ::specs::prelude::*;
|
2021-12-10 20:16:48 -05:00
|
|
|
|
2022-01-20 15:57:22 -05:00
|
|
|
use crate::components::{AreaOfEffect, EntityMoved, EntryTrigger, Name, Position};
|
|
|
|
use crate::effects::{add_effect, aoe_tiles, EffectType, Targets};
|
2021-12-10 20:16:48 -05:00
|
|
|
use crate::game_log::GameLog;
|
2022-01-20 15:57:22 -05:00
|
|
|
use crate::{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>,
|
|
|
|
ReadStorage<'a, Name>,
|
|
|
|
Entities<'a>,
|
|
|
|
WriteExpect<'a, GameLog>,
|
2022-01-20 15:57:22 -05:00
|
|
|
ReadStorage<'a, AreaOfEffect>,
|
2021-11-29 16:00:07 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
fn run(&mut self, data: Self::SystemData) {
|
|
|
|
let (
|
|
|
|
map,
|
|
|
|
mut entity_moved,
|
|
|
|
position,
|
|
|
|
entry_trigger,
|
|
|
|
names,
|
|
|
|
entities,
|
|
|
|
mut log,
|
2022-01-20 15:57:22 -05:00
|
|
|
area_of_effect,
|
2021-11-29 16:00:07 -05:00
|
|
|
) = data;
|
|
|
|
|
|
|
|
// Iterate the entities that moved and their final position
|
|
|
|
for (entity, mut _entity_moved, pos) in (&entities, &mut entity_moved, &position).join() {
|
|
|
|
let idx = map.xy_idx(pos.x, pos.y);
|
|
|
|
|
2022-01-12 10:45:13 -05:00
|
|
|
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!
|
2022-01-12 10:45:13 -05:00
|
|
|
if entity != entity_id {
|
|
|
|
match entry_trigger.get(entity_id) {
|
2021-11-29 16:00:07 -05:00
|
|
|
None => {}
|
|
|
|
Some(_trigger) => {
|
|
|
|
// We triggered it
|
2022-01-12 10:45:13 -05:00
|
|
|
if let Some(name) = names.get(entity_id) {
|
2021-11-29 16:00:07 -05:00
|
|
|
log.append(format!("{} triggers!", &name.name));
|
|
|
|
}
|
|
|
|
|
2022-01-20 15:57:22 -05:00
|
|
|
// Call the effects system
|
|
|
|
add_effect(
|
|
|
|
Some(entity),
|
|
|
|
EffectType::TriggerFire { trigger: entity_id },
|
|
|
|
if let Some(aoe) = area_of_effect.get(entity_id) {
|
|
|
|
Targets::Tiles {
|
|
|
|
tiles: aoe_tiles(
|
|
|
|
&*map,
|
|
|
|
::rltk::Point::from(*pos),
|
|
|
|
aoe.radius,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Targets::Tile {
|
|
|
|
tile_idx: idx as i32,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2021-11-29 16:00:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-12 10:45:13 -05:00
|
|
|
});
|
2021-11-29 16:00:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all entity movement markers
|
|
|
|
entity_moved.clear();
|
|
|
|
}
|
|
|
|
}
|