This commit is contained in:
yuni 2024-04-05 20:01:44 +02:00
parent 96d1169eb9
commit f13fc7167d

View file

@ -208,65 +208,60 @@ pub fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>,
settings: ResMut<settings::Settings>,
q_talker: Query<(&chat::Talker, &Transform), (Without<actor::Player>, Without<Camera>)>,
mut player: Query<(Entity, &mut Actor, &mut Transform), (With<actor::Player>, Without<Camera>)>,
mut player: Query<(Entity, &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>>,
) {
if q_camera.is_empty() {
if q_camera.is_empty() || player.is_empty() {
return;
}
let camtrans = q_camera.get_single().unwrap();
let (player_entity, player) = player.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() {
let objects: Vec<(chat::Talker, &Transform)> = q_talker
.iter()
.map(|(talker, transform)| (talker.clone(), transform))
.collect();
// TODO: replace Transform.translation with Position
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()});
}
let objects: Vec<(chat::Talker, &Transform)> = q_talker
.iter()
.map(|(talker, transform)| (talker.clone(), transform))
.collect();
// TODO: replace Transform.translation with Position
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()});
}
}
// Entering Vehicles
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) <= MAX_INTERACT_DISTANCE * MAX_INTERACT_DISTANCE {
commands.entity(vehicle_entity).insert(ActorVehicleBeingEntered);
commands.entity(player_entity).insert(ActorEnteringVehicle);
ew_vehicle.send(VehicleEnterExitEvent{
vehicle: vehicle_entity,
driver: player_entity,
is_entering: q_player_drives.is_empty(),
is_player: true,
});
break;
}
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);
commands.entity(player_entity).insert(ActorEnteringVehicle);
ew_vehicle.send(VehicleEnterExitEvent{
vehicle: vehicle_entity,
driver: player_entity,
is_entering: q_player_drives.is_empty(),
is_player: true,
});
break;
}
}
}
}
else if keyboard_input.just_pressed(settings.key_vehicle) {
// Exiting Vehicles
if let Ok((player_entity, _player_actor, _player)) = player.get_single_mut() {
for vehicle_entity in &q_player_drives {
commands.entity(vehicle_entity).insert(ActorVehicleBeingEntered);
commands.entity(player_entity).insert(ActorEnteringVehicle);
ew_vehicle.send(VehicleEnterExitEvent{
vehicle: vehicle_entity,
driver: player_entity,
is_entering: false,
is_player: true,
});
break;
}
for vehicle_entity in &q_player_drives {
commands.entity(vehicle_entity).insert(ActorVehicleBeingEntered);
commands.entity(player_entity).insert(ActorEnteringVehicle);
ew_vehicle.send(VehicleEnterExitEvent{
vehicle: vehicle_entity,
driver: player_entity,
is_entering: false,
is_player: true,
});
break;
}
}
}