From 88f275bc427033b7981e0dc2fc5cb4b711fd5fb1 Mon Sep 17 00:00:00 2001 From: troido Date: Wed, 29 Jan 2020 00:13:03 +0100 Subject: refactored control --- src/systems.rs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'src/systems.rs') diff --git a/src/systems.rs b/src/systems.rs index 322ad23..d00abc8 100644 --- a/src/systems.rs +++ b/src/systems.rs @@ -3,6 +3,7 @@ use specs::{ ReadStorage, WriteStorage, Write, + Entities, System, Join }; @@ -47,17 +48,30 @@ impl <'a> System<'a> for Move { type SystemData = (WriteStorage<'a, Controller>, WriteStorage<'a, Position>); fn run(&mut self, (mut controller, mut pos): Self::SystemData) { for (controller, pos) in (&mut controller, &mut pos).join(){ - if let Some(control) = &controller.0 { - match control { - Control::Move(direction) => { - let (dx, dy) = direction.to_position(); - pos.x += dx; - pos.y += dy; - } - _ => {} + match &controller.0 { + Control::Move(direction) => { + let (dx, dy) = direction.to_position(); + pos.x += dx; + pos.y += dy; } - controller.0 = None + _ => {} } } } } + + +pub struct ClearControllers; +impl <'a> System<'a> for ClearControllers { + type SystemData = (Entities<'a>, WriteStorage<'a, Controller>); + fn run(&mut self, (entities, mut controllers): Self::SystemData) { + let mut ents = Vec::new(); + for (ent, _controller) in (&*entities, &controllers).join() { + ents.push(ent); + } + for ent in ents { + controllers.remove(ent); + } + } +} + -- cgit