make stars clickable

This commit is contained in:
yuni 2024-04-08 00:39:57 +02:00
parent 36859f6bb6
commit 81187b4fe6
3 changed files with 47 additions and 17 deletions

View file

@ -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));

View file

@ -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<String>,
pub distance: Option<f64>,
}
impl Default for IsClickable { fn default() -> Self { Self {
name: None,
distance: None,
}}}
#[derive(Resource)]
pub struct Log {
logs: VecDeque<Message>,
@ -472,7 +481,7 @@ fn update_hud(
mut query_chat: Query<&mut Text, (With<ChatText>, Without<GaugesText>)>,
query_all_actors: Query<&actor::Actor>,
settings: Res<settings::Settings>,
q_target: Query<(&Position, &actor::Actor), With<IsTargeted>>,
q_target: Query<(Option<&Position>, &IsClickable), With<IsTargeted>>,
) {
// 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<DVec3>;
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<DVec3>;
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<String> = q_target
.iter()
.map(|(_pos, act)| act.name.clone().unwrap_or("<unnamed>".to_string()))
.map(|(_, clickable)| clickable.name.clone().unwrap_or("<unnamed>".to_string()))
.collect();
text.sections[21].value = targets.join(", ");
}

View file

@ -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,