From 6e3d9586387e2e46720cf33e9a80fb290acbc613 Mon Sep 17 00:00:00 2001 From: hut Date: Sat, 30 Mar 2024 16:32:42 +0100 Subject: [PATCH] mouse movement now changes player orientation relatively, not absolutely --- src/camera.rs | 42 ++++++++---------------------------------- 1 file changed, 8 insertions(+), 34 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index f32098b..09aa42a 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -24,25 +24,6 @@ impl Plugin for CameraControllerPlugin { // it because it felt nice. pub const RADIANS_PER_DOT: f32 = 1.0 / 180.0; -#[derive(Component)] -pub struct CameraController { - pub enabled: bool, - pub initialized: bool, - pub pitch: f32, - pub yaw: f32, -} - -impl Default for CameraController { - fn default() -> Self { - Self { - enabled: true, - initialized: false, - pitch: 1.0, // pitch=0/yaw=0 -> face sun - yaw: 0.3, - } - } -} - fn setup_camera( mut commands: Commands, ) { @@ -57,7 +38,6 @@ fn setup_camera( transform: Transform::from_xyz(0.0, 0.0, 8.0).looking_at(Vec3::ZERO, Vec3::Y), ..default() }, - CameraController::default(), BloomSettings { composite_mode: BloomCompositeMode::EnergyConserving, ..default() @@ -67,8 +47,8 @@ fn setup_camera( pub fn sync_camera_to_player( settings: Res, - mut q_camera: Query<&mut Transform, With>, - q_playercam: Query<(&actor::Actor, &Transform), (With, Without, Without)>, + mut q_camera: Query<&mut Transform, With>, + q_playercam: Query<(&actor::Actor, &Transform), (With, Without, Without)>, ) { if q_camera.is_empty() || q_playercam.is_empty() { return; @@ -126,14 +106,14 @@ fn run_camera_controller( rocket_sound_controller: Query<&AudioSink, With>, ion_sound_controller: Query<&AudioSink, With>, mut q_engine: Query<&mut actor::Engine, With>, - mut q_camera: Query<(&mut CameraController, &mut Projection)>, + mut q_camera: Query<&mut Projection, With>, q_player: Query<&actor::LifeForm, With>, mut q_playercam: Query<( &mut Transform, &mut actor::Engine, &mut AngularVelocity, &mut LinearVelocity, - ), (With, Without, Without)>, + ), (With, Without, Without)>, ) { let dt = time.delta_seconds(); let mut play_thruster_sound = false; @@ -145,14 +125,10 @@ fn run_camera_controller( } if let ( - Ok((mut controller, mut projection)), + Ok(mut projection), Ok(lifeform), Ok((mut player_transform, player_engine, mut angularvelocity, mut v)), ) = (q_camera.get_single_mut(), q_player.get_single(), q_playercam.get_single_mut()) { - if !controller.enabled { - mouse_events.clear(); - return; - } if angularvelocity.length_squared() > 0.0001 { angularvelocity.x *= 0.98; @@ -242,11 +218,9 @@ fn run_camera_controller( if mouse_delta != Vec2::ZERO { // Apply look update - controller.pitch = (controller.pitch - + mouse_delta.y * RADIANS_PER_DOT * settings.mouse_sensitivity) - .clamp(-PI / 2., PI / 2.); - controller.yaw += mouse_delta.x * RADIANS_PER_DOT * settings.mouse_sensitivity; - player_transform.rotation = Quat::from_euler(EulerRot::ZYX, 0.0, -controller.yaw, controller.pitch).normalize(); + let pitch = (mouse_delta.y * RADIANS_PER_DOT * settings.mouse_sensitivity).clamp(-PI / 2., PI / 2.); + let yaw = mouse_delta.x * RADIANS_PER_DOT * settings.mouse_sensitivity; + player_transform.rotation *= Quat::from_euler(EulerRot::ZYX, 0.0, -yaw, pitch).normalize(); } let fov = (lifeform.adrenaline * lifeform.adrenaline * lifeform.adrenaline * 45.0 + 45.0).to_radians();