Fly camera is improving... but not done
parent
4a3398cc74
commit
90f1df9da4
@ -0,0 +1,85 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
/// Menu Plugin; empty struct for Plugin impl
|
||||
pub(crate) struct CameraPlugin;
|
||||
|
||||
impl Plugin for CameraPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Update, editor_fly_camera);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub(crate) struct FlyCamera;
|
||||
|
||||
/// Fly camera system for moving around like a drone
|
||||
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>>,
|
||||
mut cursor_events: EventReader<CursorMoved>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let any_keys_pressed = keys.any_pressed([ KeyCode::KeyW, KeyCode::KeyS, KeyCode::KeyA, KeyCode::KeyD, KeyCode::KeyQ, KeyCode::KeyE ]);
|
||||
let cursor_movement = cursor_events.len() > 0;
|
||||
|
||||
(any_keys_pressed || cursor_movement).then(|| {
|
||||
// Iterate over all cameras
|
||||
cameras.iter_mut().for_each(|(c, mut t)| {
|
||||
// Determine which window this camera is attached to
|
||||
let target_window = match c.target {
|
||||
RenderTarget::Window(wr) => match wr {
|
||||
WindowRef::Entity(e) => Some(e),
|
||||
WindowRef::Primary => Some(primary_window.get_single().unwrap()),
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
// If the target window is focused
|
||||
windows.get(target_window.unwrap()).unwrap().focused.then(|| {
|
||||
let move_speed = 4.0;
|
||||
let mut delta = Vec3::ZERO;
|
||||
if keys.pressed(KeyCode::KeyW) {
|
||||
delta += t.forward() * move_speed * time.delta_seconds()
|
||||
}
|
||||
if keys.pressed(KeyCode::KeyS) {
|
||||
delta += t.back() * move_speed * time.delta_seconds()
|
||||
}
|
||||
if keys.pressed(KeyCode::KeyA) {
|
||||
delta += t.left() * move_speed * time.delta_seconds()
|
||||
}
|
||||
if keys.pressed(KeyCode::KeyD) {
|
||||
delta += t.right() * move_speed * time.delta_seconds()
|
||||
}
|
||||
if keys.pressed(KeyCode::KeyE) {
|
||||
delta += Vec3::Y * move_speed * time.delta_seconds()
|
||||
}
|
||||
if keys.pressed(KeyCode::KeyQ) {
|
||||
delta += Vec3::NEG_Y * move_speed * time.delta_seconds()
|
||||
}
|
||||
t.translation += delta;
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
Reference in New Issue