2021-11-15 09:45:12 -05:00
|
|
|
use crate::components::{
|
|
|
|
CombatStats, DefenseBonus, Equipped, MeleePowerBonus, Name, SufferDamage, WantsToMelee,
|
|
|
|
};
|
|
|
|
use crate::game_log::GameLog;
|
2021-10-29 15:15:22 -04:00
|
|
|
use specs::prelude::*;
|
|
|
|
|
|
|
|
pub struct MeleeCombatSystem {}
|
|
|
|
|
|
|
|
impl<'a> System<'a> for MeleeCombatSystem {
|
|
|
|
type SystemData = (
|
|
|
|
Entities<'a>,
|
2021-11-01 14:46:45 -04:00
|
|
|
WriteExpect<'a, GameLog>,
|
2021-10-29 15:15:22 -04:00
|
|
|
WriteStorage<'a, WantsToMelee>,
|
|
|
|
ReadStorage<'a, Name>,
|
|
|
|
ReadStorage<'a, CombatStats>,
|
|
|
|
WriteStorage<'a, SufferDamage>,
|
2021-11-15 09:45:12 -05:00
|
|
|
ReadStorage<'a, MeleePowerBonus>,
|
|
|
|
ReadStorage<'a, DefenseBonus>,
|
|
|
|
ReadStorage<'a, Equipped>,
|
2021-10-29 15:15:22 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
fn run(&mut self, data: Self::SystemData) {
|
2021-11-15 09:45:12 -05:00
|
|
|
let (
|
|
|
|
entities,
|
|
|
|
mut log,
|
|
|
|
mut wants_melee,
|
|
|
|
names,
|
|
|
|
combat_stats,
|
|
|
|
mut inflict_damage,
|
|
|
|
melee_power_bonuses,
|
|
|
|
defense_bonuses,
|
|
|
|
equipped,
|
|
|
|
) = data;
|
2021-10-29 15:15:22 -04:00
|
|
|
|
2021-11-15 09:45:12 -05:00
|
|
|
for (entity, wants_melee, name, stats) in
|
2021-10-29 15:15:22 -04:00
|
|
|
(&entities, &wants_melee, &names, &combat_stats).join()
|
|
|
|
{
|
|
|
|
if stats.hp > 0 {
|
2021-11-15 09:45:12 -05:00
|
|
|
let mut offensive_bonus = 0;
|
|
|
|
for (_item_entity, power_bonus, equipped_by) in
|
|
|
|
(&entities, &melee_power_bonuses, &equipped).join()
|
|
|
|
{
|
|
|
|
if equipped_by.owner == entity {
|
|
|
|
offensive_bonus += power_bonus.power;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 15:15:22 -04:00
|
|
|
let target_stats = combat_stats.get(wants_melee.target).unwrap();
|
|
|
|
if target_stats.hp > 0 {
|
|
|
|
let target_name = names.get(wants_melee.target).unwrap();
|
2021-11-15 09:45:12 -05:00
|
|
|
|
|
|
|
let mut defensive_bonus = 0;
|
|
|
|
for (_item_entity, defense_bonus, equipped_by) in
|
|
|
|
(&entities, &defense_bonuses, &equipped).join()
|
|
|
|
{
|
|
|
|
if equipped_by.owner == wants_melee.target {
|
|
|
|
defensive_bonus += defense_bonus.defense;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let damage = i32::max(
|
|
|
|
0,
|
|
|
|
(stats.power + offensive_bonus) - (target_stats.defense + defensive_bonus),
|
|
|
|
);
|
2021-10-29 15:15:22 -04:00
|
|
|
|
|
|
|
if damage == 0 {
|
2021-11-01 14:46:45 -04:00
|
|
|
log.entries.push(format!(
|
2021-10-29 15:15:22 -04:00
|
|
|
"{} is unable to hurt {}",
|
|
|
|
&name.name, &target_name.name
|
|
|
|
));
|
|
|
|
} else {
|
2021-11-01 14:46:45 -04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|