input.rs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. use crate::com::{Controlled, Velocity};
  2. use crate::game::MyGame;
  3. use crate::res::KeySet;
  4. use sdl2::keyboard as sdl;
  5. use specs::RunNow;
  6. pub struct Move;
  7. impl<'a> specs::System<'a> for Move {
  8. type SystemData = (
  9. specs::ReadStorage<'a, Controlled>,
  10. specs::WriteStorage<'a, Velocity>,
  11. specs::Read<'a, KeySet>,
  12. );
  13. fn run(&mut self, (movable, mut velocity, keys): Self::SystemData) {
  14. use specs::Join;
  15. for (_, vel) in (&movable, &mut velocity).join() {
  16. vel.dx = 0.0;
  17. vel.dy = 0.0;
  18. if keys.contains(&sdl::Keycode::W) {
  19. vel.dy -= 2.0;
  20. }
  21. if keys.contains(&sdl::Keycode::A) {
  22. vel.dx -= 2.0;
  23. }
  24. if keys.contains(&sdl::Keycode::S) {
  25. vel.dy += 2.0;
  26. }
  27. if keys.contains(&sdl::Keycode::D) {
  28. vel.dx += 2.0;
  29. }
  30. }
  31. }
  32. }
  33. pub fn systems(game: &mut MyGame) {
  34. Move.run_now(&game.world.res);
  35. }