diff options
| author | troido <troido@protonmail.com> | 2020-02-23 23:44:50 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-23 23:44:50 +0100 |
| commit | 86cdc6c567d0ca429d69bebbb2fb02f1d3754c8d (patch) | |
| tree | d70eb9d9f014c9cdbf301772c339f22ee5e3ab82 /src/systems/fight.rs | |
| parent | f422238d7aaae0ff1b2d560a71a99b0a881ad338 (diff) | |
players can now attack other objects
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; + } + } + } + } + _ => {} + } + } + } +} |
