Fix tab looking/general input consistency improvement (#71)
* fix tab looking/general input consistency improvement * fix input drops from consecutive FixedUpdates
This commit is contained in:
@@ -1,16 +1,19 @@
|
||||
use super::Controls;
|
||||
use crate::{GameState, control::CharacterInputEnabled};
|
||||
use bevy::input::{
|
||||
use bevy::{
|
||||
input::{
|
||||
ButtonState,
|
||||
gamepad::{GamepadConnection, GamepadEvent},
|
||||
mouse::{MouseButtonInput, MouseMotion},
|
||||
},
|
||||
prelude::*,
|
||||
};
|
||||
use lightyear::{
|
||||
input::client::InputSet,
|
||||
prelude::input::native::{ActionState, InputMarker},
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
use lightyear::input::client::InputSet;
|
||||
use lightyear::prelude::input::native::{ActionState, InputMarker};
|
||||
use shared::control::{ControlState, ControllerSet};
|
||||
use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
use std::{collections::HashMap, hash::Hash};
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.init_resource::<Controls>();
|
||||
@@ -29,8 +32,8 @@ pub fn plugin(app: &mut App) {
|
||||
mouse_click,
|
||||
gamepad_connections.run_if(on_event::<GamepadEvent>),
|
||||
combine_controls,
|
||||
clear_keyboard_state,
|
||||
clear_gamepad_state,
|
||||
clear_keyboard_just,
|
||||
clear_gamepad_just,
|
||||
)
|
||||
.chain()
|
||||
.in_set(ControllerSet::CollectInputs)
|
||||
@@ -86,19 +89,12 @@ impl<Button> Default for InputStateCache<Button> {
|
||||
}
|
||||
|
||||
impl<Button: Hash + Eq> InputStateCache<Button> {
|
||||
fn clear(&mut self) {
|
||||
fn clear_just(&mut self) {
|
||||
for state in self.map.values_mut() {
|
||||
*state = InputState::default();
|
||||
state.just_pressed = false;
|
||||
}
|
||||
}
|
||||
|
||||
fn pressed(&self, button: Button) -> bool {
|
||||
self.map
|
||||
.get(&button)
|
||||
.map(|state| state.pressed)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn just_pressed(&self, button: Button) -> bool {
|
||||
self.map
|
||||
.get(&button)
|
||||
@@ -109,7 +105,6 @@ impl<Button: Hash + Eq> InputStateCache<Button> {
|
||||
|
||||
#[derive(Default)]
|
||||
struct InputState {
|
||||
pressed: bool,
|
||||
just_pressed: bool,
|
||||
}
|
||||
|
||||
@@ -118,7 +113,6 @@ fn cache_keyboard_state(
|
||||
keyboard: Res<ButtonInput<KeyCode>>,
|
||||
) {
|
||||
let mut cache_key = |key| {
|
||||
cache.map.entry(key).or_default().pressed |= keyboard.pressed(key);
|
||||
cache.map.entry(key).or_default().just_pressed |= keyboard.just_pressed(key);
|
||||
};
|
||||
cache_key(KeyCode::Space);
|
||||
@@ -131,14 +125,13 @@ fn cache_keyboard_state(
|
||||
cache_key(KeyCode::KeyE);
|
||||
}
|
||||
|
||||
fn clear_keyboard_state(mut cache: ResMut<InputStateCache<KeyCode>>) {
|
||||
cache.clear();
|
||||
fn clear_keyboard_just(mut cache: ResMut<InputStateCache<KeyCode>>) {
|
||||
cache.clear_just();
|
||||
}
|
||||
|
||||
fn cache_gamepad_state(mut gamepads: Query<(&Gamepad, &mut InputStateCache<GamepadButton>)>) {
|
||||
for (gamepad, mut cache) in gamepads.iter_mut() {
|
||||
let mut cache_button = |button| {
|
||||
cache.map.entry(button).or_default().pressed |= gamepad.pressed(button);
|
||||
cache.map.entry(button).or_default().just_pressed |= gamepad.just_pressed(button);
|
||||
};
|
||||
|
||||
@@ -161,9 +154,9 @@ fn cache_gamepad_state(mut gamepads: Query<(&Gamepad, &mut InputStateCache<Gamep
|
||||
}
|
||||
}
|
||||
|
||||
fn clear_gamepad_state(mut caches: Query<&mut InputStateCache<GamepadButton>>) {
|
||||
fn clear_gamepad_just(mut caches: Query<&mut InputStateCache<GamepadButton>>) {
|
||||
for mut cache in caches.iter_mut() {
|
||||
cache.clear();
|
||||
cache.clear_just();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,9 +227,9 @@ fn gamepad_controls(
|
||||
let state = ControlState {
|
||||
move_dir: deadzone_square(gamepad.left_stick(), deadzone_left_stick),
|
||||
look_dir,
|
||||
jump: cache.pressed(GamepadButton::South),
|
||||
view_mode: cache.pressed(GamepadButton::LeftTrigger2),
|
||||
trigger: cache.pressed(GamepadButton::RightTrigger2),
|
||||
jump: gamepad.pressed(GamepadButton::South),
|
||||
view_mode: gamepad.pressed(GamepadButton::LeftTrigger2),
|
||||
trigger: gamepad.pressed(GamepadButton::RightTrigger2),
|
||||
just_triggered: cache.just_pressed(GamepadButton::RightTrigger2),
|
||||
select_left: cache.just_pressed(GamepadButton::LeftTrigger),
|
||||
select_right: cache.just_pressed(GamepadButton::RightTrigger),
|
||||
@@ -287,8 +280,8 @@ fn keyboard_controls(
|
||||
let direction = Vec2::new(horizontal as f32, vertical as f32).clamp_length_max(1.0);
|
||||
|
||||
controls.keyboard_state.move_dir = direction;
|
||||
controls.keyboard_state.jump = cache.pressed(KeyCode::Space);
|
||||
controls.keyboard_state.view_mode = cache.pressed(KeyCode::Tab);
|
||||
controls.keyboard_state.jump = keyboard.pressed(KeyCode::Space);
|
||||
controls.keyboard_state.view_mode = keyboard.pressed(KeyCode::Tab);
|
||||
controls.keyboard_state.backpack_toggle = cache.just_pressed(KeyCode::KeyB);
|
||||
controls.keyboard_state.backpack_swap = cache.just_pressed(KeyCode::Enter);
|
||||
controls.keyboard_state.backpack_left = cache.just_pressed(KeyCode::Comma);
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
use bevy::{ecs::entity::MapEntities, prelude::*};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
GameState,
|
||||
head::ActiveHead,
|
||||
heads_database::{HeadControls, HeadsDatabase},
|
||||
player::Player,
|
||||
};
|
||||
use bevy::{ecs::entity::MapEntities, prelude::*};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub mod controller_common;
|
||||
pub mod controller_flying;
|
||||
|
||||
Reference in New Issue
Block a user