From ce8f4666c533546ddb80955e61543d4f72ed0f64 Mon Sep 17 00:00:00 2001 From: hut Date: Sat, 30 Mar 2024 18:48:19 +0100 Subject: [PATCH] trying to fix player "teleportation" on vehicle exiting... --- src/actor.rs | 31 ++++++++++++++++--------------- src/camera.rs | 8 ++++++-- src/defs.txt | 2 +- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/actor.rs b/src/actor.rs index af61e59..39eb845 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -285,6 +285,7 @@ pub fn handle_input( is_entering: false, is_player: true, }); + commands.entity(player_entity).insert(RigidBody::Dynamic); break; } } @@ -295,43 +296,43 @@ pub fn handle_input( pub fn handle_vehicle_enter_exit( mut commands: Commands, mut er_vehicle: EventReader, - mut q_drivers: Query<(Entity, &mut Visibility, &mut Transform), (Without, With)>, - mut q_vehicles: Query<(Entity, &mut Visibility, &mut Transform), (With, Without)>, + mut q_drivers: Query<(Entity, &mut Visibility, &mut Transform, &mut LinearVelocity, &mut AngularVelocity), (Without, With)>, + mut q_vehicles: Query<(Entity, &mut Visibility, &mut Transform, &LinearVelocity, &AngularVelocity), (With, Without)>, mut ew_sfx: EventWriter, ) { for event in er_vehicle.read() { - for (driver, mut driver_vis, mut driver_trans) in q_drivers.iter_mut() { + for (driver, mut driver_vis, mut driver_trans, mut driver_linv, mut driver_angv) in q_drivers.iter_mut() { if driver == event.driver { - for (vehicle, mut vehicle_vis, vehicle_trans) in q_vehicles.iter_mut() { + for (vehicle, mut vehicle_vis, vehicle_trans, vehicle_linv, vehicle_angv) in q_vehicles.iter_mut() { if vehicle == event.vehicle { if event.is_entering { // Entering Vehicle - *driver_vis = Visibility::Hidden; + commands.entity(driver).remove::(); if event.is_player { ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::EnterVehicle)); - //*vehicle_vis = Visibility::Hidden; commands.entity(driver).remove::(); commands.entity(vehicle).insert(PlayerCamera); commands.entity(vehicle).insert(PlayerDrivesThis); - commands.entity(driver).remove::(); } } else { // Exiting Vehicle + *driver_linv = vehicle_linv.clone(); + *driver_angv = vehicle_angv.clone(); + driver_trans.translation = vehicle_trans.translation; + driver_trans.rotation = vehicle_trans.rotation; + // NOTE: I would rather have the following line here, + // but then, for some reason, changing driver translation + // does not work. For now, you must manually insert the RigidBody + // component from the place that adds this event. + //commands.entity(driver).insert(RigidBody::Dynamic); if event.is_player { ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Switch)); - *vehicle_vis = Visibility::Inherited; - // TODO: set actor momentum to vehicle momentum (linear/angular) - commands.entity(driver).insert(RigidBody::Dynamic); - commands.entity(vehicle).remove::(); commands.entity(driver).insert(PlayerCamera); commands.entity(vehicle).remove::(); + *vehicle_vis = Visibility::Inherited; } - else { - *driver_trans = vehicle_trans.clone(); - } - driver_trans.translation = vehicle_trans.translation.clone(); } } } diff --git a/src/camera.rs b/src/camera.rs index 55f5b1f..d92f4dc 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -4,8 +4,7 @@ use bevy::window::PrimaryWindow; use bevy::core_pipeline::bloom::{BloomCompositeMode, BloomSettings}; use bevy::core_pipeline::tonemapping::Tonemapping; use bevy_xpbd_3d::prelude::*; -use bevy_xpbd_3d::PhysicsSchedule; -use bevy_xpbd_3d::PhysicsStepSet; +use bevy_xpbd_3d::{PhysicsSchedule, PhysicsStepSet}; use std::f32::consts::*; use crate::{settings, audio, actor}; @@ -92,8 +91,10 @@ pub fn handle_input( } fn manage_player_actor( + mut commands: Commands, settings: Res, mut q_playercam: Query<&mut Visibility, With>, + mut q_hiddenplayer: Query<&mut Visibility, (With, Without)>, ) { for mut vis in &mut q_playercam { if settings.third_person { @@ -103,6 +104,9 @@ fn manage_player_actor( *vis = Visibility::Hidden; } } + for mut vis in &mut q_hiddenplayer { + *vis = Visibility::Hidden; + } } #[allow(clippy::too_many_arguments)] diff --git a/src/defs.txt b/src/defs.txt index 39fb507..e27f9ea 100644 --- a/src/defs.txt +++ b/src/defs.txt @@ -112,7 +112,7 @@ actor 10 -30 20 MeteorAceGT vehicle yes thrust 70 13.7 9.4 0.5 20 engine ion - collider sphere 2 + collider sphere 1.5 camdistance 50 mass 500