roguelike-game/src/melee_combat_system.rs

47 lines
1.6 KiB
Rust
Raw Normal View History

2021-11-04 10:44:52 -04:00
use crate::{game_log::GameLog, CombatStats, Name, SufferDamage, WantsToMelee};
2021-10-29 15:15:22 -04:00
use specs::prelude::*;
pub struct MeleeCombatSystem {}
impl<'a> System<'a> for MeleeCombatSystem {
type SystemData = (
Entities<'a>,
WriteExpect<'a, GameLog>,
2021-10-29 15:15:22 -04:00
WriteStorage<'a, WantsToMelee>,
ReadStorage<'a, Name>,
ReadStorage<'a, CombatStats>,
WriteStorage<'a, SufferDamage>,
);
fn run(&mut self, data: Self::SystemData) {
let (entities, mut log, mut wants_melee, names, combat_stats, mut inflict_damage) = data;
2021-10-29 15:15:22 -04:00
for (_entity, wants_melee, name, stats) in
(&entities, &wants_melee, &names, &combat_stats).join()
{
if stats.hp > 0 {
let target_stats = combat_stats.get(wants_melee.target).unwrap();
if target_stats.hp > 0 {
let target_name = names.get(wants_melee.target).unwrap();
let damage = i32::max(0, stats.power - target_stats.defense);
if damage == 0 {
log.entries.push(format!(
2021-10-29 15:15:22 -04:00
"{} is unable to hurt {}",
&name.name, &target_name.name
));
} else {
log.entries.push(format!(
2021-10-29 15:15:22 -04:00
"{} hits {}, for {} hp",
&name.name, &target_name.name, damage
));
SufferDamage::new_damage(&mut inflict_damage, wants_melee.target, damage);
}
}
}
}
wants_melee.clear();
}
}