diff --git a/CHANGELOG.md b/CHANGELOG.md index 66e8fd1..e92e55e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/astronomy/orbit.rs b/src/astronomy/orbit.rs index 403fd99..9f36fef 100644 --- a/src/astronomy/orbit.rs +++ b/src/astronomy/orbit.rs @@ -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; @@ -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::::new() + AutomaticUpdate::::new() .with_spatial_ds(SpatialStructure::KDTree2) .with_frequency(Duration::from_millis(1)), ); diff --git a/src/astronomy/planet.rs b/src/astronomy/planet.rs index 0bb36be..2e553c2 100644 --- a/src/astronomy/planet.rs +++ b/src/astronomy/planet.rs @@ -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}; @@ -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 { @@ -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, } } } diff --git a/src/astronomy/star.rs b/src/astronomy/star.rs index a2f62d1..556fdfa 100644 --- a/src/astronomy/star.rs +++ b/src/astronomy/star.rs @@ -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; @@ -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 { @@ -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, } } } diff --git a/src/hud/nav.rs b/src/hud/nav.rs index 58b881f..0bc3510 100644 --- a/src/hud/nav.rs +++ b/src/hud/nav.rs @@ -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)] @@ -46,18 +46,18 @@ impl NavBundle { pub fn current_location( mut query: Query<&mut Text, With>, player: Query<&Transform, With>, - tree: Res>, - orbitables: Query<&Name, With>, + tree: Res>, + nodes: Query<&Name, With>, ) { 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()); } } } diff --git a/src/resources/mod.rs b/src/resources/mod.rs index 0c64ece..457d13b 100644 --- a/src/resources/mod.rs +++ b/src/resources/mod.rs @@ -1,4 +1,5 @@ pub mod assets; pub mod camera; pub mod game_time; +pub mod spatial; pub mod state; diff --git a/src/resources/spatial.rs b/src/resources/spatial.rs new file mode 100644 index 0000000..cbe0bd2 --- /dev/null +++ b/src/resources/spatial.rs @@ -0,0 +1,4 @@ +use bevy::prelude::*; + +#[derive(Component)] +pub struct KDNode;