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(WantsToAttack)]
#[read_component(Player)] #[read_component(Player)]
#[write_component(Health)] #[write_component(Health)]
#[read_component(Damage)]
#[read_component(Carried)]
pub fn combat(ecs: &mut SubWorld, commands: &mut CommandBuffer) { pub fn combat(ecs: &mut SubWorld, commands: &mut CommandBuffer) {
let mut attackers = <(Entity, &WantsToAttack)>::query(); let mut attackers = <(Entity, &WantsToAttack)>::query();
let victims: Vec<(Entity, Entity)> = attackers let victims: Vec<(Entity, Entity, Entity)> = attackers
.iter(ecs) .iter(ecs)
.map(|(entity, attack)| (*entity, attack.victim)) .map(|(entity, attack)| (*entity, attack.attacker, attack.victim))
.collect(); .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 let is_player = ecs
.entry_ref(*victim) .entry_ref(*victim)
.unwrap() .unwrap()
@ -24,12 +44,10 @@ pub fn combat(ecs: &mut SubWorld, commands: &mut CommandBuffer) {
.unwrap() .unwrap()
.get_component_mut::<Health>() .get_component_mut::<Health>()
{ {
println!("Health before attack: {}", health.current); health.current -= final_damage;
health.current -= 1;
if health.current < 1 && !is_player { if health.current < 1 && !is_player {
commands.remove(*victim); commands.remove(*victim);
} }
println!("Health after attack: {}", health.current);
} }
commands.remove(*message); commands.remove(*message);
}); });