From 845e9384726b862861602ddff1026574e11286a5 Mon Sep 17 00:00:00 2001 From: hut Date: Fri, 5 Apr 2024 20:05:58 +0200 Subject: [PATCH] entering vehicles now also based on camera orientation --- src/actor.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/actor.rs b/src/actor.rs index 5454f73..28c753e 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -208,9 +208,9 @@ pub fn handle_input( keyboard_input: Res>, settings: ResMut, q_talker: Query<(&chat::Talker, &Transform), (Without, Without)>, - mut player: Query<(Entity, &mut Transform), (With, Without)>, + player: Query>, q_camera: Query<&Transform, With>, - mut q_vehicles: Query<(Entity, &mut Visibility, &Transform), (With, Without, Without)>, + q_vehicles: Query<(Entity, &Transform), (With, Without, Without)>, mut ew_conv: EventWriter, mut ew_vehicle: EventWriter, q_player_drives: Query>, @@ -219,7 +219,7 @@ pub fn handle_input( return; } let camtrans = q_camera.get_single().unwrap(); - let (player_entity, player) = player.get_single().unwrap(); + let player_entity = player.get_single().unwrap(); if keyboard_input.just_pressed(settings.key_interact) { // Talking to people @@ -235,17 +235,19 @@ pub fn handle_input( } // Entering Vehicles if q_player_drives.is_empty() { - for (vehicle_entity, _visibility, vehicle_transform) in q_vehicles.iter_mut() { - if vehicle_transform.translation.distance_squared(player.translation) <= MAX_INTERACT_DISTANCE * MAX_INTERACT_DISTANCE { - commands.entity(vehicle_entity).insert(ActorVehicleBeingEntered); + let objects: Vec<(Entity, &Transform)> = q_vehicles + .iter() + .collect(); + if let (Some(entity), dist) = camera::find_closest_target::(objects, camtrans) { + if dist <= MAX_INTERACT_DISTANCE { + commands.entity(entity).insert(ActorVehicleBeingEntered); commands.entity(player_entity).insert(ActorEnteringVehicle); ew_vehicle.send(VehicleEnterExitEvent{ - vehicle: vehicle_entity, + vehicle: entity, driver: player_entity, is_entering: q_player_drives.is_empty(), is_player: true, }); - break; } } }