2024-04-14 12:55:00 +00:00
|
|
|
use crate::{actor, audio, camera, chat, nature, var, world};
|
2024-03-17 14:23:22 +00:00
|
|
|
use bevy::prelude::*;
|
|
|
|
use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin};
|
2024-04-05 20:16:01 +00:00
|
|
|
use bevy::transform::TransformSystem;
|
2024-03-30 16:19:11 +00:00
|
|
|
use bevy_xpbd_3d::prelude::*;
|
2024-04-05 16:14:12 +00:00
|
|
|
use bevy::math::DVec3;
|
2024-03-17 23:36:56 +00:00
|
|
|
use std::collections::VecDeque;
|
2024-03-18 00:02:17 +00:00
|
|
|
use std::time::SystemTime;
|
2024-03-17 14:23:22 +00:00
|
|
|
|
2024-04-08 00:26:23 +00:00
|
|
|
pub const HUD_REFRESH_TIME: f32 = 0.1;
|
2024-03-22 13:35:07 +00:00
|
|
|
pub const FONT: &str = "fonts/Yupiter-Regular.ttf";
|
2024-04-15 01:57:21 +00:00
|
|
|
pub const LOG_MAX: usize = 4;
|
|
|
|
pub const LOG_MAX_TIME_S: f64 = 15.0;
|
|
|
|
pub const LOG_MAX_ROWS: usize = 30;
|
2024-03-20 05:55:54 +00:00
|
|
|
pub const AMBIENT_LIGHT: f32 = 0.0; // Space is DARK
|
2024-03-21 03:34:09 +00:00
|
|
|
pub const AMBIENT_LIGHT_AR: f32 = 15.0;
|
2024-03-20 17:54:22 +00:00
|
|
|
//pub const REPLY_NUMBERS: [char; 10] = ['❶', '❷', '❸', '❹', '❺', '❻', '❼', '❽', '❾', '⓿'];
|
2024-03-22 13:38:28 +00:00
|
|
|
//pub const REPLY_NUMBERS: [char; 10] = ['①', '②', '③', '④', '⑤', '⑥', '⑦', '⑧', '⑨', '⑩'];
|
|
|
|
pub const REPLY_NUMBERS: [char; 10] = ['➀', '➁', '➂', '➃', '➄', '➅', '➆', '➇', '➈', '➉'];
|
2024-03-17 20:57:30 +00:00
|
|
|
|
2024-03-17 22:49:50 +00:00
|
|
|
pub struct HudPlugin;
|
|
|
|
impl Plugin for HudPlugin {
|
2024-03-17 14:23:22 +00:00
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
app.add_systems(Startup, setup);
|
2024-04-05 19:27:19 +00:00
|
|
|
app.add_systems(Update, (
|
2024-04-07 18:02:31 +00:00
|
|
|
update_hud,
|
2024-04-10 19:03:30 +00:00
|
|
|
update_ar_overlays,
|
2024-04-05 19:27:19 +00:00
|
|
|
handle_input,
|
|
|
|
handle_target_event,
|
2024-04-05 20:16:01 +00:00
|
|
|
));
|
|
|
|
app.add_systems(PostUpdate, (
|
|
|
|
update_target_selectagon
|
|
|
|
.after(PhysicsSet::Sync)
|
|
|
|
.after(camera::apply_input_to_player)
|
|
|
|
.before(TransformSystem::TransformPropagate),
|
2024-04-05 19:27:19 +00:00
|
|
|
));
|
2024-04-10 19:03:30 +00:00
|
|
|
app.insert_resource(AugmentedRealityState {
|
|
|
|
overlays_visible: false,
|
|
|
|
});
|
2024-03-19 02:18:16 +00:00
|
|
|
app.insert_resource(Log {
|
|
|
|
logs: VecDeque::with_capacity(LOG_MAX),
|
|
|
|
needs_rerendering: true,
|
|
|
|
});
|
2024-03-17 20:57:30 +00:00
|
|
|
app.insert_resource(FPSUpdateTimer(
|
|
|
|
Timer::from_seconds(HUD_REFRESH_TIME, TimerMode::Repeating)));
|
2024-04-05 19:27:19 +00:00
|
|
|
app.add_event::<TargetEvent>();
|
2024-03-17 14:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 19:27:19 +00:00
|
|
|
#[derive(Event)] pub struct TargetEvent(pub Option<Entity>);
|
2024-03-19 00:24:27 +00:00
|
|
|
#[derive(Component)] struct GaugesText;
|
|
|
|
#[derive(Component)] struct ChatText;
|
2024-03-28 19:47:18 +00:00
|
|
|
#[derive(Component)] struct Reticule;
|
|
|
|
#[derive(Component)] struct ToggleableHudElement;
|
2024-04-05 20:43:14 +00:00
|
|
|
#[derive(Component)] struct OnlyHideWhenTogglingHud;
|
2024-04-05 20:16:01 +00:00
|
|
|
#[derive(Component)] struct Selectagon;
|
2024-04-05 16:14:12 +00:00
|
|
|
#[derive(Component)] pub struct IsTargeted;
|
2024-03-17 14:23:22 +00:00
|
|
|
|
2024-04-10 19:03:30 +00:00
|
|
|
#[derive(Resource)]
|
|
|
|
pub struct AugmentedRealityState {
|
|
|
|
pub overlays_visible: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)] pub struct AugmentedRealityOverlayBroadcaster;
|
|
|
|
#[derive(Component)]
|
|
|
|
pub struct AugmentedRealityOverlay {
|
|
|
|
pub owner: Entity,
|
|
|
|
}
|
|
|
|
|
2024-03-17 14:23:22 +00:00
|
|
|
#[derive(Resource)]
|
|
|
|
struct FPSUpdateTimer(Timer);
|
|
|
|
|
2024-03-19 02:18:16 +00:00
|
|
|
pub enum LogLevel {
|
|
|
|
Warning,
|
|
|
|
//Error,
|
|
|
|
Info,
|
|
|
|
//Debug,
|
|
|
|
Chat,
|
|
|
|
//Ping,
|
2024-03-30 19:11:11 +00:00
|
|
|
Notice,
|
2024-03-19 02:18:16 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 00:02:17 +00:00
|
|
|
struct Message {
|
|
|
|
text: String,
|
2024-03-19 02:18:16 +00:00
|
|
|
sender: String,
|
|
|
|
level: LogLevel,
|
2024-04-15 01:57:21 +00:00
|
|
|
time: f64,
|
|
|
|
}
|
|
|
|
impl Message {
|
|
|
|
pub fn get_freshness(&self) -> f64 {
|
|
|
|
if let Ok(epoch) = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
|
|
|
|
return (1.0 - (epoch.as_secs_f64() - self.time) / LOG_MAX_TIME_S).clamp(0.0, 1.0);
|
|
|
|
}
|
|
|
|
return 1.0;
|
|
|
|
}
|
2024-03-18 00:02:17 +00:00
|
|
|
}
|
|
|
|
|
2024-04-07 22:39:57 +00:00
|
|
|
#[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,
|
|
|
|
}}}
|
|
|
|
|
2024-03-17 23:36:56 +00:00
|
|
|
#[derive(Resource)]
|
2024-03-19 00:24:27 +00:00
|
|
|
pub struct Log {
|
2024-03-18 00:02:17 +00:00
|
|
|
logs: VecDeque<Message>,
|
2024-03-19 02:18:16 +00:00
|
|
|
needs_rerendering: bool,
|
2024-03-17 23:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Log {
|
2024-03-19 02:18:16 +00:00
|
|
|
pub fn info(&mut self, message: String) {
|
|
|
|
self.add(message, "System".to_string(), LogLevel::Info);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn chat(&mut self, message: String, sender: String) {
|
|
|
|
self.add(message, sender, LogLevel::Chat);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn warning(&mut self, message: String) {
|
|
|
|
self.add(message, "WARNING".to_string(), LogLevel::Warning);
|
|
|
|
}
|
|
|
|
|
2024-03-30 19:11:11 +00:00
|
|
|
pub fn notice(&mut self, message: String) {
|
|
|
|
self.add(message, "".to_string(), LogLevel::Notice);
|
|
|
|
}
|
|
|
|
|
2024-04-14 19:52:18 +00:00
|
|
|
pub fn add(&mut self, text: String, sender: String, level: LogLevel) {
|
2024-03-17 23:36:56 +00:00
|
|
|
if self.logs.len() == LOG_MAX {
|
|
|
|
self.logs.pop_front();
|
|
|
|
}
|
2024-03-18 00:02:17 +00:00
|
|
|
if let Ok(epoch) = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
|
|
|
|
self.logs.push_back(Message {
|
2024-04-14 19:52:18 +00:00
|
|
|
text,
|
|
|
|
sender,
|
|
|
|
level,
|
2024-04-15 01:57:21 +00:00
|
|
|
time: epoch.as_secs_f64(),
|
2024-03-18 00:02:17 +00:00
|
|
|
});
|
2024-03-19 02:18:16 +00:00
|
|
|
self.needs_rerendering = true;
|
2024-03-18 00:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-15 01:57:21 +00:00
|
|
|
#[allow(dead_code)]
|
2024-03-18 00:02:17 +00:00
|
|
|
pub fn remove_old(&mut self) {
|
|
|
|
if let Some(message) = self.logs.front() {
|
|
|
|
if let Ok(epoch) = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
|
2024-04-15 01:57:21 +00:00
|
|
|
if epoch.as_secs_f64() - message.time > LOG_MAX_TIME_S {
|
2024-03-18 00:02:17 +00:00
|
|
|
self.logs.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-17 23:36:56 +00:00
|
|
|
}
|
2024-04-05 03:13:09 +00:00
|
|
|
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.logs.clear();
|
|
|
|
}
|
2024-03-17 23:36:56 +00:00
|
|
|
}
|
|
|
|
|
2024-03-17 14:23:22 +00:00
|
|
|
fn setup(
|
|
|
|
mut commands: Commands,
|
2024-04-14 12:55:00 +00:00
|
|
|
settings: Res<var::Settings>,
|
2024-03-17 19:04:16 +00:00
|
|
|
asset_server: Res<AssetServer>,
|
2024-03-17 23:36:56 +00:00
|
|
|
mut log: ResMut<Log>,
|
2024-03-20 05:55:54 +00:00
|
|
|
mut ambient_light: ResMut<AmbientLight>,
|
2024-03-17 14:23:22 +00:00
|
|
|
) {
|
2024-03-30 19:11:11 +00:00
|
|
|
log.notice("Resuming from suspend".to_string());
|
|
|
|
log.notice("WARNING: Oxygen Low".to_string());
|
2024-03-17 21:28:10 +00:00
|
|
|
let visibility = if settings.hud_active {
|
2024-03-17 18:03:02 +00:00
|
|
|
Visibility::Inherited
|
|
|
|
} else {
|
|
|
|
Visibility::Hidden
|
|
|
|
};
|
2024-04-15 00:27:11 +00:00
|
|
|
let style = TextStyle {
|
|
|
|
font: asset_server.load(FONT),
|
|
|
|
font_size: settings.font_size_hud,
|
|
|
|
color: Color::GRAY,
|
|
|
|
..default()
|
|
|
|
};
|
2024-03-17 18:03:02 +00:00
|
|
|
let mut bundle_fps = TextBundle::from_sections([
|
2024-04-15 00:27:11 +00:00
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new(" ⚡ ", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new(" ☣ ", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("\n氧 OXYGEN ", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("\nProximity 警告 ", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("\nSuit Integrity ", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("\nVitals ", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()), // Target
|
2024-03-17 23:42:10 +00:00
|
|
|
]).with_style(Style {
|
2024-04-15 00:42:23 +00:00
|
|
|
position_type: PositionType::Absolute,
|
2024-03-18 00:23:35 +00:00
|
|
|
top: Val::VMin(2.0),
|
2024-04-15 00:42:23 +00:00
|
|
|
right: Val::VMin(3.0),
|
2024-03-17 23:42:10 +00:00
|
|
|
..default()
|
2024-04-15 00:42:23 +00:00
|
|
|
}).with_text_justify(JustifyText::Right);
|
2024-03-17 18:03:02 +00:00
|
|
|
bundle_fps.visibility = visibility;
|
2024-03-17 14:23:22 +00:00
|
|
|
commands.spawn((
|
2024-03-17 20:29:27 +00:00
|
|
|
GaugesText,
|
2024-03-28 19:47:18 +00:00
|
|
|
ToggleableHudElement,
|
|
|
|
bundle_fps,
|
2024-03-17 14:23:22 +00:00
|
|
|
));
|
2024-03-19 00:24:27 +00:00
|
|
|
|
|
|
|
// Add Chat Box
|
2024-03-19 02:18:16 +00:00
|
|
|
let bundle_chatbox = TextBundle::from_sections([
|
2024-04-15 01:57:21 +00:00
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
|
|
|
TextSection::new("", style.clone()),
|
2024-03-19 02:18:16 +00:00
|
|
|
]).with_style(Style {
|
|
|
|
position_type: PositionType::Absolute,
|
2024-04-15 01:57:21 +00:00
|
|
|
top: Val::VMin(0.0),
|
2024-03-19 14:51:08 +00:00
|
|
|
left: Val::VMin(0.0),
|
2024-03-19 02:18:16 +00:00
|
|
|
..default()
|
2024-03-19 05:14:25 +00:00
|
|
|
}).with_text_justify(JustifyText::Left);
|
2024-03-19 02:18:16 +00:00
|
|
|
commands.spawn((
|
2024-03-19 14:51:08 +00:00
|
|
|
NodeBundle {
|
|
|
|
style: Style {
|
2024-04-15 01:57:21 +00:00
|
|
|
width: Val::Percent(45.0),
|
2024-03-19 14:51:08 +00:00
|
|
|
align_items: AlignItems::Start,
|
|
|
|
position_type: PositionType::Absolute,
|
2024-04-15 01:57:21 +00:00
|
|
|
top: Val::VMin(2.0),
|
|
|
|
left: Val::VMin(3.0),
|
2024-03-19 14:51:08 +00:00
|
|
|
..default()
|
|
|
|
},
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
)).with_children(|parent| {
|
|
|
|
parent.spawn((
|
|
|
|
bundle_chatbox,
|
|
|
|
ChatText,
|
|
|
|
));
|
|
|
|
});
|
2024-03-20 05:55:54 +00:00
|
|
|
|
2024-03-28 19:47:18 +00:00
|
|
|
commands.spawn((
|
|
|
|
Reticule,
|
|
|
|
ToggleableHudElement,
|
|
|
|
NodeBundle {
|
|
|
|
style: Style {
|
|
|
|
width: Val::Px(2.0),
|
|
|
|
height: Val::Px(2.0),
|
|
|
|
position_type: PositionType::Absolute,
|
|
|
|
top: Val::Vh(50.0),
|
|
|
|
left: Val::Vw(50.0),
|
|
|
|
..default()
|
|
|
|
},
|
2024-04-14 19:52:18 +00:00
|
|
|
visibility,
|
2024-03-28 19:47:18 +00:00
|
|
|
background_color: Color::rgb(0.4, 0.4, 0.6).into(),
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
));
|
|
|
|
|
2024-04-05 20:16:01 +00:00
|
|
|
// Selectagon
|
|
|
|
commands.spawn((
|
|
|
|
Selectagon,
|
2024-04-05 20:43:14 +00:00
|
|
|
ToggleableHudElement,
|
|
|
|
OnlyHideWhenTogglingHud,
|
2024-04-05 20:16:01 +00:00
|
|
|
SceneBundle {
|
|
|
|
scene: asset_server.load(world::asset_name_to_path("selectagon")),
|
2024-04-05 20:43:14 +00:00
|
|
|
visibility: Visibility::Hidden,
|
2024-04-05 20:16:01 +00:00
|
|
|
..default()
|
|
|
|
},
|
|
|
|
));
|
|
|
|
|
2024-03-20 05:55:54 +00:00
|
|
|
// AR-related things
|
|
|
|
ambient_light.brightness = if settings.hud_active {
|
|
|
|
AMBIENT_LIGHT_AR
|
|
|
|
} else {
|
|
|
|
AMBIENT_LIGHT
|
|
|
|
};
|
2024-03-17 14:23:22 +00:00
|
|
|
}
|
|
|
|
|
2024-04-07 18:02:31 +00:00
|
|
|
fn update_hud(
|
2024-03-17 14:23:22 +00:00
|
|
|
diagnostics: Res<DiagnosticsStore>,
|
2024-03-17 22:49:50 +00:00
|
|
|
time: Res<Time>,
|
2024-03-18 00:02:17 +00:00
|
|
|
mut log: ResMut<Log>,
|
2024-04-08 02:16:01 +00:00
|
|
|
player: Query<(&actor::HitPoints, &actor::Suit, &actor::ExperiencesGForce), With<actor::Player>>,
|
2024-04-02 05:05:17 +00:00
|
|
|
q_camera: Query<(&Position, &LinearVelocity), With<actor::PlayerCamera>>,
|
2024-03-17 14:23:22 +00:00
|
|
|
mut timer: ResMut<FPSUpdateTimer>,
|
2024-03-17 20:29:27 +00:00
|
|
|
mut query: Query<&mut Text, With<GaugesText>>,
|
2024-04-13 13:26:45 +00:00
|
|
|
q_choices: Query<&chat::Choice>,
|
2024-04-12 22:39:21 +00:00
|
|
|
mut query_chat: Query<&mut Text, (With<ChatText>, Without<GaugesText>)>,
|
2024-03-18 02:42:14 +00:00
|
|
|
query_all_actors: Query<&actor::Actor>,
|
2024-04-14 12:55:00 +00:00
|
|
|
settings: Res<var::Settings>,
|
2024-04-08 02:37:58 +00:00
|
|
|
q_target: Query<(&IsClickable, Option<&Position>, Option<&LinearVelocity>), With<IsTargeted>>,
|
2024-03-17 14:23:22 +00:00
|
|
|
) {
|
2024-03-18 03:57:17 +00:00
|
|
|
// TODO only when hud is actually on
|
2024-03-19 02:18:16 +00:00
|
|
|
if timer.0.tick(time.delta()).just_finished() || log.needs_rerendering {
|
2024-03-30 16:19:11 +00:00
|
|
|
let q_camera_result = q_camera.get_single();
|
2024-03-17 22:49:50 +00:00
|
|
|
let player = player.get_single();
|
2024-04-15 01:57:21 +00:00
|
|
|
let mut freshest_line: f64 = 0.0;
|
2024-03-30 16:19:11 +00:00
|
|
|
if player.is_ok() && q_camera_result.is_ok() {
|
2024-04-08 02:16:01 +00:00
|
|
|
let (hp, suit, gforce) = player.unwrap();
|
2024-04-02 05:05:17 +00:00
|
|
|
let (pos, cam_v) = q_camera_result.unwrap();
|
2024-03-17 22:49:50 +00:00
|
|
|
for mut text in &mut query {
|
2024-04-08 02:16:01 +00:00
|
|
|
text.sections[0].value = format!("2524-03-12 03:02");
|
2024-03-17 22:49:50 +00:00
|
|
|
if let Some(fps) = diagnostics.get(&FrameTimeDiagnosticsPlugin::FPS) {
|
|
|
|
if let Some(value) = fps.smoothed() {
|
|
|
|
// Update the value of the second section
|
2024-04-08 02:16:01 +00:00
|
|
|
text.sections[4].value = format!("{value:.0}");
|
2024-03-17 22:49:50 +00:00
|
|
|
}
|
2024-03-17 14:23:22 +00:00
|
|
|
}
|
2024-04-01 13:41:45 +00:00
|
|
|
let power = suit.power / suit.power_max * 100.0;
|
2024-04-08 02:16:01 +00:00
|
|
|
text.sections[2].value = format!("{power:}%");
|
2024-03-17 22:49:50 +00:00
|
|
|
let oxy_percent = suit.oxygen / suit.oxygen_max * 100.0;
|
2024-03-23 19:29:16 +00:00
|
|
|
|
|
|
|
// the remaining oxygen hud info ignores leaking suits from low integrity
|
|
|
|
if suit.oxygen > nature::OXY_H {
|
|
|
|
let oxy_hour = suit.oxygen / nature::OXY_H;
|
2024-04-08 02:16:01 +00:00
|
|
|
text.sections[7].value = format!("{oxy_percent:.1}% [lasts {oxy_hour:.1} hours]");
|
2024-04-15 00:27:11 +00:00
|
|
|
text.sections[7].style.color = Color::GRAY;
|
2024-03-23 19:29:16 +00:00
|
|
|
} else {
|
|
|
|
let oxy_min = suit.oxygen / nature::OXY_M;
|
2024-04-08 02:16:01 +00:00
|
|
|
text.sections[7].value = format!("{oxy_percent:.1}% [lasts {oxy_min:.1} min]");
|
2024-04-15 00:27:11 +00:00
|
|
|
text.sections[7].style.color = Color::MAROON;
|
2024-03-23 19:29:16 +00:00
|
|
|
}
|
2024-04-08 02:16:01 +00:00
|
|
|
//let adrenaline = lifeform.adrenaline * 990.0 + 10.0;
|
|
|
|
//text.sections[11].value = format!("{adrenaline:.0}pg/mL");
|
2024-04-05 01:31:52 +00:00
|
|
|
let vitals = 100.0 * hp.current / hp.max;
|
2024-04-08 02:16:01 +00:00
|
|
|
text.sections[13].value = format!("{vitals:.0}%");
|
2024-04-15 00:32:30 +00:00
|
|
|
if vitals < 50.0 {
|
|
|
|
text.sections[13].style.color = Color::MAROON;
|
|
|
|
} else {
|
|
|
|
text.sections[13].style.color = Color::GRAY;
|
|
|
|
}
|
2024-03-18 02:42:14 +00:00
|
|
|
let all_actors = query_all_actors.iter().len();
|
2024-04-08 02:16:01 +00:00
|
|
|
text.sections[9].value = format!("{all_actors:.0}");
|
2024-03-20 17:37:10 +00:00
|
|
|
let integrity = suit.integrity * 100.0;
|
2024-04-08 02:16:01 +00:00
|
|
|
text.sections[11].value = format!("{integrity:.0}%");
|
2024-04-15 00:32:30 +00:00
|
|
|
if integrity < 50.0 {
|
|
|
|
text.sections[11].style.color = Color::MAROON;
|
|
|
|
} else {
|
|
|
|
text.sections[11].style.color = Color::GRAY;
|
|
|
|
}
|
2024-04-08 02:16:01 +00:00
|
|
|
//text.sections[17].value = format!("{speed_readable}/s / {kmh:.0}km/h / {gforce:.1}g");
|
2024-04-05 16:14:12 +00:00
|
|
|
|
|
|
|
// Target display
|
2024-04-08 02:16:01 +00:00
|
|
|
let dist_scalar: f64;
|
2024-04-08 02:37:58 +00:00
|
|
|
if let Ok((IsClickable { distance: Some(dist), .. }, _, _)) = q_target.get_single() {
|
2024-04-08 01:00:48 +00:00
|
|
|
dist_scalar = *dist;
|
2024-04-05 16:14:12 +00:00
|
|
|
}
|
|
|
|
else {
|
2024-04-07 22:39:57 +00:00
|
|
|
let target: Option<DVec3>;
|
2024-04-08 02:37:58 +00:00
|
|
|
if let Ok((_, Some(targetpos), _)) = q_target.get_single() {
|
2024-04-07 22:39:57 +00:00
|
|
|
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;
|
|
|
|
dist_scalar = dist.length();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dist_scalar = 0.0;
|
|
|
|
}
|
2024-04-05 16:14:12 +00:00
|
|
|
}
|
2024-04-07 22:39:57 +00:00
|
|
|
|
2024-04-10 23:34:16 +00:00
|
|
|
let dev_speed = if settings.dev_mode {
|
|
|
|
let x = pos.x;
|
|
|
|
let y = pos.y;
|
|
|
|
let z = pos.z;
|
|
|
|
format!("\n{x:.0}\n{y:.0}\n{z:.0}")
|
|
|
|
} else {
|
|
|
|
"".to_string()
|
|
|
|
};
|
2024-04-08 02:37:58 +00:00
|
|
|
let gforce = gforce.gforce;
|
|
|
|
if let Ok((clickable, _, target_v_maybe)) = q_target.get_single() {
|
2024-04-08 02:27:02 +00:00
|
|
|
let distance = if dist_scalar.is_nan() {
|
|
|
|
"UNKNOWN".to_string()
|
|
|
|
} else if dist_scalar != 0.0 {
|
|
|
|
nature::readable_distance(dist_scalar)
|
|
|
|
} else {
|
|
|
|
"ERROR".to_string()
|
|
|
|
};
|
2024-04-08 02:37:58 +00:00
|
|
|
let speed: f64 = if let Some(target_v) = target_v_maybe {
|
|
|
|
(target_v.0 - cam_v.0).length()
|
|
|
|
} else {
|
|
|
|
cam_v.length()
|
|
|
|
};
|
|
|
|
let speed_readable = nature::readable_distance(speed);
|
2024-04-08 02:27:02 +00:00
|
|
|
let target_name = clickable.name.clone().unwrap_or("Unnamed".to_string());
|
2024-04-10 23:34:16 +00:00
|
|
|
text.sections[14].value = format!("\n\nTarget: {target_name}\nDistance: {distance}\nΔv {speed_readable}/s + {gforce:.1}g{dev_speed}");
|
2024-04-05 16:14:12 +00:00
|
|
|
}
|
|
|
|
else {
|
2024-04-08 02:37:58 +00:00
|
|
|
let speed = cam_v.length();
|
|
|
|
let speed_readable = nature::readable_distance(speed);
|
2024-04-10 23:34:16 +00:00
|
|
|
text.sections[14].value = format!("\nv {speed_readable}/s + {gforce:.1}g{dev_speed}");
|
2024-04-05 17:03:50 +00:00
|
|
|
}
|
2024-03-17 14:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-19 02:18:16 +00:00
|
|
|
|
2024-04-12 22:39:21 +00:00
|
|
|
if let Ok(mut chat) = query_chat.get_single_mut() {
|
2024-04-15 01:57:21 +00:00
|
|
|
let mut row = 0;
|
|
|
|
let bright = Color::rgb(0.8, 0.75, 0.78);
|
|
|
|
|
|
|
|
// Chat Log and System Log
|
|
|
|
let logfilter = if settings.hud_active {
|
|
|
|
|_msg: &&Message| { true }
|
|
|
|
} else {
|
|
|
|
|msg: &&Message| { match msg.level {
|
|
|
|
LogLevel::Chat => true,
|
|
|
|
LogLevel::Warning => true,
|
|
|
|
LogLevel::Info => true,
|
|
|
|
_ => false
|
|
|
|
}}
|
|
|
|
};
|
|
|
|
let mut messages: Vec<&Message> = log.logs.iter()
|
|
|
|
.filter(logfilter)
|
|
|
|
.rev()
|
|
|
|
.take(15)
|
|
|
|
.collect();
|
|
|
|
messages.reverse();
|
|
|
|
for msg in &messages {
|
|
|
|
if msg.sender.is_empty() {
|
|
|
|
chat.sections[row].value = msg.text.clone() + "\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
chat.sections[row].value = format!("{}: {}\n", msg.sender, msg.text);
|
|
|
|
}
|
|
|
|
let freshness = msg.get_freshness();
|
|
|
|
let clr: f32 = (freshness.powf(1.5) as f32).clamp(0.1, 1.0);
|
|
|
|
freshest_line = freshest_line.max(freshness);
|
|
|
|
chat.sections[row].style.color = Color::rgba(0.7, 0.7, 0.7, clr);
|
|
|
|
row += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Highlight most recent line if in a conversation
|
|
|
|
if row > 0 && (q_choices.is_empty() || freshest_line > 0.5) {
|
|
|
|
chat.sections[row-1].style.color = bright;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add padding between chat and choices
|
|
|
|
chat.sections[row].value = "\n".to_string();
|
|
|
|
row += 1;
|
|
|
|
|
2024-04-13 13:26:45 +00:00
|
|
|
// Choices
|
|
|
|
let mut choices: Vec<String> = Vec::new();
|
|
|
|
let mut count = 0;
|
|
|
|
for choice in &q_choices {
|
|
|
|
if count > 9 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let press_this = REPLY_NUMBERS[choice.key];
|
|
|
|
let reply = &choice.text;
|
|
|
|
//let recipient = &choice.recipient;
|
|
|
|
// TODO: indicate recipients if there's more than one
|
|
|
|
choices.push(format!("{press_this} {reply}"));
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
if count < 4 {
|
|
|
|
for _padding in 0..(4-count) {
|
|
|
|
choices.push(" ".to_string());
|
|
|
|
}
|
|
|
|
}
|
2024-04-15 01:57:21 +00:00
|
|
|
for choice in choices {
|
|
|
|
chat.sections[row].value = choice + "\n";
|
|
|
|
chat.sections[row].style.color = bright;
|
|
|
|
row += 1;
|
|
|
|
}
|
2024-04-12 22:39:21 +00:00
|
|
|
|
2024-04-15 01:57:21 +00:00
|
|
|
// Blank the remaining rows
|
|
|
|
while row < LOG_MAX_ROWS {
|
|
|
|
chat.sections[row].value = "".to_string();
|
|
|
|
row += 1;
|
|
|
|
}
|
2024-04-12 22:39:21 +00:00
|
|
|
}
|
2024-03-19 02:18:16 +00:00
|
|
|
log.needs_rerendering = false;
|
2024-04-15 01:57:21 +00:00
|
|
|
|
|
|
|
if q_choices.is_empty() && freshest_line < 0.2 {
|
|
|
|
log.remove_old();
|
|
|
|
}
|
2024-03-17 14:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-17 18:03:02 +00:00
|
|
|
|
|
|
|
fn handle_input(
|
|
|
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
2024-04-05 16:14:12 +00:00
|
|
|
mouse_input: Res<ButtonInput<MouseButton>>,
|
2024-04-14 12:55:00 +00:00
|
|
|
mut settings: ResMut<var::Settings>,
|
2024-04-05 20:43:14 +00:00
|
|
|
mut q_hud: Query<(&mut Visibility, Option<&OnlyHideWhenTogglingHud>), With<ToggleableHudElement>>,
|
2024-04-05 00:58:17 +00:00
|
|
|
mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
|
|
|
|
mut ew_togglemusic: EventWriter<audio::ToggleMusicEvent>,
|
2024-04-05 19:27:19 +00:00
|
|
|
mut ew_target: EventWriter<TargetEvent>,
|
2024-03-20 05:55:54 +00:00
|
|
|
mut ambient_light: ResMut<AmbientLight>,
|
2024-04-05 18:10:45 +00:00
|
|
|
q_objects: Query<(Entity, &Transform), (With<IsClickable>, Without<IsTargeted>, Without<actor::PlayerDrivesThis>, Without<actor::Player>)>,
|
2024-04-05 16:14:12 +00:00
|
|
|
q_camera: Query<&Transform, With<Camera>>,
|
2024-03-17 18:03:02 +00:00
|
|
|
) {
|
|
|
|
if keyboard_input.just_pressed(settings.key_togglehud) {
|
2024-03-28 19:47:18 +00:00
|
|
|
if settings.hud_active {
|
2024-04-05 20:43:14 +00:00
|
|
|
for (mut hudelement_visibility, _) in q_hud.iter_mut() {
|
2024-03-28 19:47:18 +00:00
|
|
|
*hudelement_visibility = Visibility::Hidden;
|
|
|
|
}
|
|
|
|
settings.hud_active = false;
|
|
|
|
ambient_light.brightness = AMBIENT_LIGHT;
|
|
|
|
}
|
|
|
|
else {
|
2024-04-05 20:43:14 +00:00
|
|
|
for (mut hudelement_visibility, only_hide) in q_hud.iter_mut() {
|
|
|
|
if only_hide.is_none() {
|
|
|
|
*hudelement_visibility = Visibility::Inherited;
|
|
|
|
}
|
2024-03-18 13:08:11 +00:00
|
|
|
}
|
2024-03-28 19:47:18 +00:00
|
|
|
settings.hud_active = true;
|
|
|
|
ambient_light.brightness = AMBIENT_LIGHT_AR;
|
2024-03-17 18:03:02 +00:00
|
|
|
}
|
2024-04-05 00:58:17 +00:00
|
|
|
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Switch));
|
|
|
|
ew_togglemusic.send(audio::ToggleMusicEvent());
|
2024-03-17 18:03:02 +00:00
|
|
|
}
|
2024-04-05 20:43:14 +00:00
|
|
|
if settings.hud_active && mouse_input.just_pressed(settings.key_selectobject) {
|
2024-04-05 18:38:44 +00:00
|
|
|
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::<Entity>(objects, camtrans) {
|
2024-04-05 19:27:19 +00:00
|
|
|
ew_target.send(TargetEvent(Some(new_target)));
|
2024-04-05 16:14:12 +00:00
|
|
|
}
|
2024-04-05 18:38:44 +00:00
|
|
|
else {
|
2024-04-05 19:27:19 +00:00
|
|
|
ew_target.send(TargetEvent(None));
|
2024-04-05 16:14:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-20 01:03:42 +00:00
|
|
|
}
|
2024-04-05 19:27:19 +00:00
|
|
|
|
|
|
|
fn handle_target_event(
|
|
|
|
mut commands: Commands,
|
2024-04-14 12:55:00 +00:00
|
|
|
settings: Res<var::Settings>,
|
2024-04-05 19:27:19 +00:00
|
|
|
mut er_target: EventReader<TargetEvent>,
|
|
|
|
mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
|
|
|
|
q_target: Query<Entity, With<IsTargeted>>,
|
|
|
|
) {
|
|
|
|
let mut play_sfx = false;
|
|
|
|
|
|
|
|
for TargetEvent(target) in er_target.read() {
|
|
|
|
for old_target in &q_target {
|
|
|
|
commands.entity(old_target).remove::<IsTargeted>();
|
|
|
|
play_sfx = true;
|
|
|
|
}
|
|
|
|
if let Some(entity) = target {
|
|
|
|
commands.entity(*entity).insert(IsTargeted);
|
|
|
|
play_sfx = true;
|
|
|
|
}
|
|
|
|
if play_sfx && !settings.mute_sfx {
|
|
|
|
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click));
|
|
|
|
}
|
|
|
|
break; // Only accept a single event per frame
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_target_selectagon(
|
2024-04-14 12:55:00 +00:00
|
|
|
settings: Res<var::Settings>,
|
2024-04-05 20:16:01 +00:00
|
|
|
mut q_selectagon: Query<(&mut Transform, &mut Visibility), (With<Selectagon>, Without<IsTargeted>, Without<Camera>)>,
|
|
|
|
q_target: Query<&Transform, (With<IsTargeted>, Without<Camera>, Without<Selectagon>)>,
|
|
|
|
q_camera: Query<&Transform, (With<Camera>, Without<IsTargeted>, Without<Selectagon>)>,
|
2024-04-05 19:27:19 +00:00
|
|
|
) {
|
2024-04-05 20:43:14 +00:00
|
|
|
if !settings.hud_active || q_camera.is_empty() {
|
2024-04-05 20:16:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let camera_trans = q_camera.get_single().unwrap();
|
|
|
|
|
|
|
|
if let Ok((mut selectagon_trans, mut selectagon_vis)) = q_selectagon.get_single_mut() {
|
|
|
|
if let Ok(target_trans) = q_target.get_single() {
|
|
|
|
match *selectagon_vis {
|
|
|
|
Visibility::Hidden => { *selectagon_vis = Visibility::Visible; },
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
selectagon_trans.translation = target_trans.translation;
|
|
|
|
selectagon_trans.scale = target_trans.scale;
|
2024-04-15 02:07:56 +00:00
|
|
|
selectagon_trans.rotation = Quat::from_rotation_arc(Vec3::Z, (-selectagon_trans.translation).normalize());
|
2024-04-05 20:30:43 +00:00
|
|
|
|
|
|
|
// Enlarge Selectagon to a minimum angular diameter
|
|
|
|
let (angular_diameter, _, _) = camera::calc_angular_diameter(
|
|
|
|
&selectagon_trans, camera_trans);
|
|
|
|
let min_angular_diameter = 2.0f32.to_radians();
|
|
|
|
if angular_diameter < min_angular_diameter {
|
|
|
|
selectagon_trans.scale *= min_angular_diameter / angular_diameter;
|
|
|
|
}
|
2024-04-05 20:16:01 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
match *selectagon_vis {
|
|
|
|
Visibility::Hidden => {},
|
|
|
|
_ => { *selectagon_vis = Visibility::Hidden; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-05 19:27:19 +00:00
|
|
|
}
|
2024-04-10 19:03:30 +00:00
|
|
|
|
|
|
|
fn update_ar_overlays (
|
|
|
|
q_owners: Query<(Entity, &Transform, &Visibility), (With<AugmentedRealityOverlayBroadcaster>, Without<AugmentedRealityOverlay>)>,
|
|
|
|
mut q_overlays: Query<(&mut Transform, &mut Visibility, &mut AugmentedRealityOverlay)>,
|
2024-04-14 12:55:00 +00:00
|
|
|
settings: ResMut<var::Settings>,
|
2024-04-10 19:03:30 +00:00
|
|
|
mut state: ResMut<AugmentedRealityState>,
|
|
|
|
) {
|
|
|
|
let (need_activate, need_clean, need_update);
|
|
|
|
if settings.hud_active {
|
|
|
|
need_activate = !state.overlays_visible;
|
|
|
|
need_clean = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
need_activate = false;
|
|
|
|
need_clean = state.overlays_visible;
|
|
|
|
}
|
|
|
|
need_update = settings.hud_active;
|
|
|
|
state.overlays_visible = settings.hud_active;
|
|
|
|
|
|
|
|
if need_update || need_clean || need_activate {
|
2024-04-10 20:36:19 +00:00
|
|
|
for (mut trans, mut vis, ar) in &mut q_overlays {
|
2024-04-10 19:03:30 +00:00
|
|
|
for (owner_id, owner_trans, owner_vis) in &q_owners {
|
|
|
|
if owner_id == ar.owner {
|
|
|
|
*trans = *owner_trans;
|
|
|
|
if need_clean {
|
|
|
|
*vis = Visibility::Hidden;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*vis = *owner_vis;
|
|
|
|
}
|
2024-04-10 20:36:19 +00:00
|
|
|
break;
|
2024-04-10 19:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|