trigger_system.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. extern crate specs;
  2. use specs::prelude::*;
  3. use super::{EntityMoved, Position, EntryTrigger, Hidden, Map, Name, gamelog::GameLog,
  4. InflictsDamage, particle_system::ParticleBuilder, SufferDamage, SingleActivation};
  5. pub struct TriggerSystem {}
  6. impl<'a> System<'a> for TriggerSystem {
  7. #[allow(clippy::type_complexity)]
  8. type SystemData = ( ReadExpect<'a, Map>,
  9. WriteStorage<'a, EntityMoved>,
  10. ReadStorage<'a, Position>,
  11. ReadStorage<'a, EntryTrigger>,
  12. WriteStorage<'a, Hidden>,
  13. ReadStorage<'a, Name>,
  14. Entities<'a>,
  15. WriteExpect<'a, GameLog>,
  16. ReadStorage<'a, InflictsDamage>,
  17. WriteExpect<'a, ParticleBuilder>,
  18. WriteStorage<'a, SufferDamage>,
  19. ReadStorage<'a, SingleActivation>);
  20. fn run(&mut self, data : Self::SystemData) {
  21. let (map, mut entity_moved, position, entry_trigger, mut hidden,
  22. names, entities, mut log, inflicts_damage, mut particle_builder,
  23. mut inflict_damage, single_activation) = data;
  24. // Iterate the entities that moved and their final position
  25. let mut remove_entities : Vec<Entity> = Vec::new();
  26. for (entity, mut _entity_moved, pos) in (&entities, &mut entity_moved, &position).join() {
  27. let idx = map.xy_idx(pos.x, pos.y);
  28. for entity_id in map.tile_content[idx].iter() {
  29. if entity != *entity_id { // Do not bother to check yourself for being a trap!
  30. let maybe_trigger = entry_trigger.get(*entity_id);
  31. match maybe_trigger {
  32. None => {},
  33. Some(_trigger) => {
  34. // We triggered it
  35. let name = names.get(*entity_id);
  36. if let Some(name) = name {
  37. log.entries.insert(0, format!("{} triggers!", &name.name));
  38. }
  39. hidden.remove(*entity_id); // The trap is no longer hidden
  40. // If the trap is damage inflicting, do it
  41. let damage = inflicts_damage.get(*entity_id);
  42. if let Some(damage) = damage {
  43. particle_builder.request(pos.x, pos.y, rltk::RGB::named(rltk::ORANGE), rltk::RGB::named(rltk::BLACK), rltk::to_cp437('‼'), 200.0);
  44. inflict_damage.insert(entity, SufferDamage{ amount: damage.damage }).expect("Unable to do damage");
  45. }
  46. // If it is single activation, it needs to be removed
  47. let sa = single_activation.get(*entity_id);
  48. if let Some(_sa) = sa {
  49. remove_entities.push(*entity_id);
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. // Remove any single activation traps
  57. for trap in remove_entities.iter() {
  58. entities.delete(*trap).expect("Unable to delete trap");
  59. }
  60. // Remove all entity movement markers
  61. entity_moved.clear();
  62. }
  63. }