Skip to content

Commit

Permalink
add and use KDNode instead of Orbitable for nearest neighbour mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
thombruce committed Oct 10, 2023
1 parent 68bfb83 commit 4434b80
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 9 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]

### Added

- Add and use KDNode instead of Orbitable for nearest neighbour mapping

### Changed

- Separate nav and speedometer UI features into own modules
Expand Down
5 changes: 2 additions & 3 deletions src/astronomy/orbit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::Duration;
use bevy::prelude::*;
use bevy_spatial::{AutomaticUpdate, SpatialStructure};

use crate::resources::{game_time::GameTime, state::GameState};
use crate::resources::{game_time::GameTime, spatial::KDNode, state::GameState};

const ORBITAL_PERIOD_SCALING_FACTOR: f32 = 1.0;

Expand Down Expand Up @@ -31,8 +31,7 @@ pub struct OrbitPlugin;
impl Plugin for OrbitPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(
// TODO: Replace use of Orbitable in KDTree with KDNode for reusability.
AutomaticUpdate::<Orbitable>::new()
AutomaticUpdate::<KDNode>::new()
.with_spatial_ds(SpatialStructure::KDTree2)
.with_frequency(Duration::from_millis(1)),
);
Expand Down
3 changes: 3 additions & 0 deletions src/astronomy/planet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bevy::prelude::*;
use crate::{
effects::animate::{AnimationBundle, AnimationIndices, AnimationTimer},
hud::indicator::Indicated,
resources::spatial::KDNode,
};

use super::orbit::{Orbit, Orbitable};
Expand All @@ -24,6 +25,7 @@ pub struct PlanetBundle {
pub orbit: Orbit,
pub animation_bundle: AnimationBundle,
pub sprite_sheet_bundle: SpriteSheetBundle,
pub kd_node: KDNode,
}
impl Default for PlanetBundle {
fn default() -> Self {
Expand All @@ -43,6 +45,7 @@ impl Default for PlanetBundle {
timer: AnimationTimer(Timer::from_seconds(0.1, TimerMode::Repeating)),
},
sprite_sheet_bundle: SpriteSheetBundle::default(),
kd_node: KDNode,
}
}
}
4 changes: 3 additions & 1 deletion src/astronomy/star.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy::prelude::*;
use crate::{
effects::animate::{AnimationBundle, AnimationIndices, AnimationTimer},
hud::indicator::Indicated,
resources::assets::SpriteAssets,
resources::{assets::SpriteAssets, spatial::KDNode},
};

use super::orbit::Orbitable;
Expand All @@ -24,6 +24,7 @@ pub struct StarBundle {
pub orbitable: Orbitable,
pub animation_bundle: AnimationBundle,
pub sprite_sheet_bundle: SpriteSheetBundle,
pub kd_node: KDNode,
}
impl Default for StarBundle {
fn default() -> Self {
Expand All @@ -39,6 +40,7 @@ impl Default for StarBundle {
timer: AnimationTimer(Timer::from_seconds(0.1, TimerMode::Repeating)),
},
sprite_sheet_bundle: SpriteSheetBundle::default(),
kd_node: KDNode,
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/hud/nav.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy::{math::Vec3Swizzles, prelude::*};
use bevy_spatial::{kdtree::KDTree2, SpatialAccess};

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

/// UI Location component
#[derive(Component)]
Expand Down Expand Up @@ -46,18 +46,18 @@ impl NavBundle {
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>>,
tree: Res<KDTree2<KDNode>>,
nodes: Query<&Name, With<KDNode>>,
) {
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());
let node = nodes.get(entity.unwrap());

for mut text in query.iter_mut() {
text.sections[0].value = format!("Near {}", orbitable.unwrap());
text.sections[0].value = format!("Near {}", node.unwrap());
}
}
}
1 change: 1 addition & 0 deletions src/resources/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod assets;
pub mod camera;
pub mod game_time;
pub mod spatial;
pub mod state;
4 changes: 4 additions & 0 deletions src/resources/spatial.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use bevy::prelude::*;

#[derive(Component)]
pub struct KDNode;

0 comments on commit 4434b80

Please sign in to comment.