Files
bevy-snake/src/ui/status.rs
2023-09-18 21:24:51 +02:00

62 lines
1.7 KiB
Rust

use crate::{Level, Score};
use bevy::prelude::*;
#[derive(Component)]
pub(super) struct StatusText;
pub(super) fn spawn_status_text(mut commands: Commands, asset_server: Res<AssetServer>) {
let font = asset_server.load("fonts/Audiowide-Regular.ttf");
commands.spawn((
StatusText,
TextBundle::from_sections([
TextSection::new(
"Level: ",
TextStyle {
font: font.clone(),
font_size: 32.0,
color: Color::WHITE,
},
),
TextSection::from_style(TextStyle {
font: font.clone(),
font_size: 32.0,
color: Color::WHITE,
}),
TextSection::new(
" Score: ",
TextStyle {
font: font.clone(),
font_size: 32.0,
color: Color::WHITE,
},
),
TextSection::from_style(TextStyle {
font,
font_size: 32.0,
color: Color::WHITE,
}),
])
.with_style(Style {
position_type: PositionType::Absolute,
left: Val::Px(10.0),
bottom: Val::Px(10.0),
..Default::default()
}),
));
}
pub(super) fn update_status_text(
mut query: Query<&mut Text, With<StatusText>>,
score: Res<Score>,
level: Res<Level>,
) {
let score = score.0;
let level = level.0;
for mut text in query.iter_mut() {
text.sections[1].value = format!("{level}");
text.sections[3].value = format!("{score}");
}
}