use crate::components::{Movable, Velocity}; use crate::game::MyGame; use sdl2::keyboard as sdl; use specs::RunNow; pub struct Move<'t> { keys: &'t std::collections::HashSet, } impl<'a, 't> specs::System<'a> for Move<'t> { type SystemData = ( specs::ReadStorage<'a, Movable>, specs::WriteStorage<'a, Velocity>, ); fn run(&mut self, (movable, mut velocity): Self::SystemData) { use specs::Join; for (_, vel) in (&movable, &mut velocity).join() { vel.dx = 0.0; vel.dy = 0.0; if self.keys.contains(&sdl::Keycode::W) { vel.dy -= 2.0; } if self.keys.contains(&sdl::Keycode::A) { vel.dx -= 2.0; } if self.keys.contains(&sdl::Keycode::S) { vel.dy += 2.0; } if self.keys.contains(&sdl::Keycode::D) { vel.dx += 2.0; } } } } pub fn systems(game: &mut MyGame) { Move { keys: &game.keys }.run_now(&game.world.res); }