From 91b058995c096f3a44dfcc26fe685b19f09ae072 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Mon, 29 Nov 2021 12:29:45 -0600 Subject: [PATCH] Make relative direction presentation configurable. --- Cargo.toml | 2 + src/core.rs | 119 +++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 106 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bceeef3..ab20722 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,9 @@ futures-lite = "1" gilrs = "0.8" mapgen = "0.5" maze_generator = "1" +once_cell = "1" pathfinding = "2" rand = "0.8" sentry = "0.23" +serde = "1" shadowcast = "0.8" diff --git a/src/core.rs b/src/core.rs index 785282e..5e808fe 100644 --- a/src/core.rs +++ b/src/core.rs @@ -4,12 +4,15 @@ use std::{ fmt::Display, marker::PhantomData, ops::{Add, AddAssign, Sub, SubAssign}, + sync::RwLock, }; use bevy::{core::FloatOrd, ecs::query::WorldQuery, prelude::*, transform::TransformSystem}; use bevy_rapier2d::prelude::*; use derive_more::{Deref, DerefMut}; +use once_cell::sync::Lazy; use rand::prelude::*; +use serde::{Deserialize, Serialize}; #[derive(Clone, Copy, Debug, Default, Deref, DerefMut, PartialEq, PartialOrd, Reflect)] #[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> = Lazy::new(|| { + let v = RelativeDirectionMode::Directional; + RwLock::new(v) +}); + pub trait PointLike { fn x(&self) -> f32; @@ -387,23 +401,85 @@ pub trait PointLike { if distance > 0 { let tile_or_tiles = if distance == 1 { "tile" } else { "tiles" }; let direction: String = if let Some(yaw) = yaw { + let mode = RELATIVE_DIRECTION_MODE.read().unwrap(); let bearing = self.bearing(other); let relative = (bearing - yaw).degrees(); match relative { - v if v <= 15. => "ahead".into(), - v if v <= 45. => "11:00".into(), - v if v <= 75. => "10:00".into(), - v if v <= 105. => "9:00".into(), - v if v <= 135. => "8:00".into(), - v if v <= 165. => "7:00".into(), - 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 <= 15. => "ahead", + v if v <= 45. => { + if *mode == RelativeDirectionMode::ClockFacing { + "11:00" + } else { + "ahead and left" + } + } + 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 { self.direction(other).into() }; @@ -571,6 +647,8 @@ where fn setup(core_config: Res, mut rapier_config: ResMut) { 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( @@ -636,12 +714,23 @@ fn copy_collider_position_to_coordinates( #[derive(Clone, Copy, Debug)] pub struct CoreConfig { + pub relative_direction_mode: RelativeDirectionMode, pub pixels_per_unit: u8, } impl Default for CoreConfig { fn default() -> Self { - Self { pixels_per_unit: 1 } + Self { + relative_direction_mode: RelativeDirectionMode::Directional, + pixels_per_unit: 1, + } + } +} + +fn sync_config(config: Res) { + 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( CoreStage::PostUpdate, copy_collider_position_to_coordinates.system(), - ); + ).add_system(sync_config.system()); } }