From f0153eefd580ec443b380504303620a61f24630b Mon Sep 17 00:00:00 2001 From: troido Date: Mon, 3 Feb 2020 16:13:58 +0100 Subject: refactored systems in their own file each --- src/systems/controlinput.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/systems/controlinput.rs (limited to 'src/systems/controlinput.rs') 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); + } + } + } +} + -- cgit