smoother handling of vehicle entrance/exits, also fixes adrenaline

This commit is contained in:
yuni 2024-03-30 22:31:07 +01:00
parent 97640c383c
commit 9975d8b2bf
3 changed files with 42 additions and 33 deletions

View file

@ -141,7 +141,12 @@ impl Default for LifeForm { fn default() -> Self { Self {
}}} }}}
#[derive(Component)] #[derive(Component)]
pub struct Vehicle; pub struct Vehicle {
stored_drivers_collider: Option<Collider>,
}
impl Default for Vehicle { fn default() -> Self { Self {
stored_drivers_collider: None,
}}}
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq)]
pub enum EngineType { pub enum EngineType {
@ -194,17 +199,17 @@ const SUIT_SIMPLE: Suit = Suit {
pub fn update_physics_lifeforms( pub fn update_physics_lifeforms(
time: Res<Time>, time: Res<Time>,
mut query: Query<(&mut LifeForm, &mut Suit, &Actor)>, mut query: Query<(&mut LifeForm, &mut Suit, &LinearVelocity)>,
) { ) {
let d = time.delta_seconds(); let d = time.delta_seconds();
for (mut lifeform, mut suit, actor) in query.iter_mut() { for (mut lifeform, mut suit, velocity) in query.iter_mut() {
if lifeform.adrenaline_jolt.abs() > 1e-3 { if lifeform.adrenaline_jolt.abs() > 1e-3 {
lifeform.adrenaline_jolt *= 0.99; lifeform.adrenaline_jolt *= 0.99;
} }
else { else {
lifeform.adrenaline_jolt = 0.0 lifeform.adrenaline_jolt = 0.0
} }
let speed = actor.v.length(); let speed = velocity.length();
if speed > 1000.0 { if speed > 1000.0 {
lifeform.adrenaline += 0.001; lifeform.adrenaline += 0.001;
} }
@ -286,7 +291,6 @@ pub fn handle_input(
is_entering: false, is_entering: false,
is_player: true, is_player: true,
}); });
commands.entity(player_entity).insert(RigidBody::Dynamic);
break; break;
} }
} }
@ -297,38 +301,37 @@ pub fn handle_input(
pub fn handle_vehicle_enter_exit( pub fn handle_vehicle_enter_exit(
mut commands: Commands, mut commands: Commands,
mut er_vehicle: EventReader<VehicleEnterExitEvent>, mut er_vehicle: EventReader<VehicleEnterExitEvent>,
mut q_drivers: Query<(Entity, &mut Visibility, &mut Transform, &mut LinearVelocity, &mut AngularVelocity), (Without<ActorVehicleBeingEntered>, With<ActorEnteringVehicle>)>, mut q_drivers: Query<(Entity, &mut Visibility, Option<&Collider>), (Without<ActorVehicleBeingEntered>, With<ActorEnteringVehicle>)>,
mut q_vehicles: Query<(Entity, &mut Visibility, &mut Transform, &LinearVelocity, &AngularVelocity), (With<ActorVehicleBeingEntered>, Without<ActorEnteringVehicle>)>, mut q_vehicles: Query<(Entity, &mut Vehicle, &mut Visibility), (With<ActorVehicleBeingEntered>, Without<ActorEnteringVehicle>)>,
mut ew_sfx: EventWriter<audio::PlaySfxEvent>, mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
) { ) {
for event in er_vehicle.read() { for event in er_vehicle.read() {
for (driver, mut driver_vis, mut driver_trans, mut driver_linv, mut driver_angv) in q_drivers.iter_mut() { for (driver, mut driver_vis, driver_collider) in q_drivers.iter_mut() {
if driver == event.driver { if driver == event.driver {
for (vehicle, mut vehicle_vis, vehicle_trans, vehicle_linv, vehicle_angv) in q_vehicles.iter_mut() { for (vehicle, mut vehicle_component, mut vehicle_vis) in q_vehicles.iter_mut() {
if !event.is_player {
continue;
}
if vehicle == event.vehicle { if vehicle == event.vehicle {
if event.is_entering { if event.is_entering {
// Entering Vehicle // Entering Vehicle
if let Some(collider) = driver_collider {
vehicle_component.stored_drivers_collider = Some(collider.clone());
}
commands.entity(driver).remove::<RigidBody>(); commands.entity(driver).remove::<RigidBody>();
*driver_vis = Visibility::Hidden; //seems to have no effect... *driver_vis = Visibility::Hidden; //seems to have no effect...
if event.is_player {
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(vehicle).insert(PlayerCamera); commands.entity(vehicle).insert(PlayerCamera);
commands.entity(vehicle).insert(PlayerDrivesThis); commands.entity(vehicle).insert(PlayerDrivesThis);
} }
}
else { else {
// Exiting Vehicle // Exiting Vehicle
*driver_linv = vehicle_linv.clone(); if let Some(collider) = &vehicle_component.stored_drivers_collider {
*driver_angv = vehicle_angv.clone(); commands.entity(driver).insert(collider.clone());
driver_trans.translation = vehicle_trans.translation + Vec3::new(0.0, 0.0, 10.0); }
driver_trans.rotation = vehicle_trans.rotation; commands.entity(driver).insert(RigidBody::Dynamic);
// 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)); ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Switch));
commands.entity(vehicle).remove::<PlayerCamera>(); commands.entity(vehicle).remove::<PlayerCamera>();
commands.entity(driver).insert(PlayerCamera); commands.entity(driver).insert(PlayerCamera);
@ -341,7 +344,6 @@ pub fn handle_vehicle_enter_exit(
} }
} }
} }
}
pub fn handle_new_conversations( pub fn handle_new_conversations(
mut commands: Commands, mut commands: Commands,

View file

@ -97,7 +97,8 @@ pub fn handle_input(
fn manage_player_actor( fn manage_player_actor(
settings: Res<settings::Settings>, settings: Res<settings::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, (With<actor::Player>, Without<actor::PlayerCamera>)>, mut q_hiddenplayer: Query<(&mut Visibility, &mut Transform, &mut LinearVelocity, &mut AngularVelocity), (With<actor::Player>, Without<actor::PlayerCamera>)>,
q_ride: Query<(&Transform, &LinearVelocity, &AngularVelocity), (With<actor::PlayerDrivesThis>, Without<actor::Player>)>,
) { ) {
for mut vis in &mut q_playercam { for mut vis in &mut q_playercam {
if settings.third_person { if settings.third_person {
@ -107,8 +108,14 @@ fn manage_player_actor(
*vis = Visibility::Hidden; *vis = Visibility::Hidden;
} }
} }
for mut vis in &mut q_hiddenplayer { for (mut vis, mut trans, mut v, mut angv) in &mut q_hiddenplayer {
*vis = Visibility::Hidden; *vis = Visibility::Hidden;
if let Ok((ride_trans, ride_v, ride_angv)) = q_ride.get_single() {
trans.translation = ride_trans.translation + Vec3::new(0.0, 0.0, 10.0);
trans.rotation = ride_trans.rotation;
*v = ride_v.clone();
*angv = ride_angv.clone();
}
} }
} }

View file

@ -369,7 +369,7 @@ impl ParserState {
}); });
} }
if self.is_vehicle { if self.is_vehicle {
actor.insert(actor::Vehicle); actor.insert(actor::Vehicle::default());
} }
if self.is_vehicle || self.is_suited { if self.is_vehicle || self.is_suited {
actor.insert(actor::Engine { actor.insert(actor::Engine {