Skip to content

Commit

Permalink
separate nav and speedometer UI features into own modules
Browse files Browse the repository at this point in the history
  • Loading branch information
thombruce committed Oct 10, 2023
1 parent ffdcfbf commit 68bfb83
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 98 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Separate nav and speedometer UI features into own modules

## [0.0.13] - 2023-10-10

### Added
Expand Down
107 changes: 9 additions & 98 deletions src/hud/mod.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
use bevy::{math::Vec3Swizzles, prelude::*};
use bevy_rapier2d::prelude::Velocity;
use bevy_spatial::{kdtree::KDTree2, SpatialAccess};
use bevy::prelude::*;

use crate::{
astronomy::orbit::Orbitable, resources::assets::UiAssets, resources::state::GameState,
ship::Ship,
};
use crate::{resources::assets::UiAssets, resources::state::GameState};

pub mod indicator;
pub mod nav;
pub mod speedometer;

use indicator::IndicatorPlugin;
use nav::current_location;
use speedometer::hud_speedometer;

/// UI Speed component
#[derive(Component)]
pub struct UISpeed {}

/// UI Location component
#[derive(Component)]
pub struct UILocation {}
use self::{nav::NavBundle, speedometer::SpeedometerBundle};

pub struct HudPlugin;
impl Plugin for HudPlugin {
Expand Down Expand Up @@ -49,89 +42,7 @@ fn setup(mut commands: Commands, ui: Res<UiAssets>) {
Name::new("HUD"),
))
.with_children(|parent| {
parent.spawn((
TextBundle {
style: Style {
margin: UiRect {
left: Val::Px(10.0),
right: Val::Px(10.0),
top: Val::Px(10.0),
bottom: Val::Px(10.0),
},
..default()
},
text: Text::from_section(
"In Space",
TextStyle {
font: ui.font.clone(),
font_size: 25.0,
color: Color::rgb_u8(0x00, 0xAA, 0xAA),
..default()
},
),
..default()
},
UILocation {},
Name::new("Location"),
));

parent.spawn((
TextBundle {
style: Style {
margin: UiRect {
left: Val::Px(10.0),
right: Val::Px(10.0),
top: Val::Px(10.0),
bottom: Val::Px(10.0),
},
..default()
},
text: Text::from_section(
"0",
TextStyle {
font: ui.font.clone(),
font_size: 25.0,
color: Color::rgb_u8(0xAA, 0xAA, 0x33),
..default()
},
),
..default()
},
UISpeed {},
Name::new("Speedometer"),
));
parent.spawn(NavBundle::use_font(ui.font.clone()));
parent.spawn(SpeedometerBundle::use_font(ui.font.clone()));
});
}

pub fn hud_speedometer(
mut query: Query<&mut Text, With<UISpeed>>,
mut player: Query<&Velocity, With<Ship>>,
) {
let velocity = player.single_mut();

for mut text in query.iter_mut() {
text.sections[0].value = format!(
"{} m/s",
((velocity.linvel.x.powf(2.0) + velocity.linvel.y.powf(2.0)).sqrt()).trunc()
);
}
}

pub fn current_location(
mut query: Query<&mut Text, With<UILocation>>,
player: Query<&Transform, With<Ship>>,
tree: Res<KDTree2<Orbitable>>,
orbitables: Query<&Name, With<Orbitable>>,
) {
let ship_transform = player.single();

let player_translation = ship_transform.translation.xy();

if let Some((_pos, entity)) = tree.nearest_neighbour(player_translation) {
let orbitable = orbitables.get(entity.unwrap());

for mut text in query.iter_mut() {
text.sections[0].value = format!("Near {}", orbitable.unwrap());
}
}
}
63 changes: 63 additions & 0 deletions src/hud/nav.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use bevy::{math::Vec3Swizzles, prelude::*};
use bevy_spatial::{kdtree::KDTree2, SpatialAccess};

use crate::{astronomy::orbit::Orbitable, ship::Ship};

/// UI Location component
#[derive(Component)]
pub struct UILocation;

#[derive(Bundle)]
pub struct NavBundle {
text: TextBundle,
marker: UILocation,
name: Name,
}
impl NavBundle {
pub fn use_font(font: Handle<Font>) -> Self {
Self {
text: TextBundle {
style: Style {
margin: UiRect {
left: Val::Px(10.0),
right: Val::Px(10.0),
top: Val::Px(10.0),
bottom: Val::Px(10.0),
},
..default()
},
text: Text::from_section(
"In Space",
TextStyle {
font: font,
font_size: 25.0,
color: Color::rgb_u8(0x00, 0xAA, 0xAA),
..default()
},
),
..default()
},
marker: UILocation,
name: Name::new("Location"),
}
}
}

pub fn current_location(
mut query: Query<&mut Text, With<UILocation>>,
player: Query<&Transform, With<Ship>>,
tree: Res<KDTree2<Orbitable>>,
orbitables: Query<&Name, With<Orbitable>>,
) {
let ship_transform = player.single();

let player_translation = ship_transform.translation.xy();

if let Some((_pos, entity)) = tree.nearest_neighbour(player_translation) {
let orbitable = orbitables.get(entity.unwrap());

for mut text in query.iter_mut() {
text.sections[0].value = format!("Near {}", orbitable.unwrap());
}
}
}
58 changes: 58 additions & 0 deletions src/hud/speedometer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use bevy::prelude::*;
use bevy_rapier2d::prelude::Velocity;

use crate::ship::Ship;

/// UI Speed component
#[derive(Component)]
pub struct UISpeed;

#[derive(Bundle)]
pub struct SpeedometerBundle {
text: TextBundle,
marker: UISpeed,
name: Name,
}
impl SpeedometerBundle {
pub fn use_font(font: Handle<Font>) -> Self {
Self {
text: TextBundle {
style: Style {
margin: UiRect {
left: Val::Px(10.0),
right: Val::Px(10.0),
top: Val::Px(10.0),
bottom: Val::Px(10.0),
},
..default()
},
text: Text::from_section(
"0",
TextStyle {
font: font,
font_size: 25.0,
color: Color::rgb_u8(0xAA, 0xAA, 0x33),
..default()
},
),
..default()
},
marker: UISpeed,
name: Name::new("Speedometer"),
}
}
}

pub fn hud_speedometer(
mut query: Query<&mut Text, With<UISpeed>>,
mut player: Query<&Velocity, With<Ship>>,
) {
let velocity = player.single_mut();

for mut text in query.iter_mut() {
text.sections[0].value = format!(
"{} m/s",
((velocity.linvel.x.powf(2.0) + velocity.linvel.y.powf(2.0)).sqrt()).trunc()
);
}
}

0 comments on commit 68bfb83

Please sign in to comment.