Preparing inputs

This commit is contained in:
Thomas Forgione 2022-08-02 14:01:16 +02:00
parent 6a1cc8876f
commit bc48d72c90
5 changed files with 91 additions and 51 deletions

View File

@ -4,7 +4,7 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
use crate::engine::bbox::Bbox;
use crate::engine::controls::Controls;
use crate::engine::event::Action;
use crate::engine::input::{Button, Event};
use crate::engine::math::{clamp, duration_as_f64, duration_as_frame};
use crate::engine::physics;
use crate::engine::scene::Updatable;
@ -74,6 +74,9 @@ pub struct Character {
/// Whether the character is walking or not.
walking: bool,
/// Whether the user has released the jump button.
can_jump: bool,
}
impl Character {
@ -88,6 +91,7 @@ impl Character {
max_jump: 1,
animation_timer: UNIX_EPOCH,
walking: false,
can_jump: true,
}
}
@ -192,11 +196,16 @@ impl Updatable for Character {
self.position += self.speed * duration;
}
/// An action was asked to the character.
fn manage_action(&mut self, action: Action) {
match action {
Action::Button1 => {
/// An event was asked to the character.
fn manage_event(&mut self, event: Event) {
match event {
Event::ButtonPressed(Button::Button1) => {
self.jump();
self.can_jump = false;
}
Event::ButtonReleased(Button::Button1) => {
self.can_jump = true;
}
_ => (),

View File

@ -1,38 +0,0 @@
//! This module helps us deal with controls and events.
use std::cell::RefCell;
use std::collections::{HashMap, VecDeque};
use std::rc::Rc;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use crate::Result;
/// The different actions that a user can do.
#[derive(Copy, Clone)]
pub enum Action {
/// The user asks to go to the left.
GoLeft,
/// The user asks to go to the right.
GoRight,
/// The user asks to go up.
GoUp,
/// The user asks to go down.
GoDown,
/// The user asks to do the main action or validate.
Button1,
/// The user asks to do the secondary action.
Button2,
/// The user asks to do the tertiary action.
Button3,
/// The user asks to do the 4th action or cancel.
Button4,
}

63
src/engine/input.rs Normal file
View File

@ -0,0 +1,63 @@
//! This module helps us deal with controls and events.
use std::cell::RefCell;
use std::collections::{HashMap, VecDeque};
use std::rc::Rc;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use crate::Result;
/// The different actions that a user can do.
#[derive(Copy, Clone)]
pub enum Event {
/// A button has been pressed.
ButtonPressed(Button),
/// A button has been released.
ButtonReleased(Button),
}
/// The different actions that a user can do.
#[derive(Copy, Clone)]
pub enum Button {
/// The user asks to go to the left.
Left,
/// The user asks to go to the right.
Right,
/// The user asks to go up.
Up,
/// The user asks to go down.
Down,
/// The main action or validatation button.
Button1,
/// The user asks to do the secondary action.
Button2,
/// The user asks to do the tertiary action.
Button3,
/// The user asks to do the 4th action or cancel.
Button4,
}
/// This structure holds what button are pressed or released, and stores the events.
pub struct InputManager {
/// The events that have occur.
events: VecDeque<Event>,
}
impl InputManager {
/// Creates a new input manager.
pub fn new() -> InputManager {
InputManager {
events: VecDeque::new(),
}
}
}

View File

@ -3,7 +3,7 @@
pub mod bbox;
pub mod character;
pub mod controls;
pub mod event;
pub mod input;
pub mod map;
pub mod math;
pub mod physics;
@ -21,6 +21,7 @@ use wasm_bindgen::JsCast;
use crate::engine::bbox::Bbox;
use crate::engine::character::Character;
use crate::engine::controls::Controls;
use crate::engine::input::InputManager;
use crate::engine::map::Map;
use crate::engine::math::now;
use crate::engine::scene::{Scene, State};
@ -95,7 +96,8 @@ impl Engine {
inner.add_gamepad(&event);
});
(*window).add_event_listener_with_callback("gamepadconnected", cb.as_ref().unchecked_ref());
(*window)
.add_event_listener_with_callback("gamepadconnected", cb.as_ref().unchecked_ref())?;
cb.forget();
@ -240,6 +242,9 @@ pub struct InnerEngine {
/// The texture manager.
pub textures: TextureManager,
/// The input manager.
pub inputs: InputManager,
}
impl InnerEngine {
@ -253,6 +258,7 @@ impl InnerEngine {
scene,
after_loop: now(&performance),
textures: TextureManager::new()?,
inputs: InputManager::new(),
})
}

View File

@ -4,7 +4,7 @@ use std::time::{Duration, SystemTime};
use crate::engine::bbox::Bbox;
use crate::engine::character::Character;
use crate::engine::event::Action;
use crate::engine::input::Event;
use crate::engine::map::Map;
use crate::engine::texture::SPRITE_SIZE;
@ -132,10 +132,10 @@ impl Scene {
state
}
/// Transfers an action to the elements contained in the scene that should receive actions.
pub fn manage_action(&mut self, action: Action) {
/// Transfers an event to the elements contained in the scene that should receive events.
pub fn manage_event(&mut self, event: Event) {
for c in &mut self.characters {
c.manage_action(action);
c.manage_event(event);
}
}
@ -155,6 +155,6 @@ pub trait Updatable {
/// Updates the thing depending on the duration since last frame.
fn update(&mut self, now: SystemTime, duration: Duration);
/// Called when an action arrives.
fn manage_action(&mut self, action: Action);
/// Called when an event arrives.
fn manage_event(&mut self, event: Event);
}