use avian3d::prelude::LinearVelocity;
use bevy::{
color::{self, palettes::css::RED},
prelude::*,
};
use crate::player::Player;
#[derive(Component)]
pub struct VelocityText;
pub fn set_up_velocity_txt(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn((
TextBundle::from_section(
"0.0 m/s",
TextStyle {
font_size: 50.0,
..default()
},
) .with_text_justify(JustifyText::Center)
.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() + 8.0 * xform.right();
let arrow_end = arrow_start + velocity.0.normalize() * 4.0;
gizmos.arrow(arrow_start, arrow_end, RED);
}
}