input.rs 1.0 KB

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