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]);
match &binding.input {
+13 -15
View File
@@ -2,10 +2,7 @@ use std::error;
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::system::Resource;
use bevy_input::{
keyboard::KeyCode,
mouse::MouseButton,
};
use bevy_input::{keyboard::KeyCode, mouse::MouseButton};
#[cfg(feature = "inspector-egui")]
use bevy_inspector_egui::prelude::*;
#[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
/// 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>> {
// SAFETY: action is always valid
// because we iterate over all actions in `default` method
if let InputValue::Boolean(current) = *self.current.get(&action).unwrap() {
if let InputValue::Boolean(previous) = *self.previous.get(&action).unwrap() {
return Ok(current && !previous);
}
return Err("Previous input is not boolean".into());
}
return Ok(
self.current.get(&action).unwrap().to_boolean()
&& !self.previous.get(&action).unwrap().to_boolean(),
);
Err("This input is not boolean".into())
}
@@ -303,8 +296,7 @@ impl ButtonCombination {
pub fn len(&self) -> usize {
use crate::resource::ButtonCombination::*;
match self {
Single(_) => 1
// TODO: Chord
Single(_) => 1, // TODO: Chord
}
}
}
@@ -428,7 +420,13 @@ impl InputValue {
pub fn to_float(&self) -> f32 {
match self {
InputValue::Boolean(value) => if *value { 1. } else { 0. },
InputValue::Boolean(value) => {
if *value {
1.
} else {
0.
}
}
InputValue::Float(value) => *value,
InputValue::Empty => 0.,
}