diff options
| author | troido <troido@protonmail.com> | 2020-02-23 21:25:36 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-23 21:25:36 +0100 |
| commit | 613952f918b8d72a3e397dc46be309b2320c6ad0 (patch) | |
| tree | 2f34c1d8fb2aed771a5714a266845df095e5b438 /src/systems | |
| parent | 9a814769565ab36c227508c47792e112de338df1 (diff) | |
entities can be attacked
Diffstat (limited to 'src/systems')
| -rw-r--r-- | src/systems/attacking.rs | 30 | ||||
| -rw-r--r-- | src/systems/mod.rs | 4 | ||||
| -rw-r--r-- | src/systems/useitem.rs | 37 |
3 files changed, 55 insertions, 16 deletions
diff --git a/src/systems/attacking.rs b/src/systems/attacking.rs new file mode 100644 index 0000000..0fb5cf7 --- /dev/null +++ b/src/systems/attacking.rs @@ -0,0 +1,30 @@ + +use specs::{ + WriteStorage, + System, + Join +}; + +use crate::{ + components::{Health, Attacked}, + util +}; + + +pub struct Attacking; +impl <'a> System<'a> for Attacking { + type SystemData = ( + WriteStorage<'a, Attacked>, + WriteStorage<'a, Health> + ); + fn run(&mut self, (mut victims, mut healths): Self::SystemData) { + + for (health, attacked) in (&mut healths, &mut victims).join() { + for attack in attacked.attacks.drain(..) { + health.health -= attack.damage; + } + health.health = util::clamp(health.health, 0, health.maxhealth); + } + } +} + diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 88e67ad..f93088d 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -8,6 +8,7 @@ mod create; mod take; mod migrate; mod useitem; +mod attacking; pub use self::{ controlinput::ControlInput, @@ -18,5 +19,6 @@ pub use self::{ create::Create, take::Take, migrate::Migrate, - useitem::Use + useitem::Use, + attacking::Attacking }; diff --git a/src/systems/useitem.rs b/src/systems/useitem.rs index d77239c..4317162 100644 --- a/src/systems/useitem.rs +++ b/src/systems/useitem.rs @@ -1,6 +1,7 @@ use specs::{ + Entities, ReadStorage, WriteStorage, System, @@ -8,30 +9,33 @@ use specs::{ Write }; -use crate::components::{ - Controller, - Position, - Inventory, - Health +use crate::{ + components::{ + Controller, + Position, + Inventory, + Attacked + }, + resources::{NewEntities}, + components::item::ItemAction::{None, Build, Eat}, + controls::Control, + attack::Attack }; -use crate::resources::{NewEntities}; -use crate::components::item::ItemAction::{None, Build, Eat}; -use crate::controls::Control; - pub struct Use; impl <'a> System<'a> for Use { type SystemData = ( + Entities<'a>, ReadStorage<'a, Controller>, WriteStorage<'a, Position>, WriteStorage<'a, Inventory>, Write<'a, NewEntities>, - WriteStorage<'a, Health> + WriteStorage<'a, Attacked> ); - fn run(&mut self, (controllers, positions, mut inventories, mut new, mut healths): Self::SystemData) { - for (controller, position, inventory, maybe_health) in (&controllers, &positions, &mut inventories, (&mut healths).maybe()).join(){ + fn run(&mut self, (entities, controllers, positions, mut inventories, mut new, mut attacked): Self::SystemData) { + for (ent, controller, position, inventory) in (&entities, &controllers, &positions, &mut inventories).join(){ match &controller.0 { Control::Use(rank) => { if let Some(item) = inventory.items.get(*rank) { @@ -41,9 +45,12 @@ impl <'a> System<'a> for Use { inventory.items.remove(*rank); } Eat(health_diff) => { - if let Some(health) = maybe_health { - health.heal(*health_diff); - } + attacked + .entry(ent) + .unwrap() + .or_insert_with(Attacked::default) + .attacks + .push(Attack::new(-*health_diff)); inventory.items.remove(*rank); } None => {} |
