-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate nav and speedometer UI features into own modules
- Loading branch information
Showing
4 changed files
with
134 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
} |