From 2f65c652c830dc0535f42d96d54a62ea099b5ddc Mon Sep 17 00:00:00 2001 From: hut Date: Fri, 5 Apr 2024 20:38:44 +0200 Subject: [PATCH] better targeting handling --- src/defs.txt | 1 + src/hud.rs | 32 ++++++++++++++++++-------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/defs.txt b/src/defs.txt index 6d78378..26bee1d 100644 --- a/src/defs.txt +++ b/src/defs.txt @@ -7,6 +7,7 @@ actor 0 0 0 jupiter rotationx -0.50 rotationz -0.28 angularmomentum 30 30 30 + clickable no actor 0 593051 0 suit relativeto jupiter diff --git a/src/hud.rs b/src/hud.rs index aeb79b6..95a57a0 100644 --- a/src/hud.rs +++ b/src/hud.rs @@ -598,23 +598,27 @@ fn handle_input( ew_togglemusic.send(audio::ToggleMusicEvent()); } if mouse_input.just_pressed(settings.key_selectobject) { - if q_target.is_empty() { - if let Ok(camtrans) = q_camera.get_single() { - let objects: Vec<(Entity, &Transform)> = q_objects - .iter() - .map(|(entity, transform)| (entity, transform)) - .collect(); - if let (Some(entity), _dist) = camera::find_closest_target::(objects, camtrans) { - commands.entity(entity).insert(IsTargeted); + if let Ok(camtrans) = q_camera.get_single() { + let objects: Vec<(Entity, &Transform)> = q_objects.iter().collect(); + if let (Some(new_target), _dist) = camera::find_closest_target::(objects, camtrans) { + if let Ok(old_target) = q_target.get_single() { + if old_target != new_target { + commands.entity(old_target).remove::(); + commands.entity(new_target).insert(IsTargeted); + ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click)); + } + } + else { + commands.entity(new_target).insert(IsTargeted); + ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click)); + } + } + else { + for entity in &q_target { + commands.entity(entity).remove::(); ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click)); } } } - else { - for entity in &q_target { - commands.entity(entity).remove::(); - ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click)); - } - } } }