chat initiation now finds people you look at, not anyone nearby

This commit is contained in:
yuni 2024-04-05 19:57:55 +02:00
parent 4512b63681
commit df541e85a9
3 changed files with 28 additions and 15 deletions

View file

@ -2,10 +2,11 @@ use bevy::prelude::*;
use bevy_xpbd_3d::prelude::*; use bevy_xpbd_3d::prelude::*;
use bevy::scene::SceneInstance; use bevy::scene::SceneInstance;
use bevy::math::DVec3; use bevy::math::DVec3;
use crate::{actor, audio, chat, commands, effects, hud, nature, settings, world}; use crate::{actor, audio, camera, chat, commands, effects, hud, nature, settings, world};
pub const ENGINE_SPEED_FACTOR: f32 = 30.0; pub const ENGINE_SPEED_FACTOR: f32 = 30.0;
const MIN_INTERACT_DISTANCE: f32 = 30.0; const MAX_TRANSMISSION_DISTANCE: f32 = 60.0;
const MAX_INTERACT_DISTANCE: f32 = 30.0;
const NO_RIDE: u32 = 0; const NO_RIDE: u32 = 0;
pub struct ActorPlugin; pub struct ActorPlugin;
@ -206,22 +207,30 @@ pub fn handle_input(
mut commands: Commands, mut commands: Commands,
keyboard_input: Res<ButtonInput<KeyCode>>, keyboard_input: Res<ButtonInput<KeyCode>>,
settings: ResMut<settings::Settings>, settings: ResMut<settings::Settings>,
q_talker: Query<(&chat::Talker, &Transform), Without<actor::Player>>, q_talker: Query<(&chat::Talker, &Transform), (Without<actor::Player>, Without<Camera>)>,
mut player: Query<(Entity, &mut Actor, &mut Transform), With<actor::Player>>, mut player: Query<(Entity, &mut Actor, &mut Transform), (With<actor::Player>, Without<Camera>)>,
mut q_vehicles: Query<(Entity, &mut Visibility, &Transform), (With<actor::Vehicle>, Without<actor::Player>)>, q_camera: Query<&Transform, With<Camera>>,
mut q_vehicles: Query<(Entity, &mut Visibility, &Transform), (With<actor::Vehicle>, Without<actor::Player>, Without<Camera>)>,
mut ew_conv: EventWriter<chat::StartConversationEvent>, mut ew_conv: EventWriter<chat::StartConversationEvent>,
mut ew_vehicle: EventWriter<VehicleEnterExitEvent>, mut ew_vehicle: EventWriter<VehicleEnterExitEvent>,
q_player_drives: Query<Entity, With<PlayerDrivesThis>>, q_player_drives: Query<Entity, With<PlayerDrivesThis>>,
) { ) {
let mindist = MIN_INTERACT_DISTANCE * MIN_INTERACT_DISTANCE; if q_camera.is_empty() {
return;
}
let camtrans = q_camera.get_single().unwrap();
if keyboard_input.just_pressed(settings.key_interact) { if keyboard_input.just_pressed(settings.key_interact) {
// Talking to people // Talking to people
if let Ok((_player_entity, _player_actor, player)) = player.get_single() { if let Ok((_player_entity, _player_actor, _player)) = player.get_single() {
for (talker, transform) in &q_talker { let objects: Vec<(chat::Talker, &Transform)> = q_talker
.iter()
.map(|(talker, transform)| (talker.clone(), transform))
.collect();
// TODO: replace Transform.translation with Position // TODO: replace Transform.translation with Position
if transform.translation.distance_squared(player.translation) <= mindist { if let (Some(talker), dist) = camera::find_closest_target::<chat::Talker>(objects, camtrans) {
if dist <= MAX_TRANSMISSION_DISTANCE {
ew_conv.send(chat::StartConversationEvent{talker: talker.clone()}); ew_conv.send(chat::StartConversationEvent{talker: talker.clone()});
break;
} }
} }
} }
@ -229,7 +238,7 @@ pub fn handle_input(
if q_player_drives.is_empty() { if q_player_drives.is_empty() {
if let Ok((player_entity, _player_actor, player)) = player.get_single_mut() { if let Ok((player_entity, _player_actor, player)) = player.get_single_mut() {
for (vehicle_entity, _visibility, vehicle_transform) in q_vehicles.iter_mut() { for (vehicle_entity, _visibility, vehicle_transform) in q_vehicles.iter_mut() {
if vehicle_transform.translation.distance_squared(player.translation) <= mindist { if vehicle_transform.translation.distance_squared(player.translation) <= MAX_INTERACT_DISTANCE * MAX_INTERACT_DISTANCE {
commands.entity(vehicle_entity).insert(ActorVehicleBeingEntered); commands.entity(vehicle_entity).insert(ActorVehicleBeingEntered);
commands.entity(player_entity).insert(ActorEnteringVehicle); commands.entity(player_entity).insert(ActorEnteringVehicle);
ew_vehicle.send(VehicleEnterExitEvent{ ew_vehicle.send(VehicleEnterExitEvent{

View file

@ -8,7 +8,7 @@ use bevy::transform::TransformSystem;
use bevy::math::{DVec3, DQuat}; use bevy::math::{DVec3, DQuat};
use bevy_xpbd_3d::prelude::*; use bevy_xpbd_3d::prelude::*;
use std::f32::consts::PI; use std::f32::consts::PI;
use crate::{actor, audio, hud, settings}; use crate::{actor, audio, settings};
pub struct CameraPlugin; pub struct CameraPlugin;
@ -340,6 +340,7 @@ pub fn find_closest_target<TargetSpecifier>(
objects: Vec<(TargetSpecifier, &Transform)>, objects: Vec<(TargetSpecifier, &Transform)>,
camera_transform: &Transform, camera_transform: &Transform,
) -> (Option<TargetSpecifier>, f32) ) -> (Option<TargetSpecifier>, f32)
where TargetSpecifier: Clone
{ {
let mut closest_entity: Option<TargetSpecifier> = None; let mut closest_entity: Option<TargetSpecifier> = None;
let mut closest_distance: f32 = f32::MAX; let mut closest_distance: f32 = f32::MAX;
@ -360,7 +361,7 @@ pub fn find_closest_target<TargetSpecifier>(
let distance = trans.translation.distance(camera_transform.translation); let distance = trans.translation.distance(camera_transform.translation);
if distance < closest_distance { if distance < closest_distance {
closest_distance = distance; closest_distance = distance;
closest_entity = Some(entity); closest_entity = Some(entity.clone());
} }
} }
} }

View file

@ -601,7 +601,10 @@ fn handle_input(
//ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Switch)); //ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Switch));
if q_target.is_empty() { if q_target.is_empty() {
if let Ok(camtrans) = q_camera.get_single() { if let Ok(camtrans) = q_camera.get_single() {
let objects: Vec<(Entity, &Transform)> = q_objects.iter().collect(); let objects: Vec<(Entity, &Transform)> = q_objects
.iter()
.map(|(entity, transform)| (entity, transform))
.collect();
if let (Some(entity), _dist) = camera::find_closest_target::<Entity>(objects, camtrans) { if let (Some(entity), _dist) = camera::find_closest_target::<Entity>(objects, camtrans) {
commands.entity(entity).insert(IsTargeted); commands.entity(entity).insert(IsTargeted);
} }