From da58cfc71738b2addf1a3d20007af1940576172f Mon Sep 17 00:00:00 2001 From: yuni Date: Mon, 25 Nov 2024 00:15:23 +0100 Subject: [PATCH] fix flashlight flicker? (cant be sure, since it occurs randomly) --- src/actor.rs | 17 ----------------- src/camera.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++++- src/cmd.rs | 27 --------------------------- 3 files changed, 46 insertions(+), 45 deletions(-) diff --git a/src/actor.rs b/src/actor.rs index 7b5621e..f213544 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -562,14 +562,6 @@ pub fn handle_vehicle_enter_exit( mut commands: Commands, mut er_vehicle: EventReader, mut ew_achievement: EventWriter, - mut q_playerflashlight: Query< - &mut Transform, - ( - With, - Without, - Without, - ), - >, mut q_drivers: Query< (Entity, &mut Visibility, Option<&Collider>), ( @@ -615,10 +607,6 @@ pub fn handle_vehicle_enter_exit( commands.entity(vehicle).insert(PlayerCamera); commands.entity(vehicle).insert(PlayerDrivesThis); commands.entity(vehicle).insert(WantsMaxRotation(0.0)); - if let Ok(mut flashlight_trans) = q_playerflashlight.get_single_mut() { - flashlight_trans.rotation = Quat::from_rotation_y(0f32); - flashlight_trans.translation = Vec3::new(0.0, 0.0, 0.0); - } if let Some(vehicle_name) = &event.name { ew_achievement.send(game::AchievementEvent::RideVehicle( vehicle_name.clone(), @@ -629,11 +617,6 @@ pub fn handle_vehicle_enter_exit( if let Some(collider) = &vehicle_component.stored_drivers_collider { commands.entity(driver).insert(collider.clone()); } - if let Ok(mut flashlight_trans) = q_playerflashlight.get_single_mut() { - flashlight_trans.rotation = - Quat::from_rotation_y(180f32.to_radians()); - flashlight_trans.translation = Vec3::new(0.0, 0.0, 1.0); - } commands.entity(driver).remove::(); commands.entity(driver).insert(WantsAcceleration::default()); commands.entity(driver).insert(RigidBody::Dynamic); diff --git a/src/camera.rs b/src/camera.rs index bd73ed7..d00ac0b 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -123,7 +123,11 @@ impl Default for MapCam { } } -pub fn setup_camera(mut commands: Commands, settings: Res) { +pub fn setup_camera( + mut commands: Commands, + settings: Res, + prefs: Res, +) { // Add player commands.spawn(( Camera3dBundle { @@ -152,6 +156,30 @@ pub fn setup_camera(mut commands: Commands, settings: Res) { }, )); + // Add player's flashlight + commands.spawn(( + world::DespawnOnPlayerDeath, + actor::PlayersFlashLight, + SpotLightBundle { + transform: Transform { + translation: Vec3::new(0.0, 0.0, 1.0), + rotation: Quat::from_rotation_y(180f32.to_radians()), + ..default() + }, + spot_light: SpotLight { + intensity: actor::FLASHLIGHT_INTENSITY[prefs.flashlight_power], + color: Color::WHITE, + shadows_enabled: true, + inner_angle: PI32 / 8.0 * 0.65, + outer_angle: PI32 / 8.0 * 1.2, + range: 300000.0, + ..default() + }, + visibility: Visibility::Hidden, + ..default() + }, + )); + // Add Light from the Sun commands.spawn(DirectionalLightBundle { directional_light: DirectionalLight { @@ -179,6 +207,14 @@ pub fn sync_camera_to_player( settings: Res, mut q_camera: Query<&mut Transform, (With, Without)>, q_playercam: Query<(&actor::Actor, &Transform), (With, Without)>, + mut q_flashlight: Query< + &mut Transform, + ( + With, + Without, + Without, + ), + >, ) { if settings.map_active || q_camera.is_empty() || q_playercam.is_empty() { return; @@ -186,6 +222,15 @@ pub fn sync_camera_to_player( let mut camera_transform = q_camera.get_single_mut().unwrap(); let (actor, player_transform) = q_playercam.get_single().unwrap(); + // Flashlight + if let Ok(mut flashlight_trans) = q_flashlight.get_single_mut() { + let forward = player_transform.translation + + player_transform.rotation * Vec3::new(0.0, 0.0, player_transform.scale.z); + flashlight_trans.translation = player_transform.translation; + flashlight_trans.look_at(forward, Dir3::Y); + flashlight_trans.translation = forward; + } + // Rotation let rotation = player_transform.rotation * Quat::from_array([0.0, -1.0, 0.0, 0.0]); diff --git a/src/cmd.rs b/src/cmd.rs index cace18e..1bb05be 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -1275,7 +1275,6 @@ fn spawn_entities( mut achievement_tracker: ResMut, mut ew_updateavatar: EventWriter, settings: Res, - prefs: Res, ) { for state_wrapper in er_spawn.read() { let state = &state_wrapper.0; @@ -1571,32 +1570,6 @@ fn spawn_entities( )); }); } - if state.is_player { - actor.with_children(|builder| { - builder.spawn(( - world::DespawnOnPlayerDeath, - actor::PlayersFlashLight, - SpotLightBundle { - transform: Transform { - translation: Vec3::new(0.0, 0.0, 1.0), - rotation: Quat::from_rotation_y(180f32.to_radians()), - ..default() - }, - spot_light: SpotLight { - intensity: actor::FLASHLIGHT_INTENSITY[prefs.flashlight_power], - color: Color::WHITE, - shadows_enabled: true, - inner_angle: PI32 / 8.0 * 0.65, - outer_angle: PI32 / 8.0 * 1.2, - range: 300000.0, - ..default() - }, - visibility: Visibility::Hidden, - ..default() - }, - )); - }); - } actor_entity = actor.id(); for ar_asset_name in &state.ar_models {