melee_combat_system.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. extern crate specs;
  2. use specs::prelude::*;
  3. use super::{CombatStats, WantsToMelee, Name, SufferDamage, gamelog::GameLog};
  4. pub struct MeleeCombatSystem {}
  5. impl<'a> System<'a> for MeleeCombatSystem {
  6. #[allow(clippy::type_complexity)]
  7. type SystemData = ( Entities<'a>,
  8. WriteExpect<'a, GameLog>,
  9. WriteStorage<'a, WantsToMelee>,
  10. ReadStorage<'a, Name>,
  11. ReadStorage<'a, CombatStats>,
  12. WriteStorage<'a, SufferDamage>
  13. );
  14. fn run(&mut self, data : Self::SystemData) {
  15. let (entities, mut log, mut wants_melee, names, combat_stats, mut inflict_damage) = data;
  16. for (_entity, wants_melee, name, stats) in (&entities, &wants_melee, &names, &combat_stats).join() {
  17. if stats.hp > 0 {
  18. let target_stats = combat_stats.get(wants_melee.target).unwrap();
  19. if target_stats.hp > 0 {
  20. let target_name = names.get(wants_melee.target).unwrap();
  21. let damage = i32::max(0, stats.power - target_stats.defense);
  22. if damage == 0 {
  23. log.entries.insert(0, format!("{} is unable to hurt {}", &name.name, &target_name.name));
  24. } else {
  25. log.entries.insert(0, format!("{} hits {}, for {} hp.", &name.name, &target_name.name, damage));
  26. inflict_damage.insert(wants_melee.target, SufferDamage{ amount: damage }).expect("Unable to do damage");
  27. }
  28. }
  29. }
  30. }
  31. wants_melee.clear();
  32. }
  33. }