diff options
| author | troido <troido@protonmail.com> | 2020-03-04 12:15:58 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-03-04 12:15:58 +0100 |
| commit | 21919636f95a1214b7ed1a3e4aa6527f45b9d073 (patch) | |
| tree | dfe7a465cdf81048c3ac04b5719571bb0192791f /src/systems/interact.rs | |
| parent | daae8b511291b8cc854a8747e64e76c6ff2d462e (diff) | |
interacting now works
Diffstat (limited to 'src/systems/interact.rs')
| -rw-r--r-- | src/systems/interact.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/systems/interact.rs b/src/systems/interact.rs new file mode 100644 index 0000000..4c0245d --- /dev/null +++ b/src/systems/interact.rs @@ -0,0 +1,63 @@ + +use std::collections::HashSet; +use specs::{ + Entities, + ReadStorage, + WriteStorage, + System, + Join, + Read +}; + +use crate::components::{ + Controller, + Position, + ControlCooldown, + Interactable, + Dead +}; + +use crate::controls::{Control}; +use crate::resources::{Ground}; + + + +pub struct Interact; +impl <'a> System<'a> for Interact { + type SystemData = ( + Entities<'a>, + ReadStorage<'a, Controller>, + ReadStorage<'a, Position>, + Read<'a, Ground>, + WriteStorage<'a, ControlCooldown>, + ReadStorage<'a, Interactable>, + WriteStorage<'a, Dead> + ); + + fn run(&mut self, (entities, controllers, positions, ground, mut cooldowns, interactables, mut deads): Self::SystemData) { + for (entity, controller, position) in (&entities, &controllers, &positions).join(){ + let mut target = None; + match &controller.control { + Control::Interact(directions) => { + 'targets: for direction in directions { + for ent in ground.cells.get(&(position.pos + direction.to_position())).unwrap_or(&HashSet::new()) { + if let Some(interactable) = interactables.get(*ent) { + target = Some((*ent, interactable)); + break 'targets; + } + } + } + } + _ => {} + } + if let Some((ent, interactable)) = target { + match interactable { + Interactable::Harvest => { + deads.insert(ent, Dead).unwrap(); + } + } + cooldowns.insert(entity, ControlCooldown{amount: 2}).unwrap(); + } + } + } +} |
