Cribbed from bevy flycam plugin, works much better

main
Elijah C. Voigt 1 year ago
parent 90f1df9da4
commit 1e42519a31

@ -1,3 +1,5 @@
use bevy::input::mouse::MouseButtonInput;
use crate::prelude::*;
/// Menu Plugin; empty struct for Plugin impl
@ -13,11 +15,13 @@ impl Plugin for CameraPlugin {
pub(crate) struct FlyCamera;
/// Fly camera system for moving around like a drone
/// TODO: Only if key is pressed!
fn editor_fly_camera(
mut cameras: Query<(&Camera, &mut Transform), With<FlyCamera>>,
windows: Query<&Window>,
primary_window: Query<Entity, With<PrimaryWindow>>,
keys: Res<ButtonInput<KeyCode>>,
mouse: Res<ButtonInput<MouseButton>>,
mut cursor_events: EventReader<CursorMoved>,
time: Res<Time>,
) {
@ -35,8 +39,10 @@ fn editor_fly_camera(
},
_ => None,
};
let window = windows.get(target_window.unwrap()).unwrap();
// If the target window is focused
windows.get(target_window.unwrap()).unwrap().focused.then(|| {
window.focused.then(|| {
let move_speed = 4.0;
let mut delta = Vec3::ZERO;
if keys.pressed(KeyCode::KeyW) {
@ -60,26 +66,24 @@ fn editor_fly_camera(
t.translation += delta;
});
if mouse.pressed(MouseButton::Middle) {
cursor_events.read().filter_map(|CursorMoved { delta, window, .. }| {
(*window == target_window.unwrap()).then_some(delta)
}).for_each(|delta| {
// If there was actual movement, unpack those values
if let Some(Vec2 { x, y }) = delta {
if x > y {
// Find the horizontal angle of movement
let width = 1280.0;
let ratio_width = x / width;
let angle_width = ratio_width * std::f32::consts::PI;
t.rotate_local_y(-angle_width);
} else {
// Find the vertical angle of movement
let height = 720.0;
let ratio_height = y / height;
let angle_height = ratio_height * std::f32::consts::PI;
t.rotate_local_x(-angle_height);
}
// Cribbing from bevy_flycam
// Link: https://github.com/sburris0/bevy_flycam/blob/baffe50e0961ad1491d467fa6ab5551f9f21db8f/src/lib.rs#L145-L151
let (mut yaw, mut pitch, _) = t.rotation.to_euler(EulerRot::YXZ);
let window_scale = window.height().min(window.width());
let sensitivity = 0.00012;
pitch -= (sensitivity * y * window_scale).to_radians();
yaw -= (sensitivity * x * window_scale).to_radians();
t.rotation = Quat::from_axis_angle(Vec3::Y, yaw) * Quat::from_axis_angle(Vec3::X, pitch);
}
});
} else {
cursor_events.clear();
}
});
});
}

@ -104,8 +104,8 @@ fn origin_directions(
fn world_plane(
mut gizmos: Gizmos
) {
(-10..10).into_iter().for_each(|x| {
(-10..10).into_iter().for_each(|z| {
(-10..=10).into_iter().for_each(|x| {
(-10..=10).into_iter().for_each(|z| {
{
let start = Vec3::new(x as f32, 0.0, -10.0);
let end = Vec3::new(x as f32, 0.0, 10.0);

Loading…
Cancel
Save