285 lines
8.8 KiB
Rust
285 lines
8.8 KiB
Rust
use crate::{settings, actor, audio};
|
|
use bevy::prelude::*;
|
|
use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin};
|
|
use bevy::core_pipeline::bloom::{BloomCompositeMode, BloomSettings};
|
|
use std::collections::VecDeque;
|
|
use std::time::SystemTime;
|
|
|
|
const HUD_REFRESH_TIME: f32 = 0.5;
|
|
const FONT: &str = "tmp/fonts/NotoSansSC-Thin.ttf";
|
|
const LOG_MAX: usize = 5;
|
|
const LOG_MAX_TIME_S: u64 = 20;
|
|
|
|
pub struct HudPlugin;
|
|
impl Plugin for HudPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.add_systems(Startup, setup);
|
|
app.add_systems(Update, (update, handle_input));
|
|
app.insert_resource(Log { logs: VecDeque::with_capacity(LOG_MAX) });
|
|
app.insert_resource(FPSUpdateTimer(
|
|
Timer::from_seconds(HUD_REFRESH_TIME, TimerMode::Repeating)));
|
|
}
|
|
}
|
|
|
|
#[derive(Component)] struct GaugesText;
|
|
#[derive(Component)] struct ChatText;
|
|
|
|
#[derive(Resource)]
|
|
struct FPSUpdateTimer(Timer);
|
|
|
|
struct Message {
|
|
text: String,
|
|
time: u64,
|
|
}
|
|
|
|
#[derive(Resource)]
|
|
pub struct Log {
|
|
logs: VecDeque<Message>,
|
|
}
|
|
|
|
impl Log {
|
|
pub fn add(&mut self, message: String) {
|
|
if self.logs.len() == LOG_MAX {
|
|
self.logs.pop_front();
|
|
}
|
|
if let Ok(epoch) = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
|
|
self.logs.push_back(Message {
|
|
text: message,
|
|
time: epoch.as_secs(),
|
|
});
|
|
}
|
|
}
|
|
|
|
pub fn remove_old(&mut self) {
|
|
if let Some(message) = self.logs.front() {
|
|
if let Ok(epoch) = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
|
|
if epoch.as_secs() - message.time > LOG_MAX_TIME_S {
|
|
self.logs.pop_front();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn setup(
|
|
mut commands: Commands,
|
|
settings: Res<settings::Settings>,
|
|
asset_server: Res<AssetServer>,
|
|
mut log: ResMut<Log>,
|
|
) {
|
|
log.add("Customer wake-up registered.".to_string());
|
|
log.add("Systems reactivated.".to_string());
|
|
let visibility = if settings.hud_active {
|
|
Visibility::Inherited
|
|
} else {
|
|
Visibility::Hidden
|
|
};
|
|
let mut bundle_fps = TextBundle::from_sections([
|
|
TextSection::new(
|
|
"帧率 ",
|
|
TextStyle {
|
|
font: asset_server.load(FONT),
|
|
font_size: settings.font_size_hud,
|
|
color: Color::GRAY,
|
|
..default()
|
|
},
|
|
),
|
|
TextSection::new(
|
|
"",
|
|
TextStyle {
|
|
font: asset_server.load(FONT),
|
|
font_size: settings.font_size_hud,
|
|
color: Color::GRAY,
|
|
..default()
|
|
}
|
|
),
|
|
TextSection::new(
|
|
"\n电量 ",
|
|
TextStyle {
|
|
font: asset_server.load(FONT),
|
|
font_size: settings.font_size_hud,
|
|
color: Color::GRAY,
|
|
..default()
|
|
},
|
|
),
|
|
TextSection::new(
|
|
"",
|
|
TextStyle {
|
|
font: asset_server.load(FONT),
|
|
font_size: settings.font_size_hud,
|
|
color: Color::GRAY,
|
|
..default()
|
|
}
|
|
),
|
|
TextSection::new(
|
|
"\n氧 OXYGEN ",
|
|
TextStyle {
|
|
font: asset_server.load(FONT),
|
|
font_size: settings.font_size_hud,
|
|
color: Color::GRAY,
|
|
..default()
|
|
},
|
|
),
|
|
TextSection::new(
|
|
"",
|
|
TextStyle {
|
|
font: asset_server.load(FONT),
|
|
font_size: settings.font_size_hud,
|
|
color: Color::MAROON,
|
|
..default()
|
|
}
|
|
),
|
|
TextSection::new(
|
|
"\nAdren水平 ",
|
|
TextStyle {
|
|
font: asset_server.load(FONT),
|
|
font_size: settings.font_size_hud,
|
|
color: Color::GRAY,
|
|
..default()
|
|
},
|
|
),
|
|
TextSection::new(
|
|
"",
|
|
TextStyle {
|
|
font: asset_server.load(FONT),
|
|
font_size: settings.font_size_hud,
|
|
color: Color::GRAY,
|
|
..default()
|
|
}
|
|
),
|
|
TextSection::new(
|
|
"\nProximity 警告 ",
|
|
TextStyle {
|
|
font: asset_server.load(FONT),
|
|
font_size: settings.font_size_hud,
|
|
color: Color::GRAY,
|
|
..default()
|
|
},
|
|
),
|
|
TextSection::new(
|
|
"",
|
|
TextStyle {
|
|
font: asset_server.load(FONT),
|
|
font_size: settings.font_size_hud,
|
|
color: Color::GRAY,
|
|
..default()
|
|
}
|
|
),
|
|
TextSection::new(
|
|
"\n\n",
|
|
TextStyle {
|
|
font: asset_server.load(FONT),
|
|
font_size: settings.font_size_hud,
|
|
color: Color::GRAY,
|
|
..default()
|
|
}
|
|
),
|
|
TextSection::from_style(
|
|
TextStyle {
|
|
font: asset_server.load(FONT),
|
|
font_size: settings.font_size_hud,
|
|
color: Color::GRAY,
|
|
..default()
|
|
}
|
|
),
|
|
]).with_style(Style {
|
|
top: Val::VMin(2.0),
|
|
left: Val::VMin(3.0),
|
|
..default()
|
|
});
|
|
bundle_fps.visibility = visibility;
|
|
commands.spawn((
|
|
bundle_fps,
|
|
GaugesText,
|
|
));
|
|
|
|
// Add Chat Box
|
|
// let bundle_chatbox = TextBundle::from_sections([
|
|
// TextSection::new(
|
|
// "Hello :D",
|
|
// TextStyle {
|
|
// font: asset_server.load(FONT),
|
|
// font_size: settings.font_size_hud,
|
|
// color: Color::GRAY,
|
|
// ..default()
|
|
// }
|
|
// ),
|
|
// ]).with_style(Style {
|
|
// position_type: PositionType::Absolute,
|
|
// bottom: Val::VMin(40.0),
|
|
// left: Val::VMin(30.0),
|
|
// ..default()
|
|
// });
|
|
// commands.spawn((
|
|
// bundle_chatbox,
|
|
// ChatText,
|
|
// ));
|
|
}
|
|
|
|
fn update(
|
|
diagnostics: Res<DiagnosticsStore>,
|
|
time: Res<Time>,
|
|
mut log: ResMut<Log>,
|
|
player: Query<(&actor::Suit, &actor::LifeForm), With<actor::Player>>,
|
|
mut timer: ResMut<FPSUpdateTimer>,
|
|
mut query: Query<&mut Text, With<GaugesText>>,
|
|
query_all_actors: Query<&actor::Actor>,
|
|
) {
|
|
// TODO only when hud is actually on
|
|
if timer.0.tick(time.delta()).just_finished() {
|
|
let player = player.get_single();
|
|
if player.is_ok() {
|
|
let (suit, lifeform) = player.unwrap();
|
|
for mut text in &mut query {
|
|
if let Some(fps) = diagnostics.get(&FrameTimeDiagnosticsPlugin::FPS) {
|
|
if let Some(value) = fps.smoothed() {
|
|
// Update the value of the second section
|
|
text.sections[1].value = format!("{value:.0}");
|
|
}
|
|
}
|
|
let power = suit.power;
|
|
text.sections[3].value = format!("{power:}Wh");
|
|
let oxy_percent = suit.oxygen / suit.oxygen_max * 100.0;
|
|
let oxy_total = suit.oxygen * 1e6;
|
|
text.sections[5].value = format!("{oxy_percent:.1}% [{oxy_total:.0}mg]");
|
|
let adrenaline = lifeform.adrenaline * 990.0 + 10.0;
|
|
text.sections[7].value = format!("{adrenaline:.0}pg/mL");
|
|
let all_actors = query_all_actors.iter().len();
|
|
text.sections[9].value = format!("{all_actors:.0}");
|
|
}
|
|
}
|
|
for mut text in &mut query {
|
|
let logs_vec: Vec<&str> = log.logs.iter().map(|s| s.text.as_str()).collect();
|
|
text.sections[11].value = logs_vec.join("\n");
|
|
}
|
|
log.remove_old();
|
|
}
|
|
}
|
|
|
|
fn handle_input(
|
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
|
mut settings: ResMut<settings::Settings>,
|
|
mut query: Query<&mut Visibility, With<GaugesText>>,
|
|
mut query_bloomsettings: Query<&mut BloomSettings>,
|
|
mut evwriter: EventWriter<audio::AudioSwitchEvent>,
|
|
mut evwriter_togglemusic: EventWriter<audio::ToggleMusicEvent>,
|
|
) {
|
|
if keyboard_input.just_pressed(settings.key_togglehud) {
|
|
for mut vis in &mut query {
|
|
if let Ok(mut bloomsettings) = query_bloomsettings.get_single_mut() {
|
|
if *vis == Visibility::Inherited {
|
|
*vis = Visibility::Hidden;
|
|
settings.hud_active = false;
|
|
bloomsettings.composite_mode = BloomCompositeMode::EnergyConserving;
|
|
} else {
|
|
*vis = Visibility::Inherited;
|
|
settings.hud_active = true;
|
|
bloomsettings.composite_mode = BloomCompositeMode::Additive;
|
|
}
|
|
evwriter.send(audio::AudioSwitchEvent());
|
|
evwriter_togglemusic.send(audio::ToggleMusicEvent());
|
|
}
|
|
}
|
|
}
|
|
}
|