fly camera

This commit is contained in:
2025-05-10 16:41:13 -04:00
parent a04fcf3c60
commit b5cbf8e386
2 changed files with 117 additions and 1742 deletions

View File

@@ -1,14 +1,32 @@
use bevy::prelude::*; use std::f32::consts::FRAC_PI_2;
use bevy::{
input::mouse::MouseMotion,
prelude::*,
window::{CursorGrabMode, PrimaryWindow},
};
// use bevy_rapier3d::prelude::*; // use bevy_rapier3d::prelude::*;
#[derive(Default)] #[derive(Default)]
pub struct GamePlugin {} pub struct GamePlugin;
#[derive(Component, Default)]
pub struct MainCamera;
#[derive(Component, Default)]
pub struct CameraRoot;
#[derive(Component, Default)]
pub struct CameraPitch(pub f32);
#[derive(Component)]
pub struct Unfocused;
impl Plugin for GamePlugin { impl Plugin for GamePlugin {
fn build(&self, app: &mut bevy::app::App) { fn build(&self, app: &mut bevy::app::App) {
// app.add_plugins(RapierPhysicsPlugin::<NoUserData>::default()); // app.add_plugins(RapierPhysicsPlugin::<NoUserData>::default());
app.add_systems(Startup, (setup_scene, spawn_ship)); app.add_systems(Startup, (setup_scene, spawn_ship));
app.add_systems(Update, ((camera_yaw, camera_pitch).chain(), camera_toggle, fly_camera));
} }
} }
@@ -16,8 +34,23 @@ fn setup_scene(
mut commands: Commands, mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
mut window: Single<&mut Window, With<PrimaryWindow>>,
) { ) {
commands.spawn((Camera3d::default(), Transform::from_xyz(0.0, 1.3, 0.0))); window.cursor_options.visible = false;
window.cursor_options.grab_mode = CursorGrabMode::Locked;
commands.spawn((
Name::new("Camera Root"),
Transform::from_xyz(0.0, 1.3, 0.0),
CameraRoot::default(),
Visibility::default(),
children![(
Camera3d::default(),
CameraPitch::default(),
MainCamera::default(),
Transform::default()
)],
));
commands.spawn(( commands.spawn((
DirectionalLight { DirectionalLight {
@@ -83,3 +116,84 @@ fn spawn_ship(
], ],
)); ));
} }
pub fn camera_toggle(
key: Res<ButtonInput<KeyCode>>,
window_query: Single<&mut Window, With<PrimaryWindow>>,
camera: Single<Entity, With<CameraRoot>>,
mut commands: Commands,
) {
let mut window = window_query.into_inner();
if key.just_pressed(KeyCode::Escape) {
if window.cursor_options.visible {
commands.entity(camera.into_inner()).remove::<Unfocused>();
window.cursor_options.grab_mode = CursorGrabMode::Locked;
window.cursor_options.visible = false;
} else {
commands.entity(camera.into_inner()).insert(Unfocused);
window.cursor_options.grab_mode = CursorGrabMode::None;
window.cursor_options.visible = true;
}
}
}
const FLY_SPEED: f32 = 10.0;
pub fn fly_camera(
cam_query: Single<&mut Transform, (With<CameraRoot>, Without<Unfocused>)>,
cam_pitch: Single<&CameraPitch>,
key: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
) {
let mut move_dir = Vec3::ZERO;
let pitch = cam_pitch.0;
let mut cam_transform = cam_query.into_inner();
if key.pressed(KeyCode::KeyW) {
move_dir.z = -1.0;
} else if key.pressed(KeyCode::KeyS) {
move_dir.z = 1.0;
}
if key.pressed(KeyCode::KeyD) {
move_dir.x = 1.0;
} else if key.pressed(KeyCode::KeyA) {
move_dir.x = -1.0;
}
move_dir = cam_transform.rotation
* Quat::from_rotation_x(pitch)
* move_dir.normalize_or_zero()
* time.delta_secs()
* FLY_SPEED;
cam_transform.translation += move_dir;
}
pub fn camera_yaw(
cam_yaw_query: Single<&mut Transform, (With<CameraRoot>, Without<Unfocused>)>,
cam_query: Single<&mut CameraPitch>,
mouse_motion: EventReader<MouseMotion>,
time: Res<Time>,
) {
let mut yaw_transform = cam_yaw_query.into_inner();
let mut pitch = cam_query.into_inner();
let delta = get_mouse_delta(mouse_motion) * -time.delta_secs();
yaw_transform.rotate_y(delta.x);
pitch.0 = (pitch.0 + delta.y).clamp(-FRAC_PI_2 + 0.001, FRAC_PI_2 - 0.001);
}
pub fn camera_pitch(cam_query: Single<(&mut Transform, &CameraPitch)>) {
let (mut cam_transform, pitch) = cam_query.into_inner();
cam_transform.rotation = Quat::from_rotation_x(pitch.0);
}
fn get_mouse_delta(mut mouse_motion: EventReader<MouseMotion>) -> Vec2 {
let mut delta = Vec2::ZERO;
for e in mouse_motion.read() {
delta += e.delta;
}
return delta;
}

1739
tree.txt

File diff suppressed because it is too large Load Diff