Fly camera is improving... but not done

main
Elijah C. Voigt 1 year ago
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);
}
}
});
});
});
}

@ -11,6 +11,7 @@ impl Plugin for EditorPlugin {
app.add_systems(OnEnter(EditorState::Open), open_editor); app.add_systems(OnEnter(EditorState::Open), open_editor);
app.add_systems(OnExit(EditorState::Open), close_editor); app.add_systems(OnExit(EditorState::Open), close_editor);
app.add_systems(Update, origin_directions.run_if(in_state(EditorState::Open))); app.add_systems(Update, origin_directions.run_if(in_state(EditorState::Open)));
app.add_systems(Update, world_plane.run_if(in_state(EditorState::Open)));
} }
} }
@ -46,11 +47,13 @@ fn init_editor(
// Spawn editor camera // Spawn editor camera
let _editor_camera = parent.spawn(( let _editor_camera = parent.spawn((
Editor, Editor,
FlyCamera,
Camera3dBundle { Camera3dBundle {
camera: Camera { camera: Camera {
target: RenderTarget::Window(WindowRef::Entity(editor_window)), target: RenderTarget::Window(WindowRef::Entity(editor_window)),
..default() ..default()
}, },
transform: Transform::from_xyz(1.0, 1.0, 1.0).looking_at(Vec3::ZERO, Vec3::Y),
..default() ..default()
}, },
)).id(); )).id();
@ -59,7 +62,6 @@ fn init_editor(
fn open_editor( fn open_editor(
mut ws: Query<&mut Window, With<Editor>>, mut ws: Query<&mut Window, With<Editor>>,
state: Res<State<EditorState>>,
) { ) {
ws.iter_mut().for_each(|mut w| { ws.iter_mut().for_each(|mut w| {
w.visible = true; w.visible = true;
@ -68,7 +70,6 @@ fn open_editor(
fn close_editor( fn close_editor(
mut ws: Query<&mut Window, With<Editor>>, mut ws: Query<&mut Window, With<Editor>>,
state: Res<State<EditorState>>,
) { ) {
ws.iter_mut().for_each(|mut w| { ws.iter_mut().for_each(|mut w| {
w.visible = false; w.visible = false;
@ -95,5 +96,26 @@ fn toggle_editor(
fn origin_directions( fn origin_directions(
mut gizmos: Gizmos mut gizmos: Gizmos
) { ) {
gizmos.arrow(Vec3::ZERO, Vec3::ONE, Color::GREEN); gizmos.arrow(Vec3::ZERO, Vec3::X, Color::RED);
gizmos.arrow(Vec3::ZERO, Vec3::Y, Color::GREEN);
gizmos.arrow(Vec3::ZERO, Vec3::Z, Color::BLUE);
}
fn world_plane(
mut gizmos: Gizmos
) {
(-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);
gizmos.line(start, end, Color::GRAY);
}
{
let start = Vec3::new(-10.0, 0.0, z as f32);
let end = Vec3::new(10.0, 0.0, z as f32);
gizmos.line(start, end, Color::GRAY);
}
});
});
} }

@ -50,7 +50,6 @@ fn init_dice_cuboid(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>) {
[1.0, e], [0.0, e], [0.0, f], [1.0, f], [1.0, e], [0.0, e], [0.0, f], [1.0, f],
], ],
); );
info!("Adding cuboid {:?}", cuboid);
commands.insert_resource(DiceCuboid { mesh: meshes.add(cuboid) }); commands.insert_resource(DiceCuboid { mesh: meshes.add(cuboid) });
} }

@ -22,6 +22,9 @@ pub(crate) mod game;
/// Debugger/Editor for inspecting the game /// Debugger/Editor for inspecting the game
pub(crate) mod editor; pub(crate) mod editor;
/// Camera control, mostly used in debug editor
pub(crate) mod camera;
use crate::prelude::*; use crate::prelude::*;
fn main() { fn main() {
@ -39,6 +42,7 @@ fn main() {
app.add_plugins(ui::UiPlugin); app.add_plugins(ui::UiPlugin);
app.add_plugins(game::GamePlugin); app.add_plugins(game::GamePlugin);
app.add_plugins(editor::EditorPlugin); app.add_plugins(editor::EditorPlugin);
app.add_plugins(camera::CameraPlugin);
app.add_systems(Update, app.add_systems(Update,
handle_window_close.run_if(on_event::<WindowCloseRequested>()) handle_window_close.run_if(on_event::<WindowCloseRequested>())
); );

@ -30,3 +30,4 @@ pub(crate) use crate::ui::style::UiStyle;
pub(crate) use crate::ui::title::UiTitle; pub(crate) use crate::ui::title::UiTitle;
pub(crate) use crate::ui::EmitEvent; pub(crate) use crate::ui::EmitEvent;
pub(crate) use crate::ui::SetState; pub(crate) use crate::ui::SetState;
pub(crate) use crate::camera::FlyCamera;

Loading…
Cancel
Save