Preparing inputs
This commit is contained in:
parent
6a1cc8876f
commit
bc48d72c90
@ -4,7 +4,7 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
|||||||
|
|
||||||
use crate::engine::bbox::Bbox;
|
use crate::engine::bbox::Bbox;
|
||||||
use crate::engine::controls::Controls;
|
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::math::{clamp, duration_as_f64, duration_as_frame};
|
||||||
use crate::engine::physics;
|
use crate::engine::physics;
|
||||||
use crate::engine::scene::Updatable;
|
use crate::engine::scene::Updatable;
|
||||||
@ -74,6 +74,9 @@ pub struct Character {
|
|||||||
|
|
||||||
/// Whether the character is walking or not.
|
/// Whether the character is walking or not.
|
||||||
walking: bool,
|
walking: bool,
|
||||||
|
|
||||||
|
/// Whether the user has released the jump button.
|
||||||
|
can_jump: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Character {
|
impl Character {
|
||||||
@ -88,6 +91,7 @@ impl Character {
|
|||||||
max_jump: 1,
|
max_jump: 1,
|
||||||
animation_timer: UNIX_EPOCH,
|
animation_timer: UNIX_EPOCH,
|
||||||
walking: false,
|
walking: false,
|
||||||
|
can_jump: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,11 +196,16 @@ impl Updatable for Character {
|
|||||||
self.position += self.speed * duration;
|
self.position += self.speed * duration;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An action was asked to the character.
|
/// An event was asked to the character.
|
||||||
fn manage_action(&mut self, action: Action) {
|
fn manage_event(&mut self, event: Event) {
|
||||||
match action {
|
match event {
|
||||||
Action::Button1 => {
|
Event::ButtonPressed(Button::Button1) => {
|
||||||
self.jump();
|
self.jump();
|
||||||
|
self.can_jump = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Event::ButtonReleased(Button::Button1) => {
|
||||||
|
self.can_jump = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => (),
|
_ => (),
|
||||||
|
@ -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
63
src/engine/input.rs
Normal 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(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,7 @@
|
|||||||
pub mod bbox;
|
pub mod bbox;
|
||||||
pub mod character;
|
pub mod character;
|
||||||
pub mod controls;
|
pub mod controls;
|
||||||
pub mod event;
|
pub mod input;
|
||||||
pub mod map;
|
pub mod map;
|
||||||
pub mod math;
|
pub mod math;
|
||||||
pub mod physics;
|
pub mod physics;
|
||||||
@ -21,6 +21,7 @@ use wasm_bindgen::JsCast;
|
|||||||
use crate::engine::bbox::Bbox;
|
use crate::engine::bbox::Bbox;
|
||||||
use crate::engine::character::Character;
|
use crate::engine::character::Character;
|
||||||
use crate::engine::controls::Controls;
|
use crate::engine::controls::Controls;
|
||||||
|
use crate::engine::input::InputManager;
|
||||||
use crate::engine::map::Map;
|
use crate::engine::map::Map;
|
||||||
use crate::engine::math::now;
|
use crate::engine::math::now;
|
||||||
use crate::engine::scene::{Scene, State};
|
use crate::engine::scene::{Scene, State};
|
||||||
@ -95,7 +96,8 @@ impl Engine {
|
|||||||
inner.add_gamepad(&event);
|
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();
|
cb.forget();
|
||||||
|
|
||||||
@ -240,6 +242,9 @@ pub struct InnerEngine {
|
|||||||
|
|
||||||
/// The texture manager.
|
/// The texture manager.
|
||||||
pub textures: TextureManager,
|
pub textures: TextureManager,
|
||||||
|
|
||||||
|
/// The input manager.
|
||||||
|
pub inputs: InputManager,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InnerEngine {
|
impl InnerEngine {
|
||||||
@ -253,6 +258,7 @@ impl InnerEngine {
|
|||||||
scene,
|
scene,
|
||||||
after_loop: now(&performance),
|
after_loop: now(&performance),
|
||||||
textures: TextureManager::new()?,
|
textures: TextureManager::new()?,
|
||||||
|
inputs: InputManager::new(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ use std::time::{Duration, SystemTime};
|
|||||||
|
|
||||||
use crate::engine::bbox::Bbox;
|
use crate::engine::bbox::Bbox;
|
||||||
use crate::engine::character::Character;
|
use crate::engine::character::Character;
|
||||||
use crate::engine::event::Action;
|
use crate::engine::input::Event;
|
||||||
use crate::engine::map::Map;
|
use crate::engine::map::Map;
|
||||||
use crate::engine::texture::SPRITE_SIZE;
|
use crate::engine::texture::SPRITE_SIZE;
|
||||||
|
|
||||||
@ -132,10 +132,10 @@ impl Scene {
|
|||||||
state
|
state
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Transfers an action to the elements contained in the scene that should receive actions.
|
/// Transfers an event to the elements contained in the scene that should receive events.
|
||||||
pub fn manage_action(&mut self, action: Action) {
|
pub fn manage_event(&mut self, event: Event) {
|
||||||
for c in &mut self.characters {
|
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.
|
/// Updates the thing depending on the duration since last frame.
|
||||||
fn update(&mut self, now: SystemTime, duration: Duration);
|
fn update(&mut self, now: SystemTime, duration: Duration);
|
||||||
|
|
||||||
/// Called when an action arrives.
|
/// Called when an event arrives.
|
||||||
fn manage_action(&mut self, action: Action);
|
fn manage_event(&mut self, event: Event);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user