-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Add gamepad haptics (gamepad rumble)
- Loading branch information
Showing
12 changed files
with
276 additions
and
132 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
pub mod gamepad; | ||
|
||
pub use gamepad::GamepadRumble; | ||
|
||
use bevy::{app::AppExit, input::gamepad::*, prelude::*}; | ||
use bevy_ggrs::*; | ||
|
||
use crate::{player::Player, GameState, RollbackState}; | ||
|
||
pub const INPUT_FORWARD: u8 = 1 << 0; | ||
pub const INPUT_BACKWARD: u8 = 1 << 1; | ||
pub const INPUT_LEFT: u8 = 1 << 2; | ||
pub const INPUT_RIGHT: u8 = 1 << 3; | ||
pub const INPUT_FIRE: u8 = 1 << 4; | ||
pub const INPUT_DODGE: u8 = 1 << 5; | ||
pub const INPUT_ROCKET: u8 = 1 << 6; | ||
pub const INPUT_REMATCH: u8 = 1 << 7; | ||
|
||
pub fn input( | ||
In(local_handle): In<ggrs::PlayerHandle>, | ||
keys: Res<Input<KeyCode>>, | ||
mouse_buttons: Res<Input<MouseButton>>, | ||
gamepads: Res<Gamepads>, | ||
button_inputs: Res<Input<GamepadButton>>, | ||
button_axes: Res<Axis<GamepadButton>>, | ||
axes: Res<Axis<GamepadAxis>>, | ||
players: Query<(&Transform, &Player)>, | ||
) -> u8 { | ||
let mut input = 0u8; | ||
|
||
if keys.any_pressed([KeyCode::Up, KeyCode::W, KeyCode::K]) { | ||
input |= INPUT_FORWARD; | ||
} | ||
if keys.any_pressed([KeyCode::Down, KeyCode::S, KeyCode::J]) { | ||
input |= INPUT_BACKWARD; | ||
} | ||
if keys.any_pressed([KeyCode::Left, KeyCode::A]) { | ||
input |= INPUT_LEFT; | ||
} | ||
if keys.any_pressed([KeyCode::Right, KeyCode::D, KeyCode::F]) { | ||
input |= INPUT_RIGHT; | ||
} | ||
if keys.any_pressed([KeyCode::Space, KeyCode::Return]) { | ||
input |= INPUT_FIRE; | ||
} | ||
if keys.any_pressed([KeyCode::E, KeyCode::L]) || mouse_buttons.pressed(MouseButton::Right) { | ||
input |= INPUT_DODGE; | ||
} | ||
if keys.any_pressed([KeyCode::Q, KeyCode::Semicolon]) | ||
|| mouse_buttons.pressed(MouseButton::Left) | ||
{ | ||
input |= INPUT_ROCKET; | ||
} | ||
if keys.pressed(KeyCode::R) { | ||
input |= INPUT_REMATCH; | ||
} | ||
|
||
let controller_input = gamepad::get_gamepad_input( | ||
&gamepads, | ||
&button_inputs, | ||
&button_axes, | ||
&axes, | ||
&players, | ||
local_handle, | ||
); | ||
input |= controller_input; | ||
input | ||
} | ||
|
||
pub fn steer_direction(input: u8) -> f32 { | ||
let mut steer_direction: f32 = 0.0; | ||
if input & INPUT_LEFT != 0 { | ||
steer_direction += 1.0; | ||
} | ||
if input & INPUT_RIGHT != 0 { | ||
steer_direction -= 1.0; | ||
} | ||
steer_direction | ||
} | ||
|
||
pub fn accelerate_direction(input: u8) -> f32 { | ||
let mut accelerate_direction: f32 = 0.0; | ||
if input & INPUT_FORWARD != 0 { | ||
accelerate_direction += 1.0; | ||
} | ||
if input & INPUT_BACKWARD != 0 { | ||
accelerate_direction -= 1.0; | ||
} | ||
accelerate_direction | ||
} | ||
|
||
pub fn fire(input: u8) -> bool { | ||
input & INPUT_FIRE != 0 | ||
} | ||
|
||
pub fn dodge(input: u8) -> bool { | ||
input & INPUT_DODGE != 0 | ||
} | ||
|
||
pub fn rocket(input: u8) -> bool { | ||
input & INPUT_ROCKET != 0 | ||
} | ||
|
||
pub fn rematch(input: u8) -> bool { | ||
input & INPUT_REMATCH != 0 | ||
} | ||
|
||
pub fn quit( | ||
mut exit: EventWriter<AppExit>, | ||
keys: Res<Input<KeyCode>>, | ||
gamepads: Res<Gamepads>, | ||
button_inputs: Res<Input<GamepadButton>>, | ||
) { | ||
let mut pressed = keys.pressed(KeyCode::Q); | ||
for gamepad in gamepads.iter() { | ||
if button_inputs.pressed(GamepadButton::new(gamepad, GamepadButtonType::East)) { | ||
pressed = true; | ||
} | ||
} | ||
if pressed { | ||
exit.send(AppExit); | ||
} | ||
} | ||
|
||
pub struct AceInputPlugin; | ||
|
||
impl Plugin for AceInputPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_systems( | ||
Update, | ||
( | ||
gamepad::rumble_gamepads, | ||
quit.run_if( | ||
in_state(GameState::MainMenu) | ||
.or_else(in_state(GameState::Matchmaking)) | ||
.or_else( | ||
in_state(GameState::InRollbackGame) | ||
.and_then(in_state(RollbackState::GameOver)), | ||
), | ||
), | ||
), | ||
) | ||
.init_resource::<GamepadRumble>() | ||
.add_systems(OnExit(GameState::AssetLoading), gamepad::configure_gamepads); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.