fix: multiply bindings handling on single key
This commit is contained in:
+54
-52
@@ -5,7 +5,6 @@
|
||||
//! Yeah I know, now it's a bit ugly
|
||||
|
||||
use bevy::{
|
||||
diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin},
|
||||
ecs::schedule::States,
|
||||
prelude::*,
|
||||
};
|
||||
@@ -16,19 +15,19 @@ use bevy_controls::{
|
||||
plugin::ControlsPlugin,
|
||||
resource::{Binding, ButtonCombination, Controls, InputType, InputValue, Inputs, PlayerInputs},
|
||||
};
|
||||
use strum::IntoEnumIterator;
|
||||
use strum_macros::EnumIter;
|
||||
const FIRA_CODE_PATH: &str = "font/FiraCode/FiraCode-VariableFont_wght.ttf";
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, EnumIter, Clone, Copy, Debug)]
|
||||
enum MyAction {
|
||||
MoveForward,
|
||||
MoveBack,
|
||||
MoveLeft,
|
||||
MoveRight,
|
||||
Jump,
|
||||
Forward,
|
||||
Back,
|
||||
Left,
|
||||
Right,
|
||||
Up,
|
||||
}
|
||||
|
||||
// TODO: I realy need proc macro
|
||||
impl Action for MyAction {}
|
||||
|
||||
impl ActionInner for MyAction {}
|
||||
@@ -65,6 +64,9 @@ impl InputsContainerInner<MyAction> for MyInputsContainer {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource, Default, Deref, DerefMut)]
|
||||
struct JumpsCount(i32);
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins((
|
||||
@@ -74,6 +76,7 @@ fn main() {
|
||||
}),
|
||||
ControlsPlugin::<MyAction, MyInputsContainer, MyGameState>::default(),
|
||||
))
|
||||
.init_resource::<JumpsCount>()
|
||||
.add_systems(Startup, (add_bindings, setup))
|
||||
.add_systems(Update, text_update_system)
|
||||
.run();
|
||||
@@ -82,23 +85,43 @@ fn main() {
|
||||
// TODO: default bingings...
|
||||
fn add_bindings(mut controls: ResMut<Controls<MyAction, MyGameState>>) {
|
||||
controls.force_push(
|
||||
MyAction::MoveLeft,
|
||||
MyAction::Left,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::A))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::MoveBack,
|
||||
MyAction::Left,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::H))),
|
||||
);
|
||||
|
||||
controls.force_push(
|
||||
MyAction::Back,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::S))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::MoveForward,
|
||||
MyAction::Back,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::J))),
|
||||
);
|
||||
|
||||
controls.force_push(
|
||||
MyAction::Forward,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::W))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::MoveRight,
|
||||
MyAction::Forward,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::K))),
|
||||
);
|
||||
|
||||
controls.force_push(
|
||||
MyAction::Right,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::D))),
|
||||
);
|
||||
controls.force_push(
|
||||
MyAction::Jump,
|
||||
MyAction::Right,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::L))),
|
||||
);
|
||||
|
||||
controls.force_push(
|
||||
MyAction::Up,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::Space))),
|
||||
);
|
||||
}
|
||||
@@ -113,14 +136,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
|
||||
commands.spawn((
|
||||
TextBundle::from_sections([
|
||||
TextSection::new(
|
||||
"Inputs: A S W D Space",
|
||||
TextStyle {
|
||||
font: asset_server.load(FIRA_CODE_PATH),
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"\nAction: ",
|
||||
TextStyle {
|
||||
@@ -130,35 +145,35 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"MoveLeft ",
|
||||
"Left (H/A) ",
|
||||
TextStyle {
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"MoveBack ",
|
||||
"Back (J/S) ",
|
||||
TextStyle {
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"MoveForward ",
|
||||
"Forward (K/W) ",
|
||||
TextStyle {
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"MoveRight ",
|
||||
"Right (L/D) ",
|
||||
TextStyle {
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
},
|
||||
),
|
||||
TextSection::new(
|
||||
"Jump",
|
||||
"Up (Space)",
|
||||
TextStyle {
|
||||
font_size: TEXT_SIZE,
|
||||
..default()
|
||||
@@ -170,39 +185,40 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
}
|
||||
|
||||
// text sections indexes
|
||||
const MOVE_LEFT_INDEX: usize = 2;
|
||||
const MOVE_BACK_INDEX: usize = 3;
|
||||
const MOVE_FORWARD_INDEX: usize = 4;
|
||||
const MOVE_RIGHT_INDEX: usize = 5;
|
||||
const JUMP_INDEX: usize = 6;
|
||||
const MOVE_LEFT_INDEX: usize = 1;
|
||||
const MOVE_BACK_INDEX: usize = 2;
|
||||
const MOVE_FORWARD_INDEX: usize = 3;
|
||||
const MOVE_RIGHT_INDEX: usize = 4;
|
||||
const JUMP_INDEX: usize = 5;
|
||||
|
||||
fn text_update_system(
|
||||
mut query: Query<&mut Text, With<TextBlock>>,
|
||||
inputs_container: Res<MyInputsContainer>,
|
||||
mut jumps_count: ResMut<JumpsCount>,
|
||||
) {
|
||||
for text in query.iter_mut() {
|
||||
|
||||
let player_inputs = inputs_container.me().expect("This is bad");
|
||||
let mut text = update_text(
|
||||
text,
|
||||
player_inputs.get(MyAction::MoveLeft),
|
||||
player_inputs.get(MyAction::Left),
|
||||
MOVE_LEFT_INDEX);
|
||||
text = update_text(
|
||||
text,
|
||||
player_inputs.get(MyAction::MoveBack),
|
||||
player_inputs.get(MyAction::Back),
|
||||
MOVE_BACK_INDEX);
|
||||
text = update_text(
|
||||
text,
|
||||
player_inputs.get(MyAction::MoveForward),
|
||||
player_inputs.get(MyAction::Forward),
|
||||
MOVE_FORWARD_INDEX);
|
||||
text = update_text(
|
||||
text,
|
||||
player_inputs.get(MyAction::MoveRight),
|
||||
player_inputs.get(MyAction::Right),
|
||||
MOVE_RIGHT_INDEX);
|
||||
_ = update_text(
|
||||
text,
|
||||
InputValue::Boolean(player_inputs.get_just_pressed(MyAction::Jump).unwrap_or(false)),
|
||||
JUMP_INDEX);
|
||||
if player_inputs.get_just_pressed(MyAction::Up).unwrap_or(false) {
|
||||
**jumps_count += 1;
|
||||
text.sections[JUMP_INDEX].value = format!("Up (Space) - {}", **jumps_count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,17 +231,3 @@ fn update_text(mut text: Mut<Text>, input_value: InputValue, index: usize) -> Mu
|
||||
|
||||
text
|
||||
}
|
||||
|
||||
// fn text_update_system(
|
||||
// diagnostics: Res<DiagnosticsStore>,
|
||||
// mut query: Query<&mut Text, With<FpsText>>,
|
||||
// ) {
|
||||
// for mut text in &mut query {
|
||||
// if let Some(fps) = diagnostics.get(FrameTimeDiagnosticsPlugin::FPS) {
|
||||
// if let Some(value) = fps.smoothed() {
|
||||
// // Update the value of the second section
|
||||
// text.sections[1].value = format!("{value:.2}");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
+9
-4
@@ -46,11 +46,11 @@ impl<
|
||||
fn save_input(
|
||||
keyboard_input: Res<Input<KeyCode>>,
|
||||
mouse_input: Res<Input<MouseButton>>,
|
||||
mut lobby: ResMut<Ic>,
|
||||
mut inputs_container: ResMut<Ic>,
|
||||
controls: Res<Controls<A, Gs>>,
|
||||
game_state: Res<State<Gs>>,
|
||||
) {
|
||||
if let Some(inputs) = lobby.me_mut() {
|
||||
if let Some(inputs) = inputs_container.me_mut() {
|
||||
for (action, config) in controls.iter() {
|
||||
'bindings_loop: for binding in config.bindings.iter() {
|
||||
for condition in &binding.conditions {
|
||||
@@ -63,14 +63,18 @@ impl<
|
||||
}
|
||||
}
|
||||
|
||||
log::trace!("action {:?} in condition", action);
|
||||
log::trace!("action binding {:?} {:?} in condition", action, binding);
|
||||
|
||||
match &binding.input {
|
||||
ButtonCombination::Single(button) => match button {
|
||||
InputType::Keyboard(key) => {
|
||||
log::trace!("presed?: {:?}", keyboard_input.pressed(*key));
|
||||
log::trace!("active");
|
||||
inputs.forced_set(*action, keyboard_input.pressed(*key));
|
||||
if keyboard_input.pressed(*key) {
|
||||
inputs.forced_set(*action, true);
|
||||
break; // when on binding trigered no sence check another
|
||||
}
|
||||
inputs.forced_set(*action, false); // FIXME: this is happening on every pass so too frequently
|
||||
}
|
||||
InputType::Mouse(input) => {
|
||||
match input {
|
||||
@@ -124,6 +128,7 @@ impl<
|
||||
log::trace!("active");
|
||||
// TODO: should be [`Chord`](ButtonCombination::Chord) only [`Boolean`](InputValue::Boolean) type
|
||||
inputs.forced_set(*action, true);
|
||||
break; // when on binding trigered no sence check another
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user