From 95298fbc00cde40fe01f9df8182c312d5d2ecdb7 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 11 Sep 2023 00:21:11 -0500 Subject: [PATCH] feat: keycode to direction utility function --- src/direction.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/direction.rs b/src/direction.rs index 1112510..cf6b8d3 100644 --- a/src/direction.rs +++ b/src/direction.rs @@ -1,3 +1,5 @@ +use sdl2::keyboard::Keycode; + #[derive(Debug, Copy, Clone, PartialEq)] pub enum Direction { Up, @@ -24,4 +26,18 @@ impl Direction { Direction::Up => (0, -1), } } + + pub fn from_keycode(keycode: Keycode) -> Option { + match keycode { + Keycode::D => Some(Direction::Right), + Keycode::Right => Some(Direction::Right), + Keycode::A => Some(Direction::Left), + Keycode::Left => Some(Direction::Left), + Keycode::W => Some(Direction::Up), + Keycode::Up => Some(Direction::Up), + Keycode::S => Some(Direction::Down), + Keycode::Down => Some(Direction::Down), + _ => None, + } + } } \ No newline at end of file