experiment/src/comps/core/controller.rs

79 lines
2.4 KiB
Rust
Raw Normal View History

use bevy::{prelude::*, window::CursorGrabMode};
2023-09-12 04:47:37 +00:00
use bevy_rapier3d::prelude::*;
2023-09-12 16:33:51 +00:00
use crate::logic::core::player::player_movement::{
move_player, PlayerLinearXZState, PlayerLinearYState, PlayerMovementInput,
};
2023-09-12 04:47:37 +00:00
use super::markers::player::Player;
/// System that captures input and fires events
2023-09-12 16:33:51 +00:00
pub fn capture_input(
keyboard_input: Res<Input<KeyCode>>,
player_query: Query<
(
&mut Velocity,
&mut ExternalImpulse,
&mut PlayerLinearYState,
&mut PlayerLinearXZState,
&Transform,
),
With<Player>,
>,
time: Res<Time>,
) {
2023-09-12 04:47:37 +00:00
// Don't allocate on each frame. Instead Check if any of the inputs are being pressed and then allocate.
2023-09-12 16:33:51 +00:00
if keyboard_input.any_pressed([
KeyCode::A,
KeyCode::S,
KeyCode::D,
KeyCode::W,
KeyCode::C,
KeyCode::Space,
]) || keyboard_input.any_just_released([
KeyCode::A,
KeyCode::S,
KeyCode::D,
KeyCode::W,
KeyCode::C,
KeyCode::Space,
]) {
2023-09-12 04:47:37 +00:00
let player_movement_input = PlayerMovementInput {
up: keyboard_input.just_pressed(KeyCode::Space),
2023-09-12 04:47:37 +00:00
down: keyboard_input.pressed(KeyCode::C),
left: keyboard_input.pressed(KeyCode::A),
right: keyboard_input.pressed(KeyCode::D),
front: keyboard_input.pressed(KeyCode::W),
back: keyboard_input.pressed(KeyCode::S),
sprint: keyboard_input.pressed(KeyCode::ShiftLeft),
};
2023-09-12 16:33:51 +00:00
move_player(player_movement_input, player_query, time);
2023-09-12 04:47:37 +00:00
}
}
pub fn capture_cursor(
mut windows: Query<&mut Window>,
btn: Res<Input<MouseButton>>,
key: Res<Input<KeyCode>>,
) {
let mut window = windows.single_mut();
if btn.just_pressed(MouseButton::Left) {
// if you want to use the cursor, but not let it leave the window,
// use `Confined` mode:
// window.cursor.grab_mode = CursorGrabMode::Confined;
// for a game that doesn't use the cursor (like a shooter):
// use `Locked` mode to keep the cursor in one place
window.cursor.grab_mode = CursorGrabMode::Locked;
// also hide the cursor
window.cursor.visible = false;
}
if key.just_pressed(KeyCode::Escape) {
window.cursor.grab_mode = CursorGrabMode::None;
// also hide the cursor
window.cursor.visible = true;
}
2023-09-12 16:33:51 +00:00
}