diff options
| author | troido <troido@protonmail.com> | 2020-02-03 16:13:58 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-03 16:13:58 +0100 |
| commit | f0153eefd580ec443b380504303620a61f24630b (patch) | |
| tree | 98fe9cb6a9039d50b180ea774396e2d162a41656 /src/systems/controlinput.rs | |
| parent | 4bb710c6c6df8a24a2efa8033ad3c17663345dbd (diff) | |
refactored systems in their own file each
Diffstat (limited to 'src/systems/controlinput.rs')
| -rw-r--r-- | src/systems/controlinput.rs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/systems/controlinput.rs b/src/systems/controlinput.rs new file mode 100644 index 0000000..663174e --- /dev/null +++ b/src/systems/controlinput.rs @@ -0,0 +1,57 @@ + +use std::collections::{HashMap, HashSet}; + +use specs::{ + ReadStorage, + WriteStorage, + Read, + Write, + Entities, + System, + Join +}; + +use super::super::components::{ + Controller, + Played, + Position +}; + +use super::super::controls::{ + Control, + Action +}; + +use super::super::resources::{ + Input, + NewEntities +}; + + +use super::super::assemblages::Player; + + +pub struct ControlInput; +impl <'a> System<'a> for ControlInput { + type SystemData = (Entities<'a>, Read<'a, Input>, WriteStorage<'a, Controller>, ReadStorage<'a, Played>, Write<'a, NewEntities>); + fn run(&mut self, (entities, input, mut controllers, players, mut new): Self::SystemData) { + let mut playercontrols: HashMap<&str, Control> = HashMap::new(); + let mut leaving = HashSet::new(); + for action in &input.actions { + match action { + Action::Join(name) => {new.assemblages.push((Position{x:10, y:10}, Box::new(Player::new(&name))));} + Action::Leave(name) => {leaving.insert(name);} + Action::Input(name, control) => {playercontrols.insert(name, control.clone());} + } + } + for (player, entity) in (&players, &entities).join() { + if let Some(control) = playercontrols.get(player.name.as_str()){ + let _ = controllers.insert(entity, Controller(control.clone())); + } + if leaving.contains(&player.name) { + let _ = entities.delete(entity); + } + } + } +} + |
