Work on keyboard controls

This commit is contained in:
Thomas Forgione 2022-08-02 15:45:38 +02:00
parent 10934b6209
commit d591fbe693
3 changed files with 60 additions and 3 deletions

View File

@ -1,9 +1,12 @@
//! This module helps to deal with controls.
use crate::engine::input::{Action, Event};
use crate::engine::input::{Action, Button, Event, Key};
/// The different types of controls.
pub enum Controls {
/// Default keyboard controls.
Keyboard,
/// A gamepad controls the scene.
Gamepad(u32),
}
@ -12,6 +15,11 @@ impl Controls {
/// Converts an event into an action if it corresponds to an action.
pub fn convert(&self, event: Event) -> Option<Action> {
match self {
Controls::Keyboard => match event {
Event::KeyPressed(Key::Space) => Some(Action::ButtonPressed(Button::Button1)),
Event::KeyReleased(Key::Space) => Some(Action::ButtonReleased(Button::Button1)),
_ => None,
},
Controls::Gamepad(ig) => match event {
Event::ButtonPressed(ie, button) if *ig == ie => {
Some(Action::ButtonPressed(button))

View File

@ -12,6 +12,12 @@ use crate::{log, Result};
/// The different events that can be triggered.
#[derive(Debug, Copy, Clone)]
pub enum Event {
/// A key has been pressed.
KeyPressed(Key),
/// A key has been released.
KeyReleased(Key),
/// A gamepad has been connected.
GamepadConnected(u32),
@ -22,6 +28,13 @@ pub enum Event {
ButtonReleased(u32, Button),
}
/// The different keyboard keys we support.
#[derive(Debug, Copy, Clone)]
pub enum Key {
/// The space key.
Space,
}
/// The different actions a user can do.
#[derive(Debug, Copy, Clone)]
pub enum Action {
@ -78,6 +91,28 @@ impl InputManager {
}
}
/// Parses a keyboard event to the key.
pub fn key(event: web_sys::KeyboardEvent) -> Option<Key> {
match event.code().as_str() {
"Space" => Some(Key::Space),
_ => None,
}
}
/// Adds a key pressed event to the list of events.
pub fn key_pressed_event(&mut self, event: web_sys::KeyboardEvent) {
if let Some(key) = InputManager::key(event) {
self.events.push_back(Event::KeyPressed(key));
}
}
/// Adds a key released event to the list of events.
pub fn key_released_event(&mut self, event: web_sys::KeyboardEvent) {
if let Some(key) = InputManager::key(event) {
self.events.push_back(Event::KeyReleased(key));
}
}
/// Adds a gamepad to the input manager.
pub fn add_gamepad(&mut self, gamepad: web_sys::Gamepad) {
log!("Gamepad added {}", gamepad.id());
@ -176,9 +211,23 @@ impl Inputs {
let mut inner = clone.0.borrow_mut();
inner.add_gamepad(event.gamepad().unwrap());
});
window.add_event_listener_with_callback("gamepadconnected", cb.as_ref().unchecked_ref())?;
cb.forget();
let clone = inputs.clone();
let cb = Closure::<dyn FnMut(_)>::new(move |event: web_sys::KeyboardEvent| {
let mut inner = clone.0.borrow_mut();
inner.key_pressed_event(event);
});
window.add_event_listener_with_callback("keydown", cb.as_ref().unchecked_ref())?;
cb.forget();
let clone = inputs.clone();
let cb = Closure::<dyn FnMut(_)>::new(move |event: web_sys::KeyboardEvent| {
let mut inner = clone.0.borrow_mut();
inner.key_released_event(event);
});
window.add_event_listener_with_callback("keyup", cb.as_ref().unchecked_ref())?;
cb.forget();
Ok(inputs)

View File

@ -162,7 +162,7 @@ impl Engine {
let map = Map::from_str(include_str!("../../static/levels/level1.lvl")).unwrap();
let mut scene = Scene::from_map(map);
let character = Character::new();
let character = Character::with_controls(Controls::Keyboard);
scene.add(character);
Ok(Engine {