diff --git a/src/lib.rs b/src/lib.rs index 670ae95..1a9c51a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,6 @@ use std::f32::consts::FRAC_PI_2; use bevy::{ diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin}, - ecs::system::SystemParam, pbr::NotShadowCaster, prelude::*, }; @@ -220,7 +219,10 @@ fn recompute_view_distance( } } -fn get_main_camera_position(player: Player, view_distance: Res) -> (TileIndex, Vec2) { +fn get_main_camera_position( + player: player::Player, + view_distance: Res, +) -> (TileIndex, Vec2) { let player = player.pos(); let pos = player.pos(); @@ -235,61 +237,7 @@ fn get_main_camera_position(player: Player, view_distance: Res) -> #[derive(Component)] struct Compass; -#[derive(SystemParam)] -struct Player<'w, 's> { - xr_pos: Query< - 'w, - 's, - (&'static Transform, &'static GalacticGrid), - (With, Without, Without), - >, - flycam_pos: Query< - 'w, - 's, - (&'static Transform, &'static GalacticGrid), - (With, Without, Without), - >, - space: Res<'w, FloatingOriginSettings>, -} - -struct PlayerPosition<'a> { - transform: Transform, - grid: GalacticGrid, - space: &'a FloatingOriginSettings, -} - -impl PlayerPosition<'_> { - pub fn pos(&self) -> DVec3 { - self.space.grid_position_double(&self.grid, &self.transform) - } - pub fn directions(&self) -> Directions { - let up = self.pos().normalize().as_vec3(); - let west = Vec3::Z.cross(up); - let north = up.cross(west); - Directions { up, north, west } - } -} - -struct Directions { - up: Vec3, - north: Vec3, - west: Vec3, -} - -impl<'w, 's> Player<'w, 's> { - pub fn pos(&self) -> PlayerPosition<'_> { - let (&transform, &grid) = if let Ok(xr_pos) = self.xr_pos.get_single() { - xr_pos - } else { - self.flycam_pos.single() - }; - PlayerPosition { - transform, - grid, - space: &self.space, - } - } -} +mod player; fn reposition_compass( mut compass: Query< @@ -300,7 +248,7 @@ fn reposition_compass( mut meshes: ResMut>, mut materials: ResMut>, server: Res, - player: Player, + player: player::Player, ) { if let Ok((mut pos, mut grid)) = compass.get_single_mut() { let player = player.pos(); diff --git a/src/player.rs b/src/player.rs new file mode 100644 index 0000000..180ee94 --- /dev/null +++ b/src/player.rs @@ -0,0 +1,64 @@ +use super::Compass; +use super::GalacticGrid; +use bevy::ecs::system::SystemParam; +use bevy::prelude::*; +use bevy_flycam::FlyCam; +use bevy_oxr::xr_input::trackers::OpenXRTrackingRoot; +use big_space::FloatingOriginSettings; +use glam::DVec3; + +#[derive(SystemParam)] +pub struct Player<'w, 's> { + pub(crate) xr_pos: Query< + 'w, + 's, + (&'static Transform, &'static GalacticGrid), + (With, Without, Without), + >, + pub(crate) flycam_pos: Query< + 'w, + 's, + (&'static Transform, &'static GalacticGrid), + (With, Without, Without), + >, + pub(crate) space: Res<'w, FloatingOriginSettings>, +} + +pub struct PlayerPosition<'a> { + pub transform: Transform, + pub grid: GalacticGrid, + pub space: &'a FloatingOriginSettings, +} + +impl PlayerPosition<'_> { + pub fn pos(&self) -> DVec3 { + self.space.grid_position_double(&self.grid, &self.transform) + } + pub fn directions(&self) -> Directions { + let up = self.pos().normalize().as_vec3(); + let west = Vec3::Z.cross(up); + let north = up.cross(west); + Directions { up, north, west } + } +} + +pub struct Directions { + pub up: Vec3, + pub north: Vec3, + pub west: Vec3, +} + +impl<'w, 's> Player<'w, 's> { + pub fn pos(&self) -> PlayerPosition<'_> { + let (&transform, &grid) = if let Ok(xr_pos) = self.xr_pos.get_single() { + xr_pos + } else { + self.flycam_pos.single() + }; + PlayerPosition { + transform, + grid, + space: &self.space, + } + } +}