diff options
Diffstat (limited to 'src/systems/fight.rs')
| -rw-r--r-- | src/systems/fight.rs | 55 |
1 files changed, 55 insertions, 0 deletions
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; + } + } + } + } + _ => {} + } + } + } +} |
