outfly/src/hud.rs

69 lines
2 KiB
Rust
Raw Normal View History

2024-03-17 14:23:22 +00:00
use bevy::prelude::*;
use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin};
pub struct OutFlyHudPlugin;
impl Plugin for OutFlyHudPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup);
app.add_systems(Update, update);
app.insert_resource(FPSUpdateTimer(Timer::from_seconds(0.25, TimerMode::Repeating)));
}
}
#[derive(Component)]
struct FpsText;
#[derive(Resource)]
struct FPSUpdateTimer(Timer);
fn setup(
mut commands: Commands,
) {
commands.spawn((
TextBundle::from_sections([
TextSection::new(
"FPS: ",
TextStyle {
//font: asset_server.load("fonts/FiraSans-Bold.ttf"),
2024-03-17 14:34:47 +00:00
font_size: 30.0,
2024-03-17 14:23:22 +00:00
..default()
},
),
TextSection::from_style(if cfg!(feature = "default_font") {
TextStyle {
2024-03-17 14:34:47 +00:00
font_size: 30.0,
2024-03-17 14:23:22 +00:00
color: Color::GOLD,
..default()
}
} else {
// "default_font" feature is unavailable, load a font to use instead.
TextStyle {
//font: asset_server.load("fonts/FiraMono-Medium.ttf"),
2024-03-17 14:34:47 +00:00
font_size: 30.0,
2024-03-17 14:23:22 +00:00
color: Color::GOLD,
..default()
}
}),
]),
FpsText,
));
}
fn update(
diagnostics: Res<DiagnosticsStore>,
time:Res<Time>,
mut timer: ResMut<FPSUpdateTimer>,
mut query: Query<&mut Text, With<FpsText>>,
) {
if timer.0.tick(time.delta()).just_finished() {
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}");
}
}
}
}
}