From 9954b5cbaab27aaffcafa8723dcd5d1c99fa811f Mon Sep 17 00:00:00 2001 From: troido Date: Tue, 3 Mar 2020 16:47:11 +0100 Subject: implemented autoretaliate --- src/systems/attacking.rs | 19 ++++++++++++++++--- src/systems/fight.rs | 7 +++---- 2 files changed, 19 insertions(+), 7 deletions(-) (limited to 'src/systems') 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); -- cgit