melee_combat_system.rs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. extern crate specs;
  2. use specs::prelude::*;
  3. use super::{CombatStats, WantsToMelee, Name, SufferDamage, gamelog::GameLog, MeleePowerBonus, DefenseBonus, Equipped,
  4. particle_system::ParticleBuilder, Position, HungerClock, HungerState};
  5. pub struct MeleeCombatSystem {}
  6. impl<'a> System<'a> for MeleeCombatSystem {
  7. #[allow(clippy::type_complexity)]
  8. type SystemData = ( Entities<'a>,
  9. WriteExpect<'a, GameLog>,
  10. WriteStorage<'a, WantsToMelee>,
  11. ReadStorage<'a, Name>,
  12. ReadStorage<'a, CombatStats>,
  13. WriteStorage<'a, SufferDamage>,
  14. ReadStorage<'a, MeleePowerBonus>,
  15. ReadStorage<'a, DefenseBonus>,
  16. ReadStorage<'a, Equipped>,
  17. WriteExpect<'a, ParticleBuilder>,
  18. ReadStorage<'a, Position>,
  19. ReadStorage<'a, HungerClock>
  20. );
  21. fn run(&mut self, data : Self::SystemData) {
  22. let (entities, mut log, mut wants_melee, names, combat_stats, mut inflict_damage,
  23. melee_power_bonuses, defense_bonuses, equipped, mut particle_builder, positions, hunger_clock) = data;
  24. for (entity, wants_melee, name, stats) in (&entities, &wants_melee, &names, &combat_stats).join() {
  25. if stats.hp > 0 {
  26. let mut offensive_bonus = 0;
  27. for (_item_entity, power_bonus, equipped_by) in (&entities, &melee_power_bonuses, &equipped).join() {
  28. if equipped_by.owner == entity {
  29. offensive_bonus += power_bonus.power;
  30. }
  31. }
  32. let hc = hunger_clock.get(entity);
  33. if let Some(hc) = hc {
  34. if hc.state == HungerState::WellFed {
  35. offensive_bonus += 1;
  36. }
  37. }
  38. let target_stats = combat_stats.get(wants_melee.target).unwrap();
  39. if target_stats.hp > 0 {
  40. let target_name = names.get(wants_melee.target).unwrap();
  41. let mut defensive_bonus = 0;
  42. for (_item_entity, defense_bonus, equipped_by) in (&entities, &defense_bonuses, &equipped).join() {
  43. if equipped_by.owner == wants_melee.target {
  44. defensive_bonus += defense_bonus.defense;
  45. }
  46. }
  47. let pos = positions.get(wants_melee.target);
  48. if let Some(pos) = pos {
  49. particle_builder.request(pos.x, pos.y, rltk::RGB::named(rltk::ORANGE), rltk::RGB::named(rltk::BLACK), rltk::to_cp437('‼'), 200.0);
  50. }
  51. let damage = i32::max(0, (stats.power + offensive_bonus) - (target_stats.defense + defensive_bonus));
  52. if damage == 0 {
  53. log.entries.insert(0, format!("{} is unable to hurt {}", &name.name, &target_name.name));
  54. } else {
  55. log.entries.insert(0, format!("{} hits {}, for {} hp.", &name.name, &target_name.name, damage));
  56. inflict_damage.insert(wants_melee.target, SufferDamage{ amount: damage }).expect("Unable to do damage");
  57. }
  58. }
  59. }
  60. }
  61. wants_melee.clear();
  62. }
  63. }