diff options
Diffstat (limited to 'src/systems')
| -rw-r--r-- | src/systems/attacking.rs | 16 | ||||
| -rw-r--r-- | src/systems/fight.rs | 55 | ||||
| -rw-r--r-- | src/systems/mod.rs | 4 | ||||
| -rw-r--r-- | src/systems/trapping.rs | 10 | ||||
| -rw-r--r-- | src/systems/useitem.rs | 10 |
5 files changed, 75 insertions, 20 deletions
diff --git a/src/systems/attacking.rs b/src/systems/attacking.rs index 6535a75..1f518cb 100644 --- a/src/systems/attacking.rs +++ b/src/systems/attacking.rs @@ -2,11 +2,12 @@ use specs::{ WriteStorage, System, + Entities, Join }; use crate::{ - components::{Health, Attacked}, + components::{Health, Attacked, Dying, Removed}, util }; @@ -14,15 +15,22 @@ use crate::{ pub struct Attacking; impl <'a> System<'a> for Attacking { type SystemData = ( + Entities<'a>, WriteStorage<'a, Attacked>, - WriteStorage<'a, Health> + WriteStorage<'a, Health>, + WriteStorage<'a, Dying>, + WriteStorage<'a, Removed> ); - fn run(&mut self, (mut victims, mut healths): Self::SystemData) { - for (health, attacked) in (&mut healths, &mut victims).join() { + fn run(&mut self, (entities, mut victims, mut healths, mut deads, mut removals): Self::SystemData) { + for (ent, health, attacked) in (&entities, &mut healths, &mut victims).join() { for attack in attacked.attacks.drain(..) { health.health -= attack.damage; } health.health = util::clamp(health.health, 0, health.maxhealth); + if health.health == 0 { + deads.insert(ent, Dying).unwrap(); + removals.insert(ent, Removed).unwrap(); + } } victims.clear(); } diff --git a/src/systems/fight.rs b/src/systems/fight.rs new file mode 100644 index 0000000..4b3c672 --- /dev/null +++ b/src/systems/fight.rs @@ -0,0 +1,55 @@ + +use std::collections::HashSet; +use specs::{ + Entities, + ReadStorage, + WriteStorage, + System, + Join, + Read +}; + +use crate::components::{ + Controller, + Position, + Attacked, + add_attack, + Fighter, + Health +}; + +use crate::controls::{Control}; +use crate::resources::{Ground}; + + + +pub struct Fight; +impl <'a> System<'a> for Fight { + type SystemData = ( + Entities<'a>, + ReadStorage<'a, Controller>, + WriteStorage<'a, Position>, + Read<'a, Ground>, + WriteStorage<'a, Attacked>, + ReadStorage<'a, Fighter>, + ReadStorage<'a, Health> + ); + + fn run(&mut self, (entities, controllers, positions, ground, mut attacked, fighters, healths): Self::SystemData) { + for (entity, controller, position, fighter) in (&entities, &controllers, &positions, &fighters).join(){ + match &controller.0 { + Control::Attack(directions) => { + 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 { + add_attack(&mut attacked, *ent, fighter.attack.clone()); + break; + } + } + } + } + _ => {} + } + } + } +} diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 7864b1c..d73a578 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -10,6 +10,7 @@ mod migrate; mod useitem; mod attacking; mod trapping; +mod fight; pub use self::{ controlinput::ControlInput, @@ -22,5 +23,6 @@ pub use self::{ migrate::Migrate, useitem::Use, attacking::Attacking, - trapping::Trapping + trapping::Trapping, + fight::Fight }; diff --git a/src/systems/trapping.rs b/src/systems/trapping.rs index 144cd94..5e0921e 100644 --- a/src/systems/trapping.rs +++ b/src/systems/trapping.rs @@ -9,7 +9,7 @@ use specs::{ }; use crate::{ - components::{Health, Attacked, Moved, Entered, Trap, Position}, + components::{Health, Attacked, add_attack, Moved, Entered, Trap, Position}, resources::Ground }; @@ -31,13 +31,7 @@ impl <'a> System<'a> for Trapping { for (entity, _entered, trap, position) in (&entities, &entereds, &traps, &positions).join() { for ent in ground.cells.get(&position.pos).unwrap(){ if ent != &entity && moves.contains(*ent) && healths.contains(*ent) { - victims - .entry(*ent) - .unwrap() - .or_insert_with(Attacked::default) - .attacks - .push(trap.attack.clone()); - + add_attack(&mut victims, *ent, trap.attack.clone()); } } } diff --git a/src/systems/useitem.rs b/src/systems/useitem.rs index 4317162..c9bcfcd 100644 --- a/src/systems/useitem.rs +++ b/src/systems/useitem.rs @@ -14,7 +14,8 @@ use crate::{ Controller, Position, Inventory, - Attacked + Attacked, + add_attack }, resources::{NewEntities}, components::item::ItemAction::{None, Build, Eat}, @@ -45,12 +46,7 @@ impl <'a> System<'a> for Use { inventory.items.remove(*rank); } Eat(health_diff) => { - attacked - .entry(ent) - .unwrap() - .or_insert_with(Attacked::default) - .attacks - .push(Attack::new(-*health_diff)); + add_attack(&mut attacked, ent, Attack::new(-*health_diff)); inventory.items.remove(*rank); } None => {} |
