summaryrefslogtreecommitdiff
path: root/src/systems
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-03-02 00:45:21 +0100
committertroido <troido@protonmail.com>2020-03-02 00:45:21 +0100
commitd246537a28a7a71dfb2487d31d6fac3ccab5053d (patch)
tree885f89ee5605659edd7359279cf6d046ebb3fa3f /src/systems
parent32037d2247a4db1c1c09d63a7849f562b6081bfe (diff)
implemented autofight
Diffstat (limited to 'src/systems')
-rw-r--r--src/systems/controlinput.rs25
-rw-r--r--src/systems/fight.rs20
2 files changed, 35 insertions, 10 deletions
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();
+ }
+ }
}
}
}
diff --git a/src/systems/fight.rs b/src/systems/fight.rs
index 2a663e3..182dba6 100644
--- a/src/systems/fight.rs
+++ b/src/systems/fight.rs
@@ -15,7 +15,8 @@ use crate::components::{
AttackInbox,
Fighter,
Health,
- ControlCooldown
+ ControlCooldown,
+ Autofight
};
use crate::controls::{Control};
@@ -33,25 +34,34 @@ impl <'a> System<'a> for Fight {
WriteStorage<'a, AttackInbox>,
ReadStorage<'a, Fighter>,
ReadStorage<'a, Health>,
- WriteStorage<'a, ControlCooldown>
+ WriteStorage<'a, ControlCooldown>,
+ WriteStorage<'a, Autofight>
);
- fn run(&mut self, (entities, controllers, positions, ground, mut attacked, fighters, healths, mut cooldowns): Self::SystemData) {
+ fn run(&mut self, (entities, controllers, positions, ground, mut attacked, fighters, healths, mut cooldowns, mut autofighters): Self::SystemData) {
for (entity, controller, position, fighter) in (&entities, &controllers, &positions, &fighters).join(){
+ let mut target = None;
match &controller.control {
Control::Attack(directions) => {
'targets: for direction in directions {
for ent in ground.cells.get(&(position.pos + direction.to_position())).unwrap_or(&HashSet::new()) {
if healths.contains(*ent) && *ent != entity {
- AttackInbox::add_message(&mut attacked, *ent, fighter.attack.clone());
- cooldowns.insert(entity, ControlCooldown{amount: fighter.cooldown}).unwrap();
+ target = Some(*ent);
break 'targets;
}
}
}
}
+ Control::AttackTarget(t) => {target = Some(*t);}
_ => {}
}
+ if let Some(ent) = target {
+ AttackInbox::add_message(&mut attacked, ent, fighter.attack.clone());
+ cooldowns.insert(entity, ControlCooldown{amount: fighter.cooldown}).unwrap();
+ if let Some(autofighter) = autofighters.get_mut(entity){
+ autofighter.target = Some(ent);
+ }
+ }
}
}
}