51 lines
1.6 KiB
Rust
51 lines
1.6 KiB
Rust
use bevy::{
|
|
input::gamepad::{GamepadAxisChangedEvent, GamepadButtonChangedEvent, GamepadEvent},
|
|
prelude::*,
|
|
};
|
|
|
|
#[derive(Default, Debug, Resource)]
|
|
pub(crate) struct InputState {
|
|
pub yaw: f32,
|
|
pub throttle: f32,
|
|
pub brake: bool,
|
|
pub pitch: f32,
|
|
}
|
|
|
|
fn update_input(mut events: EventReader<GamepadEvent>, mut istate: ResMut<InputState>) {
|
|
for pad_event in events.read() {
|
|
match pad_event {
|
|
GamepadEvent::Button(button_event) => {
|
|
let GamepadButtonChangedEvent { button, value, .. } = button_event;
|
|
match button {
|
|
GamepadButton::RightTrigger2 => istate.throttle = *value,
|
|
GamepadButton::LeftTrigger2 => istate.throttle = -value,
|
|
GamepadButton::East => {
|
|
istate.brake = value > &0.5;
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
GamepadEvent::Axis(axis_event) => {
|
|
let GamepadAxisChangedEvent { axis, value, .. } = axis_event;
|
|
match axis {
|
|
GamepadAxis::LeftStickX => {
|
|
istate.yaw = *value;
|
|
}
|
|
GamepadAxis::RightStickY => {
|
|
istate.pitch = *value;
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
GamepadEvent::Connection(_) => {}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct CyberInputPlugin;
|
|
impl Plugin for CyberInputPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.init_resource::<InputState>()
|
|
.add_systems(Update, update_input);
|
|
}
|
|
}
|