| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use bevy::{ |
| 5 |
input::gamepad::{Gamepad, GamepadRumbleIntensity, GamepadRumbleRequest}, |
| 6 |
prelude::*, |
| 7 |
}; |
| 8 |
use core::time::Duration; |
| 9 |
|
| 10 |
fn main() { |
| 11 |
App::new() |
| 12 |
.add_plugins(DefaultPlugins) |
| 13 |
.add_systems(Update, gamepad_system) |
| 14 |
.run(); |
| 15 |
} |
| 16 |
|
| 17 |
fn gamepad_system( |
| 18 |
gamepads: Query<(Entity, &Gamepad)>, |
| 19 |
mut rumble_requests: MessageWriter<GamepadRumbleRequest>, |
| 20 |
) { |
| 21 |
for (entity, gamepad) in &gamepads { |
| 22 |
if gamepad.just_pressed(GamepadButton::North) { |
| 23 |
info!( |
| 24 |
"North face button: strong (low-frequency) with low intensity for rumble for 5 seconds. Press multiple times to increase intensity." |
| 25 |
); |
| 26 |
rumble_requests.write(GamepadRumbleRequest::Add { |
| 27 |
gamepad: entity, |
| 28 |
intensity: GamepadRumbleIntensity::strong_motor(0.1), |
| 29 |
duration: Duration::from_secs(5), |
| 30 |
}); |
| 31 |
} |
| 32 |
|
| 33 |
if gamepad.just_pressed(GamepadButton::East) { |
| 34 |
info!("East face button: maximum rumble on both motors for 5 seconds"); |
| 35 |
rumble_requests.write(GamepadRumbleRequest::Add { |
| 36 |
gamepad: entity, |
| 37 |
duration: Duration::from_secs(5), |
| 38 |
intensity: GamepadRumbleIntensity::MAX, |
| 39 |
}); |
| 40 |
} |
| 41 |
|
| 42 |
if gamepad.just_pressed(GamepadButton::South) { |
| 43 |
info!("South face button: low-intensity rumble on the weak motor for 0.5 seconds"); |
| 44 |
rumble_requests.write(GamepadRumbleRequest::Add { |
| 45 |
gamepad: entity, |
| 46 |
duration: Duration::from_secs_f32(0.5), |
| 47 |
intensity: GamepadRumbleIntensity::weak_motor(0.25), |
| 48 |
}); |
| 49 |
} |
| 50 |
|
| 51 |
if gamepad.just_pressed(GamepadButton::West) { |
| 52 |
info!("West face button: custom rumble intensity for 5 second"); |
| 53 |
rumble_requests.write(GamepadRumbleRequest::Add { |
| 54 |
gamepad: entity, |
| 55 |
intensity: GamepadRumbleIntensity { |
| 56 |
|
| 57 |
strong_motor: 0.5, |
| 58 |
|
| 59 |
weak_motor: 0.25, |
| 60 |
}, |
| 61 |
duration: Duration::from_secs(5), |
| 62 |
}); |
| 63 |
} |
| 64 |
|
| 65 |
if gamepad.just_pressed(GamepadButton::Start) { |
| 66 |
info!("Start button: Interrupt the current rumble"); |
| 67 |
rumble_requests.write(GamepadRumbleRequest::Stop { gamepad: entity }); |
| 68 |
} |
| 69 |
} |
| 70 |
} |