-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
70 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<OpenXRTrackingRoot>, Without<FlyCam>, Without<Compass>), | ||
>, | ||
pub(crate) flycam_pos: Query< | ||
'w, | ||
's, | ||
(&'static Transform, &'static GalacticGrid), | ||
(With<FlyCam>, Without<OpenXRTrackingRoot>, Without<Compass>), | ||
>, | ||
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, | ||
} | ||
} | ||
} |