From d246537a28a7a71dfb2487d31d6fac3ccab5053d Mon Sep 17 00:00:00 2001 From: troido Date: Mon, 2 Mar 2020 00:45:21 +0100 Subject: implemented autofight --- src/systems/controlinput.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'src/systems/controlinput.rs') diff --git a/src/systems/controlinput.rs b/src/systems/controlinput.rs index 17bc40f..6d5b4ea 100644 --- a/src/systems/controlinput.rs +++ b/src/systems/controlinput.rs @@ -8,8 +8,11 @@ use specs::{ Join }; -use crate::components::{Controller, Player, ControlCooldown}; -use crate::resources::{Input}; +use crate::{ + components::{Controller, Player, ControlCooldown, Autofight}, + resources::{Input}, + controls::Control +}; pub struct ControlInput; @@ -19,14 +22,26 @@ impl <'a> System<'a> for ControlInput { Write<'a, Input>, WriteStorage<'a, Controller>, ReadStorage<'a, Player>, - ReadStorage<'a, ControlCooldown> + ReadStorage<'a, ControlCooldown>, + WriteStorage<'a, Autofight> ); - fn run(&mut self, (entities, mut input, mut controllers, players, cooldowns): Self::SystemData) { + fn run(&mut self, (entities, mut input, mut controllers, players, cooldowns, mut autofighters): Self::SystemData) { controllers.clear(); for (player, entity, ()) in (&players, &entities, !&cooldowns).join() { if let Some(control) = input.actions.remove(&player.id){ - let _ = controllers.insert(entity, Controller{control: control}); + controllers.insert(entity, Controller{control: control}).unwrap(); + if let Some(autofighter) = autofighters.get_mut(entity) { + autofighter.target = None; + } + } else if let Some(autofighter) = autofighters.get_mut(entity) { + if let Some(target) = autofighter.target { + if !entities.is_alive(target) { + autofighter.target = None; + } else { + controllers.insert(entity, Controller{control: Control::AttackTarget(target)}).unwrap(); + } + } } } } -- cgit