Skip to content

Commit

Permalink
Move Player to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Dec 8, 2023
1 parent 316814b commit 0fdf7ec
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 58 deletions.
64 changes: 6 additions & 58 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::f32::consts::FRAC_PI_2;

use bevy::{
diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin},
ecs::system::SystemParam,
pbr::NotShadowCaster,
prelude::*,
};
Expand Down Expand Up @@ -220,7 +219,10 @@ fn recompute_view_distance(
}
}

fn get_main_camera_position(player: Player, view_distance: Res<ViewDistance>) -> (TileIndex, Vec2) {
fn get_main_camera_position(
player: player::Player,
view_distance: Res<ViewDistance>,
) -> (TileIndex, Vec2) {
let player = player.pos();

let pos = player.pos();
Expand All @@ -235,61 +237,7 @@ fn get_main_camera_position(player: Player, view_distance: Res<ViewDistance>) ->
#[derive(Component)]
struct Compass;

#[derive(SystemParam)]
struct Player<'w, 's> {
xr_pos: Query<
'w,
's,
(&'static Transform, &'static GalacticGrid),
(With<OpenXRTrackingRoot>, Without<FlyCam>, Without<Compass>),
>,
flycam_pos: Query<
'w,
's,
(&'static Transform, &'static GalacticGrid),
(With<FlyCam>, Without<OpenXRTrackingRoot>, Without<Compass>),
>,
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<
Expand All @@ -300,7 +248,7 @@ fn reposition_compass(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
server: Res<AssetServer>,
player: Player,
player: player::Player,
) {
if let Ok((mut pos, mut grid)) = compass.get_single_mut() {
let player = player.pos();
Expand Down
64 changes: 64 additions & 0 deletions src/player.rs
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,
}
}
}

0 comments on commit 0fdf7ec

Please sign in to comment.