Add gamepad

This commit is contained in:
Zhao Yunlong 2025-01-02 23:58:07 +08:00
parent 954e3d3b8b
commit 70ce8c3887

View File

@ -1,6 +1,7 @@
use bevy::{prelude::*, window::{PrimaryMonitor, PrimaryWindow}}; use bevy::{prelude::*, window::PrimaryWindow};
use rand::{rngs::ThreadRng, thread_rng, Rng}; use rand::{rngs::ThreadRng, thread_rng, Rng};
fn main() { fn main() {
App::new() App::new()
.add_plugins( .add_plugins(
@ -104,7 +105,7 @@ fn update_obstacles(
} }
fn spawn_obstacles( fn spawn_obstacles(
mut commands: &mut Commands, commands: &mut Commands,
rand: &mut ThreadRng, rand: &mut ThreadRng,
window_width: f32, window_width: f32,
pipe_image: &Handle<Image> pipe_image: &Handle<Image>
@ -155,9 +156,23 @@ fn update_bird(
time: Res<Time>, time: Res<Time>,
keys: Res<ButtonInput<KeyCode>>, keys: Res<ButtonInput<KeyCode>>,
game_manager: Res<GameManager>, game_manager: Res<GameManager>,
gamepads: Query<(Entity, &Gamepad)>
){ ){
let mut hit_jump = false;
for (_entity, gamepad) in &gamepads {
if gamepad.just_pressed(GamepadButton::South) {
hit_jump = true;
}
}
if keys.just_pressed(KeyCode::Space) {
hit_jump = true;
}
if let Ok((mut bird, mut transform)) = bird_query.get_single_mut(){ if let Ok((mut bird, mut transform)) = bird_query.get_single_mut(){
if keys.just_pressed(KeyCode::Space) {
if hit_jump {
bird.velocity = FLAP_FORCE; bird.velocity = FLAP_FORCE;
} }
@ -194,4 +209,6 @@ fn update_bird(
} }
} }
} }