2023-01-23 05:06:08 +00:00
|
|
|
use bevy::prelude::{
|
|
|
|
AlignSelf, App, AssetServer, Color, Commands, Component, Plugin, Query, Res, Style, Text,
|
2023-02-19 22:21:41 +00:00
|
|
|
TextBundle, TextSection, TextStyle, Transform, With,
|
2023-01-23 05:06:08 +00:00
|
|
|
};
|
2023-01-26 01:18:49 +00:00
|
|
|
#[cfg(feature = "inspector")]
|
2023-01-23 05:06:08 +00:00
|
|
|
use bevy_inspector_egui::quick::WorldInspectorPlugin;
|
|
|
|
use bevy_rapier3d::prelude::Velocity;
|
2022-01-12 08:18:13 +00:00
|
|
|
|
2022-03-13 00:06:36 +00:00
|
|
|
use crate::bike::CyberBikeBody;
|
2022-01-12 08:18:13 +00:00
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct UpText;
|
|
|
|
|
|
|
|
fn setup_ui(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
|
|
commands
|
2023-01-20 22:40:51 +00:00
|
|
|
.spawn(TextBundle {
|
2022-01-12 08:18:13 +00:00
|
|
|
style: Style {
|
|
|
|
align_self: AlignSelf::FlexEnd,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
// Use `Text` directly
|
|
|
|
text: Text {
|
|
|
|
// Construct a `Vec` of `TextSection`s
|
|
|
|
sections: vec![TextSection {
|
|
|
|
value: "".to_string(),
|
|
|
|
style: TextStyle {
|
|
|
|
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
|
|
|
|
font_size: 40.0,
|
|
|
|
color: Color::GOLD,
|
|
|
|
},
|
|
|
|
}],
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.insert(UpText);
|
|
|
|
}
|
|
|
|
|
2022-02-08 21:27:57 +00:00
|
|
|
fn update_ui(
|
2023-02-19 22:21:41 +00:00
|
|
|
state_query: Query<(&Velocity, &Transform), With<CyberBikeBody>>,
|
2022-02-08 21:27:57 +00:00
|
|
|
mut text_query: Query<&mut Text, With<UpText>>,
|
|
|
|
) {
|
2022-01-12 08:18:13 +00:00
|
|
|
let mut text = text_query.single_mut();
|
2023-02-19 22:21:41 +00:00
|
|
|
let (velocity, xform) = state_query.single();
|
|
|
|
let speed = velocity.linvel.dot(xform.forward());
|
|
|
|
text.sections[0].value = format!("spd: {:.2}", speed);
|
2022-01-12 08:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CyberUIPlugin;
|
|
|
|
|
|
|
|
impl Plugin for CyberUIPlugin {
|
|
|
|
fn build(&self, app: &mut App) {
|
2023-01-26 01:18:49 +00:00
|
|
|
#[cfg(feature = "inspector")]
|
|
|
|
app.add_plugin(WorldInspectorPlugin);
|
|
|
|
|
|
|
|
app.add_startup_system(setup_ui).add_system(update_ui);
|
2022-01-12 08:18:13 +00:00
|
|
|
}
|
|
|
|
}
|