diff --git a/starmap/src/main.rs b/starmap/src/main.rs index 0850cb9..8ec05b1 100644 --- a/starmap/src/main.rs +++ b/starmap/src/main.rs @@ -50,7 +50,7 @@ fn main() { app.insert_resource(systems::Spyglass { radius: 50., fetch: true, - // filter: true, + filter: true, }); app.insert_resource(systems::Fetched(HashSet::new())); @@ -63,6 +63,7 @@ fn main() { app.add_event::(); app.add_systems(Update, systems::fetch); app.add_systems(Update, systems::spawn.after(camera::move_camera)); + app.add_systems(Update, systems::visibility.after(systems::spawn)); app.add_systems(Update, systems::labels::respawn.after(systems::spawn)); app.add_systems( Update, diff --git a/starmap/src/systems/fetch.rs b/starmap/src/systems/fetch.rs index 0b223f8..d6fbd48 100644 --- a/starmap/src/systems/fetch.rs +++ b/starmap/src/systems/fetch.rs @@ -1,4 +1,4 @@ -use super::fetch_route; +use super::{fetch_route, Spyglass}; use crate::{search::Searched, Db}; use bevy::prelude::*; use bevy::tasks::{AsyncComputeTaskPool, Task}; @@ -35,14 +35,6 @@ pub struct FetchTasks { #[derive(Resource)] pub struct Fetched(pub HashSet); -/// A global setting which controls the spyglass around the camera -#[derive(Resource)] -pub struct Spyglass { - pub fetch: bool, - pub radius: f64, - // pub filter: bool, -} - /// Spawns tasks to load star systems from the DB pub fn fetch( camera_query: Query<&mut PanOrbitCamera>, @@ -112,7 +104,7 @@ fn fetch_around_camera( let radius = spyglass.radius; let task = task_pool.spawn(async move { let cent = [center.x as f64, center.y as f64, center.z as f64]; - DbSystem::fetch_in_range_of_point(&db, radius, cent) + DbSystem::fetch_in_range_of_point(&db, radius as f64, cent) .await .unwrap_or_default() }); diff --git a/starmap/src/systems/mod.rs b/starmap/src/systems/mod.rs index 494caeb..3856446 100644 --- a/starmap/src/systems/mod.rs +++ b/starmap/src/systems/mod.rs @@ -1,4 +1,5 @@ use bevy::prelude::*; +use bevy_panorbit_camera::PanOrbitCamera; use elite_journal::Allegiance; use galos_db::systems::System as DbSystem; @@ -19,12 +20,47 @@ pub use self::scale::*; mod spawn; pub use self::spawn::*; -pub(crate) mod labels; +pub mod labels; mod route; pub use self::route::*; -pub(crate) fn system_to_vec(system: &DbSystem) -> Vec3 { +/// A global setting which controls the spyglass around the camera +#[derive(Resource)] +pub struct Spyglass { + pub fetch: bool, + pub radius: f32, + pub filter: bool, +} + +pub fn visibility( + mut commands: Commands, + camera: Query<&PanOrbitCamera>, + systems: Query<(Entity, &Transform), With>, + spyglass: Res, +) { + // Make sure we make systems visible again. + if spyglass.is_changed() && !spyglass.filter { + for (entity, _) in &systems { + commands.entity(entity).insert(Visibility::Visible); + } + } + + if spyglass.filter { + let camera_translation = camera.single().target_focus; + for (entity, system_transform) in &systems { + let dist = + camera_translation.distance(system_transform.translation); + if dist <= spyglass.radius { + commands.entity(entity).insert(Visibility::Visible); + } else { + commands.entity(entity).insert(Visibility::Hidden); + } + } + } +} + +pub fn system_to_vec(system: &DbSystem) -> Vec3 { Vec3::new( system.position.unwrap().x as f32, system.position.unwrap().y as f32, diff --git a/starmap/src/systems/spawn.rs b/starmap/src/systems/spawn.rs index 4578cc3..4e6390b 100644 --- a/starmap/src/systems/spawn.rs +++ b/starmap/src/systems/spawn.rs @@ -82,7 +82,7 @@ pub fn spawn( } /// Generate all the star system entities. -pub(crate) fn spawn_systems( +pub fn spawn_systems( new_systems: &[DbSystem], systems_query: &Query<(Entity, &System)>, color_by: &Res, diff --git a/starmap/src/ui.rs b/starmap/src/ui.rs index 79d9910..f9d2f73 100644 --- a/starmap/src/ui.rs +++ b/starmap/src/ui.rs @@ -100,8 +100,8 @@ fn settings( ui.add_space(2.); ui.checkbox(&mut spyglass.fetch, "Load Systems from DB"); ui.add_space(2.); - // ui.checkbox(&mut spyglass.filter, "Hide Systems Outside Spyglass"); - // ui.add_space(2.); + ui.checkbox(&mut spyglass.filter, "Spyglass Filter"); + ui.add_space(2.); if ui.button("Despawn Systems").clicked() { despawner.send(Despawn); }