bulk commit

This commit is contained in:
yuni 2024-03-17 00:24:47 +01:00
parent 1514fb5b02
commit e400628af5
4 changed files with 45 additions and 47 deletions

View file

@ -1,6 +1,6 @@
use bevy::prelude::*;
use bevy::input::mouse::MouseMotion;
use bevy::window::{Window, CursorGrabMode};
use bevy::window::PrimaryWindow;
use std::{f32::consts::*, fmt};
pub struct CameraControllerPlugin;
@ -48,9 +48,9 @@ impl Default for CameraController {
key_back: KeyCode::KeyS,
key_left: KeyCode::KeyA,
key_right: KeyCode::KeyD,
key_up: KeyCode::KeyE,
key_down: KeyCode::KeyQ,
key_run: KeyCode::ShiftLeft,
key_up: KeyCode::ShiftLeft,
key_down: KeyCode::ControlLeft,
key_run: KeyCode::KeyR,
mouse_key_cursor_grab: MouseButton::Left,
keyboard_key_toggle_cursor_grab: KeyCode::KeyM,
walk_speed: 5.0,
@ -92,7 +92,7 @@ Freecam Controls:
#[allow(clippy::too_many_arguments)]
fn run_camera_controller(
time: Res<Time>,
mut windows: Query<&mut Window>,
mut windows: Query<&mut Window, With<PrimaryWindow>>,
mut mouse_events: EventReader<MouseMotion>,
mouse_button_input: Res<ButtonInput<MouseButton>>,
key_input: Res<ButtonInput<KeyCode>>,
@ -102,6 +102,12 @@ fn run_camera_controller(
) {
let dt = time.delta_seconds();
let window_result = windows.get_single_mut();
let mut focused = true;
if window_result.is_ok() {
focused = window_result.unwrap().focused;
}
if let Ok((mut transform, mut controller)) = query.get_single_mut() {
if !controller.initialized {
let (yaw, pitch, _roll) = transform.rotation.to_euler(EulerRot::YXZ);
@ -118,37 +124,35 @@ fn run_camera_controller(
// Handle key input
let mut axis_input = Vec3::ZERO;
if key_input.pressed(controller.key_forward) {
axis_input.z += 1.0;
}
if key_input.pressed(controller.key_back) {
axis_input.z -= 1.0;
}
if key_input.pressed(controller.key_right) {
axis_input.x += 1.0;
}
if key_input.pressed(controller.key_left) {
axis_input.x -= 1.0;
}
if key_input.pressed(controller.key_up) {
axis_input.y += 1.0;
}
if key_input.pressed(controller.key_down) {
axis_input.y -= 1.0;
if focused {
if key_input.pressed(controller.key_forward) {
axis_input.z += 1.0;
}
if key_input.pressed(controller.key_back) {
axis_input.z -= 1.0;
}
if key_input.pressed(controller.key_right) {
axis_input.x += 1.0;
}
if key_input.pressed(controller.key_left) {
axis_input.x -= 1.0;
}
if key_input.pressed(controller.key_up) {
axis_input.y += 1.0;
}
if key_input.pressed(controller.key_down) {
axis_input.y -= 1.0;
}
}
let mut cursor_grab_change = false;
if key_input.just_pressed(controller.keyboard_key_toggle_cursor_grab) {
*toggle_cursor_grab = !*toggle_cursor_grab;
cursor_grab_change = true;
}
if mouse_button_input.just_pressed(controller.mouse_key_cursor_grab) {
*mouse_cursor_grab = true;
cursor_grab_change = true;
}
if mouse_button_input.just_released(controller.mouse_key_cursor_grab) {
*mouse_cursor_grab = false;
cursor_grab_change = true;
}
let cursor_grab = *mouse_cursor_grab || *toggle_cursor_grab;
@ -173,25 +177,6 @@ fn run_camera_controller(
+ controller.velocity.y * dt * Vec3::Y
+ controller.velocity.z * dt * forward;
// Handle cursor grab
if cursor_grab_change {
if cursor_grab {
for mut window in &mut windows {
if !window.focused {
continue;
}
window.cursor.grab_mode = CursorGrabMode::Locked;
window.cursor.visible = false;
}
} else {
for mut window in &mut windows {
window.cursor.grab_mode = CursorGrabMode::None;
window.cursor.visible = true;
}
}
}
// Handle mouse input
let mut mouse_delta = Vec2::ZERO;
if cursor_grab {

View file

@ -35,6 +35,7 @@ fn setup(
window.cursor.grab_mode = CursorGrabMode::Locked;
window.cursor.visible = false;
window.mode = WindowMode::Fullscreen;
window.title = "OutFly".to_string();
}
}

View file

@ -1,4 +1,5 @@
use bevy::prelude::*;
use bevy::window::PrimaryWindow;
#[derive(Component)]
pub struct Player {
@ -23,7 +24,18 @@ pub fn setup(mut commands: Commands) {
pub fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut windows: Query<&mut Window, With<PrimaryWindow>>,
) {
if keyboard_input.pressed(KeyCode::KeyW) {
let window_result = windows.get_single_mut();
let mut focused = true;
if window_result.is_ok() {
let window = window_result.unwrap();
if !window.focused {
focused = false;
}
}
if focused {
if keyboard_input.pressed(KeyCode::KeyW) {
}
}
}

View file

@ -53,7 +53,7 @@ pub fn setup(
transform: Transform::from_xyz(
0.0,
0.0,
-2000.0,
-200.0,
),
..default()
});