From 86cdc6c567d0ca429d69bebbb2fb02f1d3754c8d Mon Sep 17 00:00:00 2001 From: troido Date: Sun, 23 Feb 2020 23:44:50 +0100 Subject: players can now attack other objects --- src/systems/fight.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/systems/fight.rs (limited to 'src/systems/fight.rs') 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; + } + } + } + } + _ => {} + } + } + } +} -- cgit