Display movement speed in gui
This commit is contained in:
parent
edba5d013d
commit
e0ceb680b4
3 changed files with 50 additions and 5 deletions
BIN
assets/fonts/FiraMono-Medium.ttf
Normal file
BIN
assets/fonts/FiraMono-Medium.ttf
Normal file
Binary file not shown.
53
src/hud.rs
53
src/hud.rs
|
@ -1,7 +1,9 @@
|
||||||
|
use crate::player::PlayerMarker;
|
||||||
use bevy::{
|
use bevy::{
|
||||||
diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin},
|
diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
};
|
};
|
||||||
|
use bevy_rapier3d::{physics::RigidBodyHandleComponent, rapier::dynamics::RigidBodySet};
|
||||||
|
|
||||||
pub struct HudPlugin;
|
pub struct HudPlugin;
|
||||||
|
|
||||||
|
@ -10,12 +12,13 @@ impl Plugin for HudPlugin {
|
||||||
app
|
app
|
||||||
.add_plugin(FrameTimeDiagnosticsPlugin::default())
|
.add_plugin(FrameTimeDiagnosticsPlugin::default())
|
||||||
.add_startup_system(setup.system())
|
.add_startup_system(setup.system())
|
||||||
.add_system(fps_update_system.system());
|
.add_system(fps_update_system.system())
|
||||||
|
.add_system(speed_update_system.system());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// A unit struct to help identify the FPS UI component, since there may be many Text components
|
|
||||||
struct FpsText;
|
struct FpsText;
|
||||||
|
struct SpeedText;
|
||||||
|
|
||||||
fn fps_update_system(diagnostics: Res<Diagnostics>, mut query: Query<&mut Text, With<FpsText>>) {
|
fn fps_update_system(diagnostics: Res<Diagnostics>, mut query: Query<&mut Text, With<FpsText>>) {
|
||||||
for mut text in query.iter_mut() {
|
for mut text in query.iter_mut() {
|
||||||
|
@ -27,11 +30,28 @@ fn fps_update_system(diagnostics: Res<Diagnostics>, mut query: Query<&mut Text,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn speed_update_system(
|
||||||
|
rigid_body_set: Res<RigidBodySet>,
|
||||||
|
mut text_query: Query<&mut Text, With<SpeedText>>,
|
||||||
|
player_query: Query<&RigidBodyHandleComponent, With<PlayerMarker>>,
|
||||||
|
) {
|
||||||
|
for player in player_query.iter() {
|
||||||
|
// TODO: would be better to calculate this in a function in player somehow
|
||||||
|
let player_body = rigid_body_set.get(player.handle());
|
||||||
|
let player_velocity = player_body.unwrap().linvel();
|
||||||
|
let player_speed = Vec3::new(player_velocity.x, 0.0, player_velocity.z).length();
|
||||||
|
|
||||||
|
for mut text in text_query.iter_mut() {
|
||||||
|
text.value = format!("{} m/s", player_speed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn setup(commands: &mut Commands, asset_server: Res<AssetServer>) {
|
fn setup(commands: &mut Commands, asset_server: Res<AssetServer>) {
|
||||||
commands
|
commands
|
||||||
// UI camera
|
// UI camera
|
||||||
.spawn(CameraUiBundle::default())
|
.spawn(CameraUiBundle::default())
|
||||||
// texture
|
// fps display
|
||||||
.spawn(TextBundle {
|
.spawn(TextBundle {
|
||||||
style: Style {
|
style: Style {
|
||||||
align_self: AlignSelf::FlexEnd,
|
align_self: AlignSelf::FlexEnd,
|
||||||
|
@ -54,5 +74,30 @@ fn setup(commands: &mut Commands, asset_server: Res<AssetServer>) {
|
||||||
},
|
},
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
.with(FpsText);
|
.with(FpsText)
|
||||||
|
// speed display
|
||||||
|
.spawn(TextBundle {
|
||||||
|
style: Style {
|
||||||
|
// TODO: dont know how to properly center this horizontally
|
||||||
|
align_self: AlignSelf::Center,
|
||||||
|
position_type: PositionType::Relative,
|
||||||
|
position: Rect {
|
||||||
|
top: Val::Px(50.0),
|
||||||
|
right: Val::Px(-480.0),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
text: Text {
|
||||||
|
value: "speed".to_string(),
|
||||||
|
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
|
||||||
|
style: TextStyle {
|
||||||
|
font_size: 16.0,
|
||||||
|
color: Color::rgba(1.0, 1.0, 1.0, 0.5),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
},
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.with(SpeedText);
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ impl Plugin for PlayerPlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct PlayerMarker;
|
pub struct PlayerMarker;
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Debug)]
|
||||||
struct CameraData {
|
struct CameraData {
|
||||||
|
|
Loading…
Reference in a new issue