fix: get_just_pressed

This commit is contained in:
2024-04-06 02:06:45 +02:00
parent b332347f85
commit 9cd99b898b
2 changed files with 18 additions and 20 deletions
+1 -1
View File
@@ -153,7 +153,7 @@ impl<A: Action, Ic: InputsContainer<A>, Gs: GameState> ControlsPlugin<A, Ic, Gs>
} }
} }
log::info!("{:#?}", collected_inputs); //log::info!("{:#?}", collected_inputs);
//log::info!("{:#?}", action_binding_pairs[0]); //log::info!("{:#?}", action_binding_pairs[0]);
match &binding.input { match &binding.input {
+17 -19
View File
@@ -2,10 +2,7 @@ use std::error;
use bevy_derive::{Deref, DerefMut}; use bevy_derive::{Deref, DerefMut};
use bevy_ecs::system::Resource; use bevy_ecs::system::Resource;
use bevy_input::{ use bevy_input::{keyboard::KeyCode, mouse::MouseButton};
keyboard::KeyCode,
mouse::MouseButton,
};
#[cfg(feature = "inspector-egui")] #[cfg(feature = "inspector-egui")]
use bevy_inspector_egui::prelude::*; use bevy_inspector_egui::prelude::*;
#[cfg(all(feature = "reflect", feature = "serialize"))] #[cfg(all(feature = "reflect", feature = "serialize"))]
@@ -122,14 +119,10 @@ impl<A: Action> PlayerActions<A> {
/// Returns `true` if current `InputValue` has become `InputValue::Boolean(true)` in this frame /// Returns `true` if current `InputValue` has become `InputValue::Boolean(true)` in this frame
/// If input has not same type as `InputValue` on previous frame return `Error()` /// If input has not same type as `InputValue` on previous frame return `Error()`
pub fn get_just_pressed(&self, action: A) -> Result<bool, Box<dyn error::Error>> { pub fn get_just_pressed(&self, action: A) -> Result<bool, Box<dyn error::Error>> {
// SAFETY: action is always valid return Ok(
// because we iterate over all actions in `default` method self.current.get(&action).unwrap().to_boolean()
if let InputValue::Boolean(current) = *self.current.get(&action).unwrap() { && !self.previous.get(&action).unwrap().to_boolean(),
if let InputValue::Boolean(previous) = *self.previous.get(&action).unwrap() { );
return Ok(current && !previous);
}
return Err("Previous input is not boolean".into());
}
Err("This input is not boolean".into()) Err("This input is not boolean".into())
} }
@@ -300,13 +293,12 @@ common_traits_conditions! {
} }
impl ButtonCombination { impl ButtonCombination {
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
use crate::resource::ButtonCombination::*; use crate::resource::ButtonCombination::*;
match self { match self {
Single(_) => 1 Single(_) => 1, // TODO: Chord
// TODO: Chord
}
} }
}
} }
common_traits_conditions! { common_traits_conditions! {
@@ -428,7 +420,13 @@ impl InputValue {
pub fn to_float(&self) -> f32 { pub fn to_float(&self) -> f32 {
match self { match self {
InputValue::Boolean(value) => if *value { 1. } else { 0. }, InputValue::Boolean(value) => {
if *value {
1.
} else {
0.
}
}
InputValue::Float(value) => *value, InputValue::Float(value) => *value,
InputValue::Empty => 0., InputValue::Empty => 0.,
} }