diff --git a/assets/fonts/FiraMono-Medium.ttf b/assets/fonts/FiraMono-Medium.ttf new file mode 100644 index 0000000..1e95ced Binary files /dev/null and b/assets/fonts/FiraMono-Medium.ttf differ diff --git a/src/hud.rs b/src/hud.rs index 1c3d8fd..8ce8305 100644 --- a/src/hud.rs +++ b/src/hud.rs @@ -1,7 +1,9 @@ +use crate::player::PlayerMarker; use bevy::{ diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin}, prelude::*, }; +use bevy_rapier3d::{physics::RigidBodyHandleComponent, rapier::dynamics::RigidBodySet}; pub struct HudPlugin; @@ -10,12 +12,13 @@ impl Plugin for HudPlugin { app .add_plugin(FrameTimeDiagnosticsPlugin::default()) .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 SpeedText; fn fps_update_system(diagnostics: Res, mut query: Query<&mut Text, With>) { for mut text in query.iter_mut() { @@ -27,11 +30,28 @@ fn fps_update_system(diagnostics: Res, mut query: Query<&mut Text, } } +fn speed_update_system( + rigid_body_set: Res, + mut text_query: Query<&mut Text, With>, + player_query: Query<&RigidBodyHandleComponent, With>, +) { + 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) { commands // UI camera .spawn(CameraUiBundle::default()) - // texture + // fps display .spawn(TextBundle { style: Style { align_self: AlignSelf::FlexEnd, @@ -54,5 +74,30 @@ fn setup(commands: &mut Commands, asset_server: Res) { }, ..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); } diff --git a/src/player.rs b/src/player.rs index 7ffc50f..cee029b 100644 --- a/src/player.rs +++ b/src/player.rs @@ -21,7 +21,7 @@ impl Plugin for PlayerPlugin { } #[derive(Default)] -struct PlayerMarker; +pub struct PlayerMarker; #[derive(Default, Debug)] struct CameraData {