Make relative direction presentation configurable.

This commit is contained in:
Nolan Darilek 2021-11-29 12:29:45 -06:00
parent 07a4bd9d64
commit 91b058995c
2 changed files with 106 additions and 15 deletions

View File

@ -31,7 +31,9 @@ futures-lite = "1"
gilrs = "0.8" gilrs = "0.8"
mapgen = "0.5" mapgen = "0.5"
maze_generator = "1" maze_generator = "1"
once_cell = "1"
pathfinding = "2" pathfinding = "2"
rand = "0.8" rand = "0.8"
sentry = "0.23" sentry = "0.23"
serde = "1"
shadowcast = "0.8" shadowcast = "0.8"

View File

@ -4,12 +4,15 @@ use std::{
fmt::Display, fmt::Display,
marker::PhantomData, marker::PhantomData,
ops::{Add, AddAssign, Sub, SubAssign}, ops::{Add, AddAssign, Sub, SubAssign},
sync::RwLock,
}; };
use bevy::{core::FloatOrd, ecs::query::WorldQuery, prelude::*, transform::TransformSystem}; use bevy::{core::FloatOrd, ecs::query::WorldQuery, prelude::*, transform::TransformSystem};
use bevy_rapier2d::prelude::*; use bevy_rapier2d::prelude::*;
use derive_more::{Deref, DerefMut}; use derive_more::{Deref, DerefMut};
use once_cell::sync::Lazy;
use rand::prelude::*; use rand::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Default, Deref, DerefMut, PartialEq, PartialOrd, Reflect)] #[derive(Clone, Copy, Debug, Default, Deref, DerefMut, PartialEq, PartialOrd, Reflect)]
#[reflect(Component)] #[reflect(Component)]
@ -321,6 +324,17 @@ impl Display for CardinalDirection {
} }
} }
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum RelativeDirectionMode {
ClockFacing,
Directional,
}
static RELATIVE_DIRECTION_MODE: Lazy<RwLock<RelativeDirectionMode>> = Lazy::new(|| {
let v = RelativeDirectionMode::Directional;
RwLock::new(v)
});
pub trait PointLike { pub trait PointLike {
fn x(&self) -> f32; fn x(&self) -> f32;
@ -387,23 +401,85 @@ pub trait PointLike {
if distance > 0 { if distance > 0 {
let tile_or_tiles = if distance == 1 { "tile" } else { "tiles" }; let tile_or_tiles = if distance == 1 { "tile" } else { "tiles" };
let direction: String = if let Some(yaw) = yaw { let direction: String = if let Some(yaw) = yaw {
let mode = RELATIVE_DIRECTION_MODE.read().unwrap();
let bearing = self.bearing(other); let bearing = self.bearing(other);
let relative = (bearing - yaw).degrees(); let relative = (bearing - yaw).degrees();
match relative { match relative {
v if v <= 15. => "ahead".into(), v if v <= 15. => "ahead",
v if v <= 45. => "11:00".into(), v if v <= 45. => {
v if v <= 75. => "10:00".into(), if *mode == RelativeDirectionMode::ClockFacing {
v if v <= 105. => "9:00".into(), "11:00"
v if v <= 135. => "8:00".into(), } else {
v if v <= 165. => "7:00".into(), "ahead and left"
v if v <= 195. => "behind".into(),
v if v <= 225. => "5:00".into(),
v if v <= 255. => "4:00".into(),
v if v <= 285. => "3:00".into(),
v if v <= 315. => "2:00".into(),
v if v <= 345. => "1:00".into(),
_ => "ahead".into(),
} }
}
v if v <= 75. => {
if *mode == RelativeDirectionMode::ClockFacing {
"10:00"
} else {
"left and ahead"
}
}
v if v <= 105. => {
if *mode == RelativeDirectionMode::ClockFacing {
"9:00"
} else {
"left"
}
}
v if v <= 135. => {
if *mode == RelativeDirectionMode::ClockFacing {
"8:00"
} else {
"left and behind"
}
}
v if v <= 165. => {
if *mode == RelativeDirectionMode::ClockFacing {
"7:00"
} else {
"behind and left"
}
}
v if v <= 195. => "behind",
v if v <= 225. => {
if *mode == RelativeDirectionMode::ClockFacing {
"5:00"
} else {
"behind and right"
}
}
v if v <= 255. => {
if *mode == RelativeDirectionMode::ClockFacing {
"4:00"
} else {
"right and behind"
}
}
v if v <= 285. => {
if *mode == RelativeDirectionMode::ClockFacing {
"3:00"
} else {
"right"
}
}
v if v <= 315. => {
if *mode == RelativeDirectionMode::ClockFacing {
"2:00"
} else {
"right and ahead"
}
}
v if v <= 345. => {
if *mode == RelativeDirectionMode::ClockFacing {
"1:00"
} else {
"ahead and right"
}
}
_ => "ahead",
}
.into()
} else { } else {
self.direction(other).into() self.direction(other).into()
}; };
@ -571,6 +647,8 @@ where
fn setup(core_config: Res<CoreConfig>, mut rapier_config: ResMut<RapierConfiguration>) { fn setup(core_config: Res<CoreConfig>, mut rapier_config: ResMut<RapierConfiguration>) {
rapier_config.scale = core_config.pixels_per_unit as f32; rapier_config.scale = core_config.pixels_per_unit as f32;
let mut mode = RELATIVE_DIRECTION_MODE.write().unwrap();
*mode = core_config.relative_direction_mode;
} }
fn copy_coordinates_to_transform( fn copy_coordinates_to_transform(
@ -636,12 +714,23 @@ fn copy_collider_position_to_coordinates(
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct CoreConfig { pub struct CoreConfig {
pub relative_direction_mode: RelativeDirectionMode,
pub pixels_per_unit: u8, pub pixels_per_unit: u8,
} }
impl Default for CoreConfig { impl Default for CoreConfig {
fn default() -> Self { fn default() -> Self {
Self { pixels_per_unit: 1 } Self {
relative_direction_mode: RelativeDirectionMode::Directional,
pixels_per_unit: 1,
}
}
}
fn sync_config(config: Res<CoreConfig>) {
if config.is_changed() {
let mut mode = RELATIVE_DIRECTION_MODE.write().unwrap();
*mode = config.relative_direction_mode;
} }
} }
@ -667,7 +756,7 @@ impl Plugin for CorePlugin {
.add_system_to_stage( .add_system_to_stage(
CoreStage::PostUpdate, CoreStage::PostUpdate,
copy_collider_position_to_coordinates.system(), copy_collider_position_to_coordinates.system(),
); ).add_system(sync_config.system());
} }
} }