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