56 lines
1.4 KiB
Rust
56 lines
1.4 KiB
Rust
use avian3d::prelude::LinearVelocity;
|
|
use bevy::{
|
|
app::Update,
|
|
prelude::{
|
|
AlignSelf, App, AssetServer, Color, Commands, Component, Entity, Plugin, Query, Res, Text,
|
|
With,
|
|
},
|
|
text::{TextColor, TextFont},
|
|
ui::{Node, TargetCamera},
|
|
};
|
|
|
|
use crate::bike::CyberBikeBody;
|
|
|
|
#[derive(Component)]
|
|
struct UpText;
|
|
|
|
pub(crate) fn setup_ui(
|
|
mut commands: Commands,
|
|
asset_server: Res<AssetServer>,
|
|
target_camera: Entity,
|
|
) {
|
|
commands
|
|
.spawn((
|
|
Node {
|
|
align_self: AlignSelf::FlexEnd,
|
|
..Default::default()
|
|
},
|
|
// Use `Text` directly
|
|
TextFont {
|
|
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
|
|
font_size: 40.0,
|
|
..Default::default()
|
|
},
|
|
TextColor(Color::srgba_u8(255, 215, 0, 230)),
|
|
Text::default(),
|
|
))
|
|
.insert((UpText, TargetCamera(target_camera)));
|
|
}
|
|
|
|
fn update_ui(
|
|
state_query: Query<&LinearVelocity, With<CyberBikeBody>>,
|
|
mut text_query: Query<&mut Text, With<UpText>>,
|
|
) {
|
|
let mut text = text_query.single_mut();
|
|
let velocity = state_query.single();
|
|
let speed = velocity.0.length();
|
|
text.0 = format!("spd: {:.2}", speed);
|
|
}
|
|
|
|
pub struct CyberUIPlugin;
|
|
|
|
impl Plugin for CyberUIPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.add_systems(Update, update_ui);
|
|
}
|
|
}
|