input.rs 717 B

12345678910111213141516171819202122232425262728
  1. use crate::components::{Movable, Position};
  2. use sdl2::keyboard as sdl;
  3. pub struct Move {
  4. pub keycode: sdl::Keycode,
  5. }
  6. impl<'a> specs::System<'a> for Move {
  7. type SystemData = (
  8. specs::ReadStorage<'a, Movable>,
  9. specs::WriteStorage<'a, Position>,
  10. );
  11. fn run(&mut self, (movable, mut position): Self::SystemData) {
  12. use specs::Join;
  13. for (_, pos) in (&movable, &mut position).join() {
  14. match self.keycode {
  15. sdl::Keycode::W => pos.y -= 1.0,
  16. sdl::Keycode::A => pos.x -= 1.0,
  17. sdl::Keycode::S => pos.y += 1.0,
  18. sdl::Keycode::D => pos.x += 1.0,
  19. _ => (),
  20. }
  21. }
  22. }
  23. }