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::scene::SceneInstance;
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;
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;
pub struct ActorPlugin;
@ -206,22 +207,30 @@ pub fn handle_input(
mut commands: Commands,
keyboard_input: Res<ButtonInput<KeyCode>>,
settings: ResMut<settings::Settings>,
q_talker: Query<(&chat::Talker, &Transform), Without<actor::Player>>,
mut player: Query<(Entity, &mut Actor, &mut Transform), With<actor::Player>>,
mut q_vehicles: Query<(Entity, &mut Visibility, &Transform), (With<actor::Vehicle>, 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>, Without<Camera>)>,
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_vehicle: EventWriter<VehicleEnterExitEvent>,
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) {
// Talking to people
if let Ok((_player_entity, _player_actor, player)) = player.get_single() {
for (talker, transform) in &q_talker {
if let Ok((_player_entity, _player_actor, _player)) = player.get_single() {
let objects: Vec<(chat::Talker, &Transform)> = q_talker
.iter()
.map(|(talker, transform)| (talker.clone(), transform))
.collect();
// 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()});
break;
}
}
}
@ -229,7 +238,7 @@ pub fn handle_input(
if q_player_drives.is_empty() {
if let Ok((player_entity, _player_actor, player)) = player.get_single_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(player_entity).insert(ActorEnteringVehicle);
ew_vehicle.send(VehicleEnterExitEvent{

View file

@ -8,7 +8,7 @@ use bevy::transform::TransformSystem;
use bevy::math::{DVec3, DQuat};
use bevy_xpbd_3d::prelude::*;
use std::f32::consts::PI;
use crate::{actor, audio, hud, settings};
use crate::{actor, audio, settings};
pub struct CameraPlugin;
@ -340,6 +340,7 @@ pub fn find_closest_target<TargetSpecifier>(
objects: Vec<(TargetSpecifier, &Transform)>,
camera_transform: &Transform,
) -> (Option<TargetSpecifier>, f32)
where TargetSpecifier: Clone
{
let mut closest_entity: Option<TargetSpecifier> = None;
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);
if distance < closest_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));
if q_target.is_empty() {
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) {
commands.entity(entity).insert(IsTargeted);
}