add system log to hud

This commit is contained in:
yuni 2024-03-18 00:36:56 +01:00
parent d95d3e8f9f
commit 49414989bd

View file

@ -1,15 +1,18 @@
use crate::{settings, actor}; use crate::{settings, actor};
use bevy::prelude::*; use bevy::prelude::*;
use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin}; use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin};
use std::collections::VecDeque;
const HUD_REFRESH_TIME: f32 = 0.5; const HUD_REFRESH_TIME: f32 = 0.5;
const FONT: &str = "tmp/fonts/NotoSansSC-Thin.ttf"; const FONT: &str = "tmp/fonts/NotoSansSC-Thin.ttf";
const LOG_MAX: usize = 5;
pub struct HudPlugin; pub struct HudPlugin;
impl Plugin for HudPlugin { impl Plugin for HudPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, setup); app.add_systems(Startup, setup);
app.add_systems(Update, (update, handle_input)); app.add_systems(Update, (update, handle_input));
app.insert_resource(Log { logs: VecDeque::with_capacity(LOG_MAX) });
app.insert_resource(FPSUpdateTimer( app.insert_resource(FPSUpdateTimer(
Timer::from_seconds(HUD_REFRESH_TIME, TimerMode::Repeating))); Timer::from_seconds(HUD_REFRESH_TIME, TimerMode::Repeating)));
} }
@ -21,11 +24,28 @@ struct GaugesText;
#[derive(Resource)] #[derive(Resource)]
struct FPSUpdateTimer(Timer); struct FPSUpdateTimer(Timer);
#[derive(Resource)]
struct Log {
logs: VecDeque<String>,
}
impl Log {
pub fn add(&mut self, message: String) {
if self.logs.len() == LOG_MAX {
self.logs.pop_front();
}
self.logs.push_back(message);
}
}
fn setup( fn setup(
mut commands: Commands, mut commands: Commands,
settings: Res<settings::Settings>, settings: Res<settings::Settings>,
asset_server: Res<AssetServer>, 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 { let visibility = if settings.hud_active {
Visibility::Inherited Visibility::Inherited
} else { } else {
@ -104,6 +124,24 @@ fn setup(
..default() ..default()
} }
), ),
TextSection::new(
"\n\n",
TextStyle {
font: asset_server.load(FONT),
font_size: settings.font_size_hud,
color: Color::GRAY,
..default()
}
),
TextSection::new(
"<log>",
TextStyle {
font: asset_server.load(FONT),
font_size: settings.font_size_hud,
color: Color::GRAY,
..default()
}
),
]); ]);
bundle_fps.visibility = visibility; bundle_fps.visibility = visibility;
commands.spawn(( commands.spawn((
@ -115,6 +153,7 @@ fn setup(
fn update( fn update(
diagnostics: Res<DiagnosticsStore>, diagnostics: Res<DiagnosticsStore>,
time: Res<Time>, time: Res<Time>,
log: Res<Log>,
player: Query<(&actor::Suit, &actor::LifeForm), With<actor::Player>>, player: Query<(&actor::Suit, &actor::LifeForm), With<actor::Player>>,
mut timer: ResMut<FPSUpdateTimer>, mut timer: ResMut<FPSUpdateTimer>,
mut query: Query<&mut Text, With<GaugesText>>, mut query: Query<&mut Text, With<GaugesText>>,
@ -139,6 +178,10 @@ fn update(
text.sections[7].value = format!("{adrenaline:.0}pg/mL"); text.sections[7].value = format!("{adrenaline:.0}pg/mL");
} }
} }
for mut text in &mut query {
let logs_vec: Vec<&str> = log.logs.iter().map(|s| s.as_str()).collect();
text.sections[9].value = logs_vec.join("\n");
}
} }
} }