Break fly cam into move and rotate systems

main
Elijah C. Voigt 1 year ago
parent 1e42519a31
commit fdce4410db

@ -1,5 +1,3 @@
use bevy::input::mouse::MouseButtonInput;
use crate::prelude::*; use crate::prelude::*;
/// Menu Plugin; empty struct for Plugin impl /// Menu Plugin; empty struct for Plugin impl
@ -7,7 +5,8 @@ pub(crate) struct CameraPlugin;
impl Plugin for CameraPlugin { impl Plugin for CameraPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Update, editor_fly_camera); app.add_systems(Update, move_editor_fly_camera);
app.add_systems(Update, rotate_editor_fly_camera);
} }
} }
@ -16,19 +15,21 @@ pub(crate) struct FlyCamera;
/// Fly camera system for moving around like a drone /// Fly camera system for moving around like a drone
/// TODO: Only if key is pressed! /// TODO: Only if key is pressed!
fn editor_fly_camera( fn move_editor_fly_camera(
mut cameras: Query<(&Camera, &mut Transform), With<FlyCamera>>, mut cameras: Query<(&Camera, &mut Transform), With<FlyCamera>>,
windows: Query<&Window>, windows: Query<&Window>,
primary_window: Query<Entity, With<PrimaryWindow>>, primary_window: Query<Entity, With<PrimaryWindow>>,
keys: Res<ButtonInput<KeyCode>>, keys: Res<ButtonInput<KeyCode>>,
mouse: Res<ButtonInput<MouseButton>>,
mut cursor_events: EventReader<CursorMoved>,
time: Res<Time>, time: Res<Time>,
) { ) {
let any_keys_pressed = keys.any_pressed([ KeyCode::KeyW, KeyCode::KeyS, KeyCode::KeyA, KeyCode::KeyD, KeyCode::KeyQ, KeyCode::KeyE ]); (keys.any_pressed([
let cursor_movement = cursor_events.len() > 0; KeyCode::KeyW,
KeyCode::KeyS,
(any_keys_pressed || cursor_movement).then(|| { KeyCode::KeyA,
KeyCode::KeyD,
KeyCode::KeyQ,
KeyCode::KeyE
])).then(|| {
// Iterate over all cameras // Iterate over all cameras
cameras.iter_mut().for_each(|(c, mut t)| { cameras.iter_mut().for_each(|(c, mut t)| {
// Determine which window this camera is attached to // Determine which window this camera is attached to
@ -65,7 +66,29 @@ fn editor_fly_camera(
} }
t.translation += delta; t.translation += delta;
}); });
});
});
}
fn rotate_editor_fly_camera(
mut cameras: Query<(&Camera, &mut Transform), With<FlyCamera>>,
windows: Query<&Window>,
primary_window: Query<Entity, With<PrimaryWindow>>,
mouse: Res<ButtonInput<MouseButton>>,
mut cursor_events: EventReader<CursorMoved>,
) {
(cursor_events.len() > 0).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,
};
let window = windows.get(target_window.unwrap()).unwrap();
if mouse.pressed(MouseButton::Middle) { if mouse.pressed(MouseButton::Middle) {
cursor_events.read().filter_map(|CursorMoved { delta, window, .. }| { cursor_events.read().filter_map(|CursorMoved { delta, window, .. }| {
(*window == target_window.unwrap()).then_some(delta) (*window == target_window.unwrap()).then_some(delta)

Loading…
Cancel
Save