use avian3d::prelude::LinearVelocity;
use bevy::{
color::{self, palettes::css::RED},
prelude::*,
};
use crate::player::Player;
// A unit struct to help identify the color-changing Text component
#[derive(Component)]
pub struct VelocityText;
pub fn set_up_velocity_txt(mut commands: Commands, asset_server: Res<AssetServer>) {
// UI camera
commands.spawn(Camera2dBundle::default());
// Text with one section
commands.spawn((
// Create a TextBundle that has a Text with a single section.
TextBundle::from_section(
// Accepts a `String` or any type that converts into a `String`, such as `&str`
"0.0 m/s",
TextStyle {
font_size: 50.0,
..default()
},
) // Set the justification of the Text
.with_text_justify(JustifyText::Center)
// Set the style of the TextBundle itself.
.with_style(Style {
position_type: PositionType::Absolute,
bottom: Val::Px(5.0),
right: Val::Px(5.0),
..default()
}),
VelocityText,
));
}
pub fn update_velocity_txt(
velocity_query: Query<(&Player, &LinearVelocity)>,
mut txt_query: Query<(&VelocityText, &mut Text)>,
) {
let (_, vel) = velocity_query.single();
let (_, mut txt) = txt_query.single_mut();
txt.sections[0].value = format!("{:.1} m/s", vel.length());
}
pub fn update_velocity_arrow(
mut gizmos: Gizmos,
query: Query<(&Player, &LinearVelocity, &Transform)>,
) {
let (__, velocity, xform) = query.single();
if velocity.length() > 0.5 {
let arrow_start = xform.translation + 2.0 * xform.up() + 10.0 * xform.right();
let arrow_end = arrow_start + velocity.0.normalize() * 10.0;
gizmos.arrow(arrow_start, arrow_end, RED);
}
}