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:
PROMETHIA-27
2025-10-21 01:01:33 -04:00
committed by GitHub
parent 5d2ac11ebd
commit f8540b01e0
2 changed files with 27 additions and 35 deletions

View File

@@ -1,16 +1,19 @@
use super::Controls; use super::Controls;
use crate::{GameState, control::CharacterInputEnabled}; use crate::{GameState, control::CharacterInputEnabled};
use bevy::input::{ use bevy::{
input::{
ButtonState, ButtonState,
gamepad::{GamepadConnection, GamepadEvent}, gamepad::{GamepadConnection, GamepadEvent},
mouse::{MouseButtonInput, MouseMotion}, 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 shared::control::{ControlState, ControllerSet};
use std::collections::HashMap; use std::{collections::HashMap, hash::Hash};
use std::hash::Hash;
pub fn plugin(app: &mut App) { pub fn plugin(app: &mut App) {
app.init_resource::<Controls>(); app.init_resource::<Controls>();
@@ -29,8 +32,8 @@ pub fn plugin(app: &mut App) {
mouse_click, mouse_click,
gamepad_connections.run_if(on_event::<GamepadEvent>), gamepad_connections.run_if(on_event::<GamepadEvent>),
combine_controls, combine_controls,
clear_keyboard_state, clear_keyboard_just,
clear_gamepad_state, clear_gamepad_just,
) )
.chain() .chain()
.in_set(ControllerSet::CollectInputs) .in_set(ControllerSet::CollectInputs)
@@ -86,19 +89,12 @@ impl<Button> Default for InputStateCache<Button> {
} }
impl<Button: Hash + Eq> InputStateCache<Button> { impl<Button: Hash + Eq> InputStateCache<Button> {
fn clear(&mut self) { fn clear_just(&mut self) {
for state in self.map.values_mut() { 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 { fn just_pressed(&self, button: Button) -> bool {
self.map self.map
.get(&button) .get(&button)
@@ -109,7 +105,6 @@ impl<Button: Hash + Eq> InputStateCache<Button> {
#[derive(Default)] #[derive(Default)]
struct InputState { struct InputState {
pressed: bool,
just_pressed: bool, just_pressed: bool,
} }
@@ -118,7 +113,6 @@ fn cache_keyboard_state(
keyboard: Res<ButtonInput<KeyCode>>, keyboard: Res<ButtonInput<KeyCode>>,
) { ) {
let mut cache_key = |key| { 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.map.entry(key).or_default().just_pressed |= keyboard.just_pressed(key);
}; };
cache_key(KeyCode::Space); cache_key(KeyCode::Space);
@@ -131,14 +125,13 @@ fn cache_keyboard_state(
cache_key(KeyCode::KeyE); cache_key(KeyCode::KeyE);
} }
fn clear_keyboard_state(mut cache: ResMut<InputStateCache<KeyCode>>) { fn clear_keyboard_just(mut cache: ResMut<InputStateCache<KeyCode>>) {
cache.clear(); cache.clear_just();
} }
fn cache_gamepad_state(mut gamepads: Query<(&Gamepad, &mut InputStateCache<GamepadButton>)>) { fn cache_gamepad_state(mut gamepads: Query<(&Gamepad, &mut InputStateCache<GamepadButton>)>) {
for (gamepad, mut cache) in gamepads.iter_mut() { for (gamepad, mut cache) in gamepads.iter_mut() {
let mut cache_button = |button| { 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); 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() { for mut cache in caches.iter_mut() {
cache.clear(); cache.clear_just();
} }
} }
@@ -234,9 +227,9 @@ fn gamepad_controls(
let state = ControlState { let state = ControlState {
move_dir: deadzone_square(gamepad.left_stick(), deadzone_left_stick), move_dir: deadzone_square(gamepad.left_stick(), deadzone_left_stick),
look_dir, look_dir,
jump: cache.pressed(GamepadButton::South), jump: gamepad.pressed(GamepadButton::South),
view_mode: cache.pressed(GamepadButton::LeftTrigger2), view_mode: gamepad.pressed(GamepadButton::LeftTrigger2),
trigger: cache.pressed(GamepadButton::RightTrigger2), trigger: gamepad.pressed(GamepadButton::RightTrigger2),
just_triggered: cache.just_pressed(GamepadButton::RightTrigger2), just_triggered: cache.just_pressed(GamepadButton::RightTrigger2),
select_left: cache.just_pressed(GamepadButton::LeftTrigger), select_left: cache.just_pressed(GamepadButton::LeftTrigger),
select_right: cache.just_pressed(GamepadButton::RightTrigger), 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); let direction = Vec2::new(horizontal as f32, vertical as f32).clamp_length_max(1.0);
controls.keyboard_state.move_dir = direction; controls.keyboard_state.move_dir = direction;
controls.keyboard_state.jump = cache.pressed(KeyCode::Space); controls.keyboard_state.jump = keyboard.pressed(KeyCode::Space);
controls.keyboard_state.view_mode = cache.pressed(KeyCode::Tab); controls.keyboard_state.view_mode = keyboard.pressed(KeyCode::Tab);
controls.keyboard_state.backpack_toggle = cache.just_pressed(KeyCode::KeyB); controls.keyboard_state.backpack_toggle = cache.just_pressed(KeyCode::KeyB);
controls.keyboard_state.backpack_swap = cache.just_pressed(KeyCode::Enter); controls.keyboard_state.backpack_swap = cache.just_pressed(KeyCode::Enter);
controls.keyboard_state.backpack_left = cache.just_pressed(KeyCode::Comma); controls.keyboard_state.backpack_left = cache.just_pressed(KeyCode::Comma);

View File

@@ -1,12 +1,11 @@
use bevy::{ecs::entity::MapEntities, prelude::*};
use serde::{Deserialize, Serialize};
use crate::{ use crate::{
GameState, GameState,
head::ActiveHead, head::ActiveHead,
heads_database::{HeadControls, HeadsDatabase}, heads_database::{HeadControls, HeadsDatabase},
player::Player, player::Player,
}; };
use bevy::{ecs::entity::MapEntities, prelude::*};
use serde::{Deserialize, Serialize};
pub mod controller_common; pub mod controller_common;
pub mod controller_flying; pub mod controller_flying;