From 81187b4fe6dbb45ca6114d64b2939b97e0decf2e Mon Sep 17 00:00:00 2001 From: hut Date: Mon, 8 Apr 2024 00:39:57 +0200 Subject: [PATCH] make stars clickable --- src/commands.rs | 5 ++++- src/hud.rs | 53 +++++++++++++++++++++++++++++++++++-------------- src/world.rs | 6 +++++- 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index 09d48d8..8f19eaa 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -660,7 +660,10 @@ fn spawn_entities( }); } if state.is_clickable { - actor.insert(hud::IsClickable); + actor.insert(hud::IsClickable { + name: state.name.clone(), + ..default() + }); } if let Some(value) = state.wants_maxrotation { actor.insert(actor::WantsMaxRotation(value)); diff --git a/src/hud.rs b/src/hud.rs index 180d73d..15ef3a1 100644 --- a/src/hud.rs +++ b/src/hud.rs @@ -50,7 +50,6 @@ impl Plugin for HudPlugin { #[derive(Component)] struct ToggleableHudElement; #[derive(Component)] struct OnlyHideWhenTogglingHud; #[derive(Component)] struct Selectagon; -#[derive(Component)] pub struct IsClickable; #[derive(Component)] pub struct IsTargeted; #[derive(Resource)] @@ -73,6 +72,16 @@ struct Message { time: u64, } +#[derive(Component)] +pub struct IsClickable { + pub name: Option, + pub distance: Option, +} +impl Default for IsClickable { fn default() -> Self { Self { + name: None, + distance: None, +}}} + #[derive(Resource)] pub struct Log { logs: VecDeque, @@ -472,7 +481,7 @@ fn update_hud( mut query_chat: Query<&mut Text, (With, Without)>, query_all_actors: Query<&actor::Actor>, settings: Res, - q_target: Query<(&Position, &actor::Actor), With>, + q_target: Query<(Option<&Position>, &IsClickable), With>, ) { // TODO only when hud is actually on if timer.0.tick(time.delta()).just_finished() || log.needs_rerendering { @@ -516,24 +525,38 @@ fn update_hud( text.sections[19].value = format!("{speed:.0}m/s | {kmh:.0}km/h"); // Target display - let target: Option; - if let Ok((targetpos, _actr)) = q_target.get_single() { - target = Some(targetpos.0); - } - else if q_target.is_empty() { - target = Some(DVec3::new(0.0, 0.0, 0.0)); + let (x, y, z, dist_scalar) : (f64, f64, f64, f64); + if let Ok((_, IsClickable { distance: Some(dist), .. })) = q_target.get_single() { + dist_scalar = *dist; + (x, y, z) = (0.0, 0.0, 0.0); } else { - target = None; + let target: Option; + if let Ok((Some(targetpos), _)) = q_target.get_single() { + target = Some(targetpos.0); + } + else if q_target.is_empty() { + target = Some(DVec3::new(0.0, 0.0, 0.0)); + } + else { + target = None; + } + if let Some(target_pos) = target { + let dist = pos.0 - target_pos; + (x, y, z) = (dist.x, dist.y, dist.z); + dist_scalar = dist.length(); + } + else { + (x, y, z) = (0.0, 0.0, 0.0); + dist_scalar = 0.0; + } } - if let Some(target_pos) = target { - let dist = pos.0 - target_pos; - let (x, y, z) = (dist.x, dist.y, dist.z); - let dist_scalar = dist.length(); + + if dist_scalar != 0.0 { text.sections[7].value = format!("{x:.0}m / {z:.0}m / {y:.0}m / distance: {dist_scalar:.0}m"); } else { - text.sections[7].value = format!("ERROR: MULTIPLE TARGETS"); + text.sections[7].value = format!("TARGET ERROR"); } if q_target.is_empty() { text.sections[21].value = "Jupiter".to_string(); @@ -541,7 +564,7 @@ fn update_hud( else { let targets: Vec = q_target .iter() - .map(|(_pos, act)| act.name.clone().unwrap_or("".to_string())) + .map(|(_, clickable)| clickable.name.clone().unwrap_or("".to_string())) .collect(); text.sections[21].value = targets.join(", "); } diff --git a/src/world.rs b/src/world.rs index a16c7af..ad78475 100644 --- a/src/world.rs +++ b/src/world.rs @@ -1,4 +1,4 @@ -use crate::{actor, nature, settings}; +use crate::{actor, hud, nature, settings}; use bevy::prelude::*; use bevy::render::render_resource::{AsBindGroup, ShaderRef}; use bevy::math::{DVec3, I64Vec3}; @@ -151,6 +151,10 @@ pub fn setup( let dist = 1e9; commands.spawn(( Star, + hud::IsClickable { + name: Some("Star".to_string()), + distance: Some(10000000000.0), + }, PbrBundle { mesh: sphere_handle.clone(), material: star_color_handle,