special case gamepad
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
use bevy::{
|
use bevy::{
|
||||||
input::{
|
input::{
|
||||||
ButtonState,
|
ButtonState,
|
||||||
|
gamepad::{GamepadConnection, GamepadEvent},
|
||||||
mouse::{MouseButtonInput, MouseMotion},
|
mouse::{MouseButtonInput, MouseMotion},
|
||||||
},
|
},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
@@ -27,6 +28,7 @@ pub fn plugin(app: &mut App) {
|
|||||||
app.add_systems(PreUpdate, clear_mouse_delta);
|
app.add_systems(PreUpdate, clear_mouse_delta);
|
||||||
app.add_systems(Update, (gamepad_controls, keyboard_controls, mouse_rotate));
|
app.add_systems(Update, (gamepad_controls, keyboard_controls, mouse_rotate));
|
||||||
app.add_systems(Update, mouse_click.run_if(on_event::<MouseButtonInput>));
|
app.add_systems(Update, mouse_click.run_if(on_event::<MouseButtonInput>));
|
||||||
|
app.add_systems(Update, gamepad_connections.run_if(on_event::<GamepadEvent>));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gamepad_controls(
|
fn gamepad_controls(
|
||||||
@@ -49,9 +51,9 @@ fn gamepad_controls(
|
|||||||
|
|
||||||
const EPSILON: f32 = 0.015;
|
const EPSILON: f32 = 0.015;
|
||||||
|
|
||||||
let state = ControlState {
|
//8BitDo Ultimate wireless Controller for PC
|
||||||
move_dir: gamepad.left_stick(),
|
let look_dir = if gamepad.vendor_id() == Some(11720) && gamepad.product_id() == Some(12306) {
|
||||||
look_dir: Vec2::new(
|
Vec2::new(
|
||||||
if rotate < 0.5 - EPSILON {
|
if rotate < 0.5 - EPSILON {
|
||||||
40. * (rotate - 0.5)
|
40. * (rotate - 0.5)
|
||||||
} else if rotate > 0.5 + EPSILON {
|
} else if rotate > 0.5 + EPSILON {
|
||||||
@@ -60,7 +62,14 @@ fn gamepad_controls(
|
|||||||
0.
|
0.
|
||||||
},
|
},
|
||||||
0.,
|
0.,
|
||||||
),
|
)
|
||||||
|
} else {
|
||||||
|
gamepad.right_stick() * 40.
|
||||||
|
};
|
||||||
|
|
||||||
|
let state = ControlState {
|
||||||
|
move_dir: gamepad.left_stick(),
|
||||||
|
look_dir,
|
||||||
jump: gamepad.pressed(GamepadButton::South),
|
jump: gamepad.pressed(GamepadButton::South),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -133,3 +142,27 @@ fn mouse_click(mut events: EventReader<MouseButtonInput>, mut commands: Commands
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn gamepad_connections(mut evr_gamepad: EventReader<GamepadEvent>) {
|
||||||
|
for ev in evr_gamepad.read() {
|
||||||
|
// we only care about connection events
|
||||||
|
let GamepadEvent::Connection(ev_conn) = ev else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
match &ev_conn.connection {
|
||||||
|
GamepadConnection::Connected {
|
||||||
|
name,
|
||||||
|
vendor_id,
|
||||||
|
product_id,
|
||||||
|
} => {
|
||||||
|
info!(
|
||||||
|
"New gamepad connected: {:?}, name: {name}, vendor: {vendor_id:?}, product: {product_id:?}",
|
||||||
|
ev_conn.gamepad
|
||||||
|
);
|
||||||
|
}
|
||||||
|
GamepadConnection::Disconnected => {
|
||||||
|
info!("Lost connection with gamepad: {:?}", ev_conn.gamepad);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user