Add new damage/weapon setup to combat system

This commit is contained in:
Daniel Lynn 2021-07-11 11:34:02 -05:00
parent b6e044a8fe
commit b9f8b7ebbe

View File

@ -4,15 +4,35 @@ use crate::prelude::*;
#[read_component(WantsToAttack)]
#[read_component(Player)]
#[write_component(Health)]
#[read_component(Damage)]
#[read_component(Carried)]
pub fn combat(ecs: &mut SubWorld, commands: &mut CommandBuffer) {
let mut attackers = <(Entity, &WantsToAttack)>::query();
let victims: Vec<(Entity, Entity)> = attackers
let victims: Vec<(Entity, Entity, Entity)> = attackers
.iter(ecs)
.map(|(entity, attack)| (*entity, attack.victim))
.map(|(entity, attack)| (*entity, attack.attacker, attack.victim))
.collect();
victims.iter().for_each(|(message, victim)| {
victims.iter().for_each(|(message, attacker, victim)| {
let base_damage = if let Ok(v) = ecs.entry_ref(*attacker) {
if let Ok(dmg) = v.get_component::<Damage>() {
dmg.0
} else {
0
}
} else {
0
};
let weapon_damage: i32 = <(&Carried, &Damage)>::query()
.iter(ecs)
.filter(|(carried, _)| carried.0 == *attacker)
.map(|(_, dmg)| dmg.0)
.sum();
let final_damage = base_damage + weapon_damage;
let is_player = ecs
.entry_ref(*victim)
.unwrap()
@ -24,12 +44,10 @@ pub fn combat(ecs: &mut SubWorld, commands: &mut CommandBuffer) {
.unwrap()
.get_component_mut::<Health>()
{
println!("Health before attack: {}", health.current);
health.current -= 1;
health.current -= final_damage;
if health.current < 1 && !is_player {
commands.remove(*victim);
}
println!("Health after attack: {}", health.current);
}
commands.remove(*message);
});