|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
use std::collections::HashSet;
|
|
|
|
|
use std::collections::{HashMap, HashSet};
|
|
|
|
|
use bevy::{
|
|
|
|
|
input::gamepad::{Gamepad, GamepadAxisType, GamepadButtonType, GamepadEvent, GamepadEventType},
|
|
|
|
|
prelude::*,
|
|
|
|
@ -42,7 +42,46 @@ const GAMEPAD_KEYS: &[(GamepadButtonType, Key)] = &[
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
pub struct InputState(pub HashSet<(Source, Key)>);
|
|
|
|
|
pub struct InputState {
|
|
|
|
|
pub keys: HashSet<(Source, Key)>,
|
|
|
|
|
x: HashMap<Source, f32>,
|
|
|
|
|
y: HashMap<Source, f32>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl InputState {
|
|
|
|
|
pub fn direction_of(&self, source: &Source) -> Vec2 {
|
|
|
|
|
let x = *self.x.get(source)
|
|
|
|
|
.unwrap_or(&0.0) +
|
|
|
|
|
if self.keys.contains(&(source.clone(), Key::Left)) {
|
|
|
|
|
-1.0
|
|
|
|
|
} else {
|
|
|
|
|
0.0
|
|
|
|
|
} +
|
|
|
|
|
if self.keys.contains(&(source.clone(), Key::Right)) {
|
|
|
|
|
1.0
|
|
|
|
|
} else {
|
|
|
|
|
0.0
|
|
|
|
|
};
|
|
|
|
|
let y = *self.y.get(source)
|
|
|
|
|
.unwrap_or(&0.0) +
|
|
|
|
|
if self.keys.contains(&(source.clone(), Key::Down)) {
|
|
|
|
|
-1.0
|
|
|
|
|
} else {
|
|
|
|
|
0.0
|
|
|
|
|
} +
|
|
|
|
|
if self.keys.contains(&(source.clone(), Key::Up)) {
|
|
|
|
|
1.0
|
|
|
|
|
} else {
|
|
|
|
|
0.0
|
|
|
|
|
};
|
|
|
|
|
let result = Vec2::new(x, -y);
|
|
|
|
|
if result.length_squared() > 0.0 {
|
|
|
|
|
result.normalize()
|
|
|
|
|
} else {
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn setup(mut commands: Commands) {
|
|
|
|
|
commands.insert_resource(InputState::default());
|
|
|
|
@ -56,9 +95,9 @@ pub fn handle(
|
|
|
|
|
// handle keyboard
|
|
|
|
|
for (key_code, source, key) in KEYBOARD_KEYS {
|
|
|
|
|
if keyboard.pressed(*key_code) {
|
|
|
|
|
state.0.insert((source.clone(), *key));
|
|
|
|
|
state.keys.insert((source.clone(), *key));
|
|
|
|
|
} else {
|
|
|
|
|
state.0.remove(&(source.clone(), *key));
|
|
|
|
|
state.keys.remove(&(source.clone(), *key));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -70,37 +109,19 @@ pub fn handle(
|
|
|
|
|
button == button_
|
|
|
|
|
}) {
|
|
|
|
|
if *value > 0.01 {
|
|
|
|
|
state.0.insert((Source::Gamepad(*gamepad), *key));
|
|
|
|
|
state.keys.insert((Source::Gamepad(*gamepad), *key));
|
|
|
|
|
} else {
|
|
|
|
|
state.0.remove(&(Source::Gamepad(*gamepad), *key));
|
|
|
|
|
state.keys.remove(&(Source::Gamepad(*gamepad), *key));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
GamepadEvent(gamepad, GamepadEventType::AxisChanged(GamepadAxisType::DPadX, value)) |
|
|
|
|
|
GamepadEvent(gamepad, GamepadEventType::AxisChanged(GamepadAxisType::LeftStickX, value)) => {
|
|
|
|
|
if *value < -0.01 {
|
|
|
|
|
state.0.insert((Source::Gamepad(*gamepad), Key::Left));
|
|
|
|
|
state.0.remove(&(Source::Gamepad(*gamepad), Key::Right));
|
|
|
|
|
} else if *value > 0.01 {
|
|
|
|
|
state.0.insert((Source::Gamepad(*gamepad), Key::Right));
|
|
|
|
|
state.0.remove(&(Source::Gamepad(*gamepad), Key::Left));
|
|
|
|
|
} else {
|
|
|
|
|
state.0.remove(&(Source::Gamepad(*gamepad), Key::Left));
|
|
|
|
|
state.0.remove(&(Source::Gamepad(*gamepad), Key::Right));
|
|
|
|
|
}
|
|
|
|
|
state.x.insert(Source::Gamepad(*gamepad), *value);
|
|
|
|
|
}
|
|
|
|
|
GamepadEvent(gamepad, GamepadEventType::AxisChanged(GamepadAxisType::DPadY, value)) |
|
|
|
|
|
GamepadEvent(gamepad, GamepadEventType::AxisChanged(GamepadAxisType::LeftStickY, value)) => {
|
|
|
|
|
if *value < -0.01 {
|
|
|
|
|
state.0.insert((Source::Gamepad(*gamepad), Key::Down));
|
|
|
|
|
state.0.remove(&(Source::Gamepad(*gamepad), Key::Up));
|
|
|
|
|
} else if *value > 0.01 {
|
|
|
|
|
state.0.insert((Source::Gamepad(*gamepad), Key::Up));
|
|
|
|
|
state.0.remove(&(Source::Gamepad(*gamepad), Key::Down));
|
|
|
|
|
} else {
|
|
|
|
|
state.0.remove(&(Source::Gamepad(*gamepad), Key::Up));
|
|
|
|
|
state.0.remove(&(Source::Gamepad(*gamepad), Key::Down));
|
|
|
|
|
}
|
|
|
|
|
state.y.insert(Source::Gamepad(*gamepad), *value);
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|