Inputs start to be nice

This commit is contained in:
Thomas Forgione 2022-08-02 15:10:01 +02:00
parent 679d6ebe4f
commit db8fa3ebc8
3 changed files with 22 additions and 17 deletions

View File

@ -199,12 +199,12 @@ impl Updatable for Character {
/// An event was asked to the character.
fn manage_event(&mut self, event: Event) {
match event {
Event::ButtonPressed(Button::Button0) => {
Event::ButtonPressed(Button::Button1) => {
self.jump();
self.can_jump = false;
}
Event::ButtonReleased(Button::Button0) => {
Event::ButtonReleased(Button::Button1) => {
self.can_jump = true;
}

View File

@ -7,10 +7,10 @@ use std::rc::Rc;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use crate::Result;
use crate::{log, Result};
/// The different actions that a user can do.
#[derive(Copy, Clone)]
#[derive(Debug, Copy, Clone)]
pub enum Event {
/// A button has been pressed.
ButtonPressed(Button),
@ -20,7 +20,7 @@ pub enum Event {
}
/// The different actions that a user can do.
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Button {
/// The user asks to go to the left.
Left,
@ -67,13 +67,14 @@ impl InputManager {
/// Adds a gamepad to the input manager.
pub fn add_gamepad(&mut self, gamepad: web_sys::Gamepad) {
log!("Gamepad added {}", gamepad.id());
self.gamepads.push(Gamepad::new(gamepad));
}
/// Updates the gamepad states.
pub fn update(&mut self) {
for gamepad in &mut self.gamepads {
gamepad.update();
gamepad.update(&mut self.events);
}
}
}
@ -98,9 +99,7 @@ impl Gamepad {
}
/// Updates the state of the gamepad and adds the corresponding events to the deque.
pub fn update(&mut self) -> VecDeque<Event> {
let mut events = VecDeque::new();
pub fn update(&mut self, events: &mut VecDeque<Event>) {
const BUTTONS: [Button; 4] = [
Button::Button0,
Button::Button1,
@ -125,8 +124,6 @@ impl Gamepad {
events.push_back(Event::ButtonPressed(button));
}
}
events
}
/// Checks if a button is pressed.
@ -143,9 +140,9 @@ impl Gamepad {
Button::Up => false,
Button::Down => false,
Button::Button0 => Into::<web_sys::GamepadButton>::into(buttons.get(0)).pressed(),
Button::Button1 => Into::<web_sys::GamepadButton>::into(buttons.get(0)).pressed(),
Button::Button2 => Into::<web_sys::GamepadButton>::into(buttons.get(0)).pressed(),
Button::Button3 => Into::<web_sys::GamepadButton>::into(buttons.get(0)).pressed(),
Button::Button1 => Into::<web_sys::GamepadButton>::into(buttons.get(1)).pressed(),
Button::Button2 => Into::<web_sys::GamepadButton>::into(buttons.get(2)).pressed(),
Button::Button3 => Into::<web_sys::GamepadButton>::into(buttons.get(3)).pressed(),
}
}
}
@ -167,6 +164,8 @@ impl Inputs {
window.add_event_listener_with_callback("gamepadconnected", cb.as_ref().unchecked_ref())?;
cb.forget();
Ok(inputs)
}
@ -177,4 +176,10 @@ impl Inputs {
let mut inner = self.0.borrow_mut();
inner.update();
}
/// Returns the next event.
pub fn next(&mut self) -> Option<Event> {
let mut inner = self.0.borrow_mut();
inner.events.pop_front()
}
}

View File

@ -180,9 +180,9 @@ impl Engine {
if self.document.has_focus()? {
// Manage events
// while let Some(event) = inner.keyboard.pop() {
// inner.scene.manage_action(&action);
// }
while let Some(event) = self.inputs.next() {
self.scene.manage_event(event);
}
// let keyboard = inner.keyboard.clone();
if self.scene.update(now, duration) == State::Finished {