first support for gamepad controls
This commit is contained in:
@@ -1,26 +1,21 @@
|
||||
use std::{f32::consts::PI, time::Duration};
|
||||
|
||||
use crate::{
|
||||
DebugVisuals,
|
||||
alien::{ALIEN_ASSET_PATH, Animations},
|
||||
camera::GameCameraRig,
|
||||
cash::{Cash, CashCollectEvent},
|
||||
controls::Controls,
|
||||
heads_ui::HeadChanged,
|
||||
shooting::TriggerState,
|
||||
tb_entities::SpawnPoint,
|
||||
};
|
||||
use avian3d::prelude::*;
|
||||
use bevy::{
|
||||
input::{
|
||||
ButtonState,
|
||||
mouse::{MouseButtonInput, MouseMotion},
|
||||
},
|
||||
prelude::*,
|
||||
window::{CursorGrabMode, PrimaryWindow},
|
||||
};
|
||||
use bevy_dolly::prelude::Rig;
|
||||
use bevy_tnua::{TnuaUserControlsSystemSet, prelude::*};
|
||||
use bevy_tnua_avian3d::TnuaAvian3dSensorShape;
|
||||
use std::{f32::consts::PI, time::Duration};
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub struct Player;
|
||||
@@ -63,10 +58,9 @@ pub fn plugin(app: &mut App) {
|
||||
apply_controls.in_set(TnuaUserControlsSystemSet),
|
||||
);
|
||||
|
||||
app.add_systems(Update, mouse_rotate.run_if(on_event::<MouseMotion>));
|
||||
app.add_systems(Update, mouse_click.run_if(on_event::<MouseButtonInput>));
|
||||
app.add_systems(Update, rotate_view);
|
||||
|
||||
app.add_observer(updaate_head);
|
||||
app.add_observer(update_head);
|
||||
}
|
||||
|
||||
fn spawn(
|
||||
@@ -125,36 +119,16 @@ fn spawn(
|
||||
player_spawned.spawned = true;
|
||||
}
|
||||
|
||||
fn mouse_rotate(
|
||||
mut mouse: EventReader<MouseMotion>,
|
||||
fn rotate_view(
|
||||
controls: Res<Controls>,
|
||||
// todo: Put the player head as a child of the rig to avoid this mess:
|
||||
mut player: Query<&mut Transform, Or<(With<PlayerRig>, With<PlayerHead>)>>,
|
||||
) {
|
||||
for ev in mouse.read() {
|
||||
for mut tr in &mut player {
|
||||
tr.rotate_y(ev.delta.x * -0.001);
|
||||
}
|
||||
}
|
||||
}
|
||||
for mut tr in &mut player {
|
||||
tr.rotate_y(controls.keyboard_state.look_dir.x * -0.001);
|
||||
|
||||
fn mouse_click(mut events: EventReader<MouseButtonInput>, mut commands: Commands) {
|
||||
for ev in events.read() {
|
||||
match ev {
|
||||
MouseButtonInput {
|
||||
button: MouseButton::Left,
|
||||
state: ButtonState::Pressed,
|
||||
..
|
||||
} => {
|
||||
commands.trigger(TriggerState::Active);
|
||||
}
|
||||
MouseButtonInput {
|
||||
button: MouseButton::Left,
|
||||
state: ButtonState::Released,
|
||||
..
|
||||
} => {
|
||||
commands.trigger(TriggerState::Inactive);
|
||||
}
|
||||
_ => {}
|
||||
if let Some(gamepad) = controls.gamepad_state {
|
||||
tr.rotate_y(gamepad.look_dir.x * -0.001);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -181,10 +155,10 @@ fn initial_grab_cursor(mut primary_window: Query<&mut Window, With<PrimaryWindow
|
||||
}
|
||||
|
||||
fn apply_controls(
|
||||
keyboard: Res<ButtonInput<KeyCode>>,
|
||||
controls: Res<Controls>,
|
||||
mut query: Query<&mut TnuaController>,
|
||||
player: Query<&Transform, With<PlayerRig>>,
|
||||
mut controls: ResMut<PlayerMovement>,
|
||||
mut movement: ResMut<PlayerMovement>,
|
||||
) {
|
||||
let Ok(mut controller) = query.get_single_mut() else {
|
||||
return;
|
||||
@@ -194,23 +168,15 @@ fn apply_controls(
|
||||
return;
|
||||
};
|
||||
|
||||
let mut direction = Vec3::ZERO;
|
||||
let controls = controls
|
||||
.gamepad_state
|
||||
.unwrap_or_else(|| controls.keyboard_state);
|
||||
|
||||
if keyboard.pressed(KeyCode::KeyW) {
|
||||
direction = player.forward().as_vec3() * -1.;
|
||||
}
|
||||
if keyboard.pressed(KeyCode::KeyS) {
|
||||
direction = player.forward().as_vec3();
|
||||
}
|
||||
if keyboard.pressed(KeyCode::KeyA) {
|
||||
direction += player.right().as_vec3();
|
||||
}
|
||||
if keyboard.pressed(KeyCode::KeyD) {
|
||||
direction += player.left().as_vec3();
|
||||
}
|
||||
let mut direction = player.forward().as_vec3() * -controls.move_dir.y;
|
||||
direction += player.right().as_vec3() * -controls.move_dir.x;
|
||||
|
||||
if controls.any_direction != (direction != Vec3::ZERO) {
|
||||
controls.any_direction = direction != Vec3::ZERO;
|
||||
if movement.any_direction != (direction != Vec3::ZERO) {
|
||||
movement.any_direction = direction != Vec3::ZERO;
|
||||
}
|
||||
|
||||
controller.basis(TnuaBuiltinWalk {
|
||||
@@ -228,7 +194,7 @@ fn apply_controls(
|
||||
|
||||
// Feed the jump action every frame as long as the player holds the jump button. If the player
|
||||
// stops holding the jump button, simply stop feeding the action.
|
||||
if keyboard.pressed(KeyCode::Space) {
|
||||
if controls.jump {
|
||||
controller.action(TnuaBuiltinJump {
|
||||
// The height is the only mandatory field of the jump button.
|
||||
height: 4.0,
|
||||
@@ -317,7 +283,7 @@ fn toggle_animation(
|
||||
}
|
||||
}
|
||||
|
||||
fn updaate_head(
|
||||
fn update_head(
|
||||
trigger: Trigger<HeadChanged>,
|
||||
mut commands: Commands,
|
||||
asset_server: Res<AssetServer>,
|
||||
|
||||
Reference in New Issue
Block a user