diff options
| author | troido <troido@protonmail.com> | 2020-03-03 16:47:11 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-03-03 16:47:11 +0100 |
| commit | 9954b5cbaab27aaffcafa8723dcd5d1c99fa811f (patch) | |
| tree | 0133821c4fd3dfb7c5500204efaaa02f9bcc57fe | |
| parent | b98ea15acbebe8815f9316b5a8ef3393fdd1c3e6 (diff) | |
implemented autoretaliate
| -rw-r--r-- | src/systems/attacking.rs | 19 | ||||
| -rw-r--r-- | src/systems/fight.rs | 7 |
2 files changed, 19 insertions, 7 deletions
diff --git a/src/systems/attacking.rs b/src/systems/attacking.rs index 6075bd5..9d10fee 100644 --- a/src/systems/attacking.rs +++ b/src/systems/attacking.rs @@ -9,7 +9,7 @@ use specs::{ }; use crate::{ - components::{Health, AttackInbox, Dead, Position}, + components::{Health, AttackInbox, Dead, Position, Autofight}, resources::NewEntities, Template, util @@ -24,9 +24,22 @@ impl <'a> System<'a> for Attacking { WriteStorage<'a, Health>, WriteStorage<'a, Dead>, ReadStorage<'a, Position>, - Write<'a, NewEntities> + Write<'a, NewEntities>, + WriteStorage<'a, Autofight> ); - fn run(&mut self, (entities, mut attackeds, mut healths, mut deads, positions, mut new): Self::SystemData) { + fn run(&mut self, (entities, mut attackeds, mut healths, mut deads, positions, mut new, mut autofighters): Self::SystemData) { + + for (entity, attacked, autofighter) in (&entities, &attackeds, &mut autofighters).join() { + for attack in &attacked.messages { + if attack.damage > 0 { + if let Some(attacker) = attack.attacker { + if healths.contains(attacker) && attacker != entity { + autofighter.target = Some(attacker); + } + } + } + } + } for (ent, health, attacked) in (&entities, &mut healths, &mut attackeds).join() { let mut wounded = false; for attack in attacked.messages.drain(..) { diff --git a/src/systems/fight.rs b/src/systems/fight.rs index 703ea4a..88305e2 100644 --- a/src/systems/fight.rs +++ b/src/systems/fight.rs @@ -54,9 +54,6 @@ impl <'a> System<'a> for Fight { } Control::AttackTarget(t) => { if *t == entity { // don't knock yourself out - if let Some(autofighter) = autofighters.get_mut(entity){ - autofighter.target = None; - } } else if let Some(target_position) = positions.get(*t){ if position.pos.distance_to(target_position.pos) <= fighter.range { target = Some(*t); @@ -66,7 +63,9 @@ impl <'a> System<'a> for Fight { _ => {} } if let Some(ent) = target { - AttackInbox::add_message(&mut attacked, ent, fighter.attack.clone()); + let mut attack = fighter.attack.clone(); + attack.attacker = Some(entity); + AttackInbox::add_message(&mut attacked, ent, attack); cooldowns.insert(entity, ControlCooldown{amount: fighter.cooldown}).unwrap(); if let Some(autofighter) = autofighters.get_mut(entity){ autofighter.target = Some(ent); |
