fix dying from entering vehicles

This commit is contained in:
yuni 2024-04-17 13:55:39 +02:00
parent c9e271184c
commit 919b801832
2 changed files with 12 additions and 2 deletions

View file

@ -110,6 +110,7 @@ impl Default for ExperiencesGForce { fn default() -> Self { Self {
#[derive(Component)] pub struct Player; // Attached to the suit of the player #[derive(Component)] pub struct Player; // Attached to the suit of the player
#[derive(Component)] pub struct PlayerDrivesThis; // Attached to the entered vehicle #[derive(Component)] pub struct PlayerDrivesThis; // Attached to the entered vehicle
#[derive(Component)] pub struct PlayerCamera; // Attached to the actor to use as point of view #[derive(Component)] pub struct PlayerCamera; // Attached to the actor to use as point of view
#[derive(Component)] pub struct JustNowEnteredVehicle;
#[derive(Component)] pub struct ActorEnteringVehicle; #[derive(Component)] pub struct ActorEnteringVehicle;
#[derive(Component)] pub struct ActorVehicleBeingEntered; #[derive(Component)] pub struct ActorVehicleBeingEntered;
#[derive(Component)] pub struct WantsMaxRotation(pub f64); #[derive(Component)] pub struct WantsMaxRotation(pub f64);
@ -326,6 +327,7 @@ pub fn handle_vehicle_enter_exit(
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::EnterVehicle)); ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::EnterVehicle));
commands.entity(driver).remove::<PlayerCamera>(); commands.entity(driver).remove::<PlayerCamera>();
commands.entity(driver).remove::<Collider>(); commands.entity(driver).remove::<Collider>();
commands.entity(driver).insert(JustNowEnteredVehicle);
commands.entity(vehicle).insert(PlayerCamera); commands.entity(vehicle).insert(PlayerCamera);
commands.entity(vehicle).insert(PlayerDrivesThis); commands.entity(vehicle).insert(PlayerDrivesThis);
} }

View file

@ -129,9 +129,10 @@ pub fn handle_input(
} }
fn manage_player_actor( fn manage_player_actor(
mut commands: Commands,
settings: Res<var::Settings>, settings: Res<var::Settings>,
mut q_playercam: Query<&mut Visibility, With<actor::PlayerCamera>>, mut q_playercam: Query<&mut Visibility, With<actor::PlayerCamera>>,
mut q_hiddenplayer: Query<(&mut Visibility, &mut Position, &mut Rotation, &mut LinearVelocity, &mut AngularVelocity), (With<actor::Player>, Without<actor::PlayerCamera>)>, mut q_hiddenplayer: Query<(Entity, &mut Visibility, &mut Position, &mut Rotation, &mut LinearVelocity, &mut AngularVelocity, Option<&mut actor::ExperiencesGForce>, Option<&actor::JustNowEnteredVehicle>), (With<actor::Player>, Without<actor::PlayerCamera>)>,
q_ride: Query<(&Transform, &Position, &Rotation, &LinearVelocity, &AngularVelocity), (With<actor::PlayerDrivesThis>, Without<actor::Player>)>, q_ride: Query<(&Transform, &Position, &Rotation, &LinearVelocity, &AngularVelocity), (With<actor::PlayerDrivesThis>, Without<actor::Player>)>,
) { ) {
for mut vis in &mut q_playercam { for mut vis in &mut q_playercam {
@ -142,7 +143,7 @@ fn manage_player_actor(
*vis = Visibility::Hidden; *vis = Visibility::Hidden;
} }
} }
for (mut vis, mut pos, mut rot, mut v, mut angv) in &mut q_hiddenplayer { for (entity, mut vis, mut pos, mut rot, mut v, mut angv, mut gforce, entering) in &mut q_hiddenplayer {
// If we are riding a vehicle, place the player at the position where // If we are riding a vehicle, place the player at the position where
// it would be after exiting the vehicle. // it would be after exiting the vehicle.
// I would rather place it in the center of the vehicle, but at the time // I would rather place it in the center of the vehicle, but at the time
@ -154,6 +155,13 @@ fn manage_player_actor(
rot.0 = ride_rot.0 * DQuat::from_array([-1.0, 0.0, 0.0, 0.0]); rot.0 = ride_rot.0 * DQuat::from_array([-1.0, 0.0, 0.0, 0.0]);
*v = ride_v.clone(); *v = ride_v.clone();
*angv = ride_angv.clone(); *angv = ride_angv.clone();
// I really don't want people to die from the g-forces of entering
// vehicles at high relative speed, even though they probably should.
if let (Some(gforce), Some(_)) = (&mut gforce, entering) {
gforce.last_linear_velocity = v.0;
commands.entity(entity).remove::<actor::JustNowEnteredVehicle>();
}
} }
} }
} }