Skip to content

Commit

Permalink
Add Spyglass.filter
Browse files Browse the repository at this point in the history
Fixes #29
  • Loading branch information
nixpulvis committed Sep 20, 2024
1 parent cda8c12 commit fbf6e5d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
3 changes: 2 additions & 1 deletion starmap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -63,6 +63,7 @@ fn main() {
app.add_event::<systems::Despawn>();
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,
Expand Down
12 changes: 2 additions & 10 deletions starmap/src/systems/fetch.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -35,14 +35,6 @@ pub struct FetchTasks {
#[derive(Resource)]
pub struct Fetched(pub HashSet<FetchIndex>);

/// 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>,
Expand Down Expand Up @@ -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()
});
Expand Down
40 changes: 38 additions & 2 deletions starmap/src/systems/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy::prelude::*;
use bevy_panorbit_camera::PanOrbitCamera;
use elite_journal::Allegiance;
use galos_db::systems::System as DbSystem;

Expand All @@ -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<System>>,
spyglass: Res<Spyglass>,
) {
// 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,
Expand Down
2 changes: 1 addition & 1 deletion starmap/src/systems/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ColorBy>,
Expand Down
4 changes: 2 additions & 2 deletions starmap/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit fbf6e5d

Please sign in to comment.