From f13fc7167d7bfb7c549e71d77627427c98cacca0 Mon Sep 17 00:00:00 2001 From: hut Date: Fri, 5 Apr 2024 20:01:44 +0200 Subject: [PATCH] cleanup --- src/actor.rs | 69 ++++++++++++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 37 deletions(-) diff --git a/src/actor.rs b/src/actor.rs index 5a7edac..5454f73 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -208,65 +208,60 @@ pub fn handle_input( keyboard_input: Res>, settings: ResMut, q_talker: Query<(&chat::Talker, &Transform), (Without, Without)>, - mut player: Query<(Entity, &mut Actor, &mut Transform), (With, Without)>, + mut player: Query<(Entity, &mut Transform), (With, Without)>, q_camera: Query<&Transform, With>, mut q_vehicles: Query<(Entity, &mut Visibility, &Transform), (With, Without, Without)>, mut ew_conv: EventWriter, mut ew_vehicle: EventWriter, q_player_drives: Query>, ) { - 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::(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::(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; } } }