From db8fa3ebc820ee1a0535d1632d3e2c12d0a0c669 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Tue, 2 Aug 2022 15:10:01 +0200 Subject: [PATCH] Inputs start to be nice --- src/engine/character.rs | 4 ++-- src/engine/input.rs | 29 +++++++++++++++++------------ src/engine/mod.rs | 6 +++--- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/engine/character.rs b/src/engine/character.rs index a14c9cc..e562d02 100644 --- a/src/engine/character.rs +++ b/src/engine/character.rs @@ -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; } diff --git a/src/engine/input.rs b/src/engine/input.rs index d4eeadf..c461429 100644 --- a/src/engine/input.rs +++ b/src/engine/input.rs @@ -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 { - let mut events = VecDeque::new(); - + pub fn update(&mut self, events: &mut VecDeque) { 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::::into(buttons.get(0)).pressed(), - Button::Button1 => Into::::into(buttons.get(0)).pressed(), - Button::Button2 => Into::::into(buttons.get(0)).pressed(), - Button::Button3 => Into::::into(buttons.get(0)).pressed(), + Button::Button1 => Into::::into(buttons.get(1)).pressed(), + Button::Button2 => Into::::into(buttons.get(2)).pressed(), + Button::Button3 => Into::::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 { + let mut inner = self.0.borrow_mut(); + inner.events.pop_front() + } } diff --git a/src/engine/mod.rs b/src/engine/mod.rs index 1b135e4..44151b9 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -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 {