]> git.nega.tv - josh/narcissus/commitdiff
shark: Use match for key bindings
authorJosh Simmons <josh@nega.tv>
Mon, 13 May 2024 21:25:55 +0000 (23:25 +0200)
committerJosh Simmons <josh@nega.tv>
Mon, 13 May 2024 21:25:55 +0000 (23:25 +0200)
title/shark/src/main.rs

index 9e3d2c8428b72d16ebb4f35c3bd7822d27b1c8b6..37aef799bd36c27428d4c3933d223c60f682cb1b 100644 (file)
@@ -1217,46 +1217,23 @@ pub fn main() {
                                 continue 'poll_events;
                             }
 
-                            if key == Key::Escape {
-                                break 'main;
-                            }
-
-                            {
-                                let value = match pressed {
-                                    PressedState::Released => 0.0,
-                                    PressedState::Pressed => 1.0,
-                                };
-
-                                if key == Key::Left || key == Key::A {
-                                    action_queue.push(ActionEvent {
-                                        action: Action::Left,
-                                        value,
-                                    })
-                                }
-                                if key == Key::Right || key == Key::D {
-                                    action_queue.push(ActionEvent {
-                                        action: Action::Right,
-                                        value,
-                                    })
-                                }
-                                if key == Key::Up || key == Key::W {
-                                    action_queue.push(ActionEvent {
-                                        action: Action::Up,
-                                        value,
-                                    })
-                                }
-                                if key == Key::Down || key == Key::S {
-                                    action_queue.push(ActionEvent {
-                                        action: Action::Down,
-                                        value,
-                                    })
-                                }
-                                if key == Key::Space {
-                                    action_queue.push(ActionEvent {
-                                        action: Action::Damage,
-                                        value,
-                                    })
-                                }
+                            let action = match key {
+                                Key::Left | Key::A => Some(Action::Left),
+                                Key::Right | Key::D => Some(Action::Right),
+                                Key::Up | Key::W => Some(Action::Up),
+                                Key::Down | Key::S => Some(Action::Down),
+                                Key::Space => Some(Action::Damage),
+                                Key::Escape => break 'main,
+                                _ => None,
+                            };
+
+                            let value = match pressed {
+                                PressedState::Released => 0.0,
+                                PressedState::Pressed => 1.0,
+                            };
+
+                            if let Some(action) = action {
+                                action_queue.push(ActionEvent { action, value })
                             }
                         }
                         Quit => {