diff --git a/Cargo.toml b/Cargo.toml index b807f8e..1404622 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,10 +13,43 @@ speech_dispatcher_0_9 = ["bevy_tts/speech_dispatcher_0_9"] speech_dispatcher_0_10 = ["bevy_tts/speech_dispatcher_0_10"] speech_dispatcher_0_11 = ["bevy_tts/speech_dispatcher_0_11"] +[dependencies.bevy] +version = "0.14" +default-features = false +features = [ + "android_shared_stdcxx", + "animation", + "bevy_animation", + "bevy_asset", + "bevy_color", + "bevy_core_pipeline", + "bevy_gilrs", + "bevy_gizmos", + "bevy_gltf", + "bevy_pbr", + "bevy_render", + "bevy_scene", + "bevy_sprite", + "bevy_state", + "bevy_text", + "bevy_ui", + "bevy_winit", + "default_font", + "hdr", + "ktx2", + "multi_threaded", + "png", + "smaa_luts", + "sysinfo_plugin", + "tonemapping_luts", + "webgl2", + "x11", + "zstd", +] + [dependencies] avian2d = "0.1" -bevy = "0.14" -bevy_synthizer = "0.7" +bevy_synthizer = "0.8" bevy_tts = { version = "0.9", default-features = false, features = ["tolk"] } coord_2d = "0.3" here_be_dragons = { version = "0.3", features = ["serde"] } @@ -26,4 +59,4 @@ once_cell = "1" pathfinding = "4" rand = "0.8" serde = "1" -shadowcast = "0.8" \ No newline at end of file +shadowcast = "0.8" diff --git a/src/core.rs b/src/core.rs index 6d03afe..316a288 100644 --- a/src/core.rs +++ b/src/core.rs @@ -2,7 +2,6 @@ use std::{ cmp::{max, min}, f32::consts::PI, fmt::Display, - ops::Sub, sync::RwLock, }; @@ -13,180 +12,100 @@ use avian2d::{ }, prelude::*, }; -use bevy::{app::PluginGroupBuilder, math::FloatOrd, prelude::*}; +use bevy::{ + app::PluginGroupBuilder, + math::{CompassOctant, CompassQuadrant, FloatOrd}, + prelude::*, +}; use once_cell::sync::Lazy; use rand::prelude::*; use serde::{Deserialize, Serialize}; -#[derive(Clone, Copy, Debug, Reflect)] -pub enum Angle { - Degrees(f32), - Radians(f32), +fn relative_desc(rot: &Rot2) -> String { + let mode = RELATIVE_DIRECTION_MODE.read().unwrap(); + match rot.as_radians() { + v if v <= PI / 12. && v > -PI / 12. => "ahead", + v if v <= PI / 4. && v > PI / 12. => { + if *mode == RelativeDirectionMode::ClockFacing { + "11:00" + } else { + "ahead and left" + } + } + v if v <= 3. * PI / 8. && v > PI / 4. => { + if *mode == RelativeDirectionMode::ClockFacing { + "10:00" + } else { + "left and ahead" + } + } + v if v <= 5. * PI / 8. && v > 3. * PI / 8. => "left", + v if v <= 3. * PI / 4. && v > 5. * PI / 8. => { + if *mode == RelativeDirectionMode::ClockFacing { + "8:00" + } else { + "left and behind" + } + } + v if v <= 11. * PI / 12. && v > 3. * PI / 4. => { + if *mode == RelativeDirectionMode::ClockFacing { + "7:00" + } else { + "behind and left" + } + } + v if v <= PI && v > 11. * PI / 12. || v > -PI && v <= -11. * PI / 12. => "behind", + v if v <= -3. * PI / 4. && v > -11. * PI / 12. => { + if *mode == RelativeDirectionMode::ClockFacing { + "5:00" + } else { + "behind and right" + } + } + v if v <= -5. * PI / 8. && v > -3. * PI / 4. => { + if *mode == RelativeDirectionMode::ClockFacing { + "4:00" + } else { + "right and behind" + } + } + v if v <= -3. * PI / 8. && v > -5. * PI / 8. => "right", + v if v <= -PI / 4. && v > -3. * PI / 8. => { + if *mode == RelativeDirectionMode::ClockFacing { + "2:00" + } else { + "right and ahead" + } + } + v if v <= -PI / 12. && v > -PI / 4. => { + if *mode == RelativeDirectionMode::ClockFacing { + "1:00" + } else { + "ahead and right" + } + } + _ => "ahead", + } + .into() } -impl Default for Angle { - fn default() -> Self { - Self::Radians(0.) - } -} +#[derive(Clone, Copy, Debug, Eq, PartialEq, Deref, DerefMut, Reflect)] +pub struct MovementDirection(CompassOctant); -impl Angle { - pub fn degrees(&self) -> f32 { - use Angle::*; - let mut degrees: f32 = match self { - Degrees(v) => *v, - Radians(v) => v.to_degrees(), - }; - while degrees < 0. { - degrees += 360.; - } - while degrees >= 360. { - degrees %= 360.; - } - degrees - } - - pub fn degrees_u32(&self) -> u32 { - self.degrees() as u32 - } - - pub fn radians(&self) -> f32 { - use Angle::*; - match self { - Degrees(v) => v.to_radians(), - Radians(v) => *v, - } - } - - fn relative_desc(&self) -> String { - let mode = RELATIVE_DIRECTION_MODE.read().unwrap(); - match self.degrees() { - 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. => "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. => "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() - } -} - -impl Sub for Angle { - type Output = Self; - - fn sub(self, rhs: Self) -> Self::Output { - match self { - Angle::Degrees(v1) => match rhs { - Angle::Degrees(v2) => Angle::Degrees(v1 - v2), - Angle::Radians(v2) => Angle::Degrees(v1 - v2.to_degrees()), - }, - Angle::Radians(v1) => match rhs { - Angle::Degrees(v2) => Angle::Radians(v1 - v2.to_radians()), - Angle::Radians(v2) => Angle::Radians(v1 - v2), - }, - } - } -} - -#[derive(Clone, Copy, Debug, Eq, PartialEq, Reflect)] -pub enum MovementDirection { - North, - NorthNortheast, - Northeast, - EastNortheast, - East, - EastSoutheast, - Southeast, - SouthSoutheast, - South, - SouthSouthwest, - Southwest, - WestSouthwest, - West, - WestNorthwest, - Northwest, - NorthNorthwest, -} - -impl From for MovementDirection { - fn from(angle: Angle) -> Self { - let heading = angle.degrees(); - use MovementDirection::*; - match heading { - h if h < 11.5 => East, - h if h < 34.0 => EastNortheast, - h if h < 56.5 => Northeast, - h if h < 79.0 => NorthNortheast, - h if h < 101.5 => North, - h if h < 124.0 => NorthNorthwest, - h if h < 146.5 => Northwest, - h if h < 169.0 => WestNorthwest, - h if h < 191.5 => West, - h if h < 214.0 => WestSouthwest, - h if h < 236.5 => Southwest, - h if h < 259.0 => SouthSouthwest, - h if h < 281.5 => South, - h if h < 304.0 => SouthSoutheast, - h if h < 326.5 => Southeast, - h if h <= 349.0 => EastSoutheast, - _ => East, - } +impl From for MovementDirection { + fn from(rot: Rot2) -> Self { + use CompassOctant::*; + MovementDirection(match rot.as_radians() { + h if h > -PI / 8. && h <= PI / 8. => East, + h if h > PI / 8. && h <= 3. * PI / 8. => NorthEast, + h if h > 3. * PI / 8. && h <= 5. * PI / 8. => North, + h if h > 5. * PI / 8. && h <= 7. * PI / 8. => NorthWest, + h if h > 7. * PI / 8. || h <= -7. * PI / 8. => West, + h if h > -7. * PI / 8. && h <= -5. * PI / 8. => SouthWest, + h if h > -5. * PI / 8. && h <= -3. * PI / 8. => South, + h if h > -3. * PI / 8. && h <= -PI / 8. => SouthEast, + _ => West, + }) } } @@ -194,25 +113,18 @@ impl From for MovementDirection { #[allow(clippy::from_over_into)] impl Into for MovementDirection { fn into(self) -> String { - use MovementDirection::*; - match self { - North => "north".to_string(), - NorthNortheast => "north northeast".to_string(), - Northeast => "northeast".to_string(), - EastNortheast => "east northeast".to_string(), - East => "east".to_string(), - EastSoutheast => "east southeast".to_string(), - Southeast => "southeast".to_string(), - SouthSoutheast => "south southeast".to_string(), - South => "south".to_string(), - SouthSouthwest => "south southwest".to_string(), - Southwest => "southwest".to_string(), - WestSouthwest => "west southwest".to_string(), - West => "west".to_string(), - WestNorthwest => "west northwest".to_string(), - Northwest => "northwest".to_string(), - NorthNorthwest => "north northwest".to_string(), + use CompassOctant::*; + match self.0 { + North => "north", + NorthEast => "northeast", + East => "east", + SouthEast => "southeast", + South => "south", + SouthWest => "southwest", + West => "west", + NorthWest => "northwest", } + .into() } } @@ -223,38 +135,36 @@ impl Display for MovementDirection { } } -#[derive(Component, Clone, Copy, Default, Debug, Eq, PartialEq, Reflect)] +#[derive(Component, Clone, Copy, Debug, Eq, PartialEq, Deref, DerefMut, Reflect)] #[reflect(Component)] -pub enum CardinalDirection { - #[default] - North, - East, - South, - West, -} +pub struct CardinalDirection(pub CompassQuadrant); -impl From for CardinalDirection { - fn from(angle: Angle) -> Self { - let heading = angle.degrees(); - use CardinalDirection::*; - match heading { - h if h <= 45. => East, - h if h <= 135. => North, - h if h <= 225. => West, - h if h <= 315. => South, - _ => East, - } +impl Default for CardinalDirection { + fn default() -> Self { + Self(CompassQuadrant::East) } } -impl From<&CardinalDirection> for Angle { +impl From for CardinalDirection { + fn from(rot: Rot2) -> Self { + use CompassQuadrant::*; + CardinalDirection(match rot.as_radians() { + h if h > -PI / 4. && h <= PI / 4. => East, + h if h > PI / 4. && h <= 3. * PI / 4. => North, + h if h > 3. * PI / 4. || h <= -3. * PI / 4. => West, + _ => South, + }) + } +} + +impl From<&CardinalDirection> for Rot2 { fn from(direction: &CardinalDirection) -> Self { - use CardinalDirection::*; - match direction { - North => Angle::Radians(PI / 2.), - East => Angle::Radians(0.), - South => Angle::Radians(PI * 1.5), - West => Angle::Radians(PI), + use CompassQuadrant::*; + match direction.0 { + North => Rot2::radians(PI / 2.), + East => Rot2::radians(0.), + South => Rot2::radians(-PI / 2.), + West => Rot2::radians(PI), } } } @@ -263,8 +173,8 @@ impl From<&CardinalDirection> for Angle { #[allow(clippy::from_over_into)] impl Into for CardinalDirection { fn into(self) -> String { - use CardinalDirection::*; - match self { + use CompassQuadrant::*; + match self.0 { North => "north".to_string(), East => "east".to_string(), South => "south".to_string(), @@ -344,24 +254,26 @@ pub trait PointLike { self.distance_squared(other).sqrt() } - fn bearing(&self, other: &dyn PointLike) -> Angle { + fn bearing(&self, other: &dyn PointLike) -> Rot2 { let y = other.y() - self.y(); let x = other.x() - self.x(); - Angle::Radians(y.atan2(x)) + Rot2::radians(y.atan2(x)) } fn direction(&self, other: &dyn PointLike) -> MovementDirection { self.bearing(other).into() } - fn direction_and_distance(&self, other: &dyn PointLike, yaw: Option) -> String { + fn direction_and_distance(&self, other: &dyn PointLike, yaw: Option) -> String { let mut tokens: Vec = vec![]; let distance = self.distance(other).round() as i32; if distance > 0 { let tile_or_tiles = if distance == 1 { "tile" } else { "tiles" }; let direction: String = if let Some(yaw) = yaw { - let bearing = self.bearing(other); - (bearing - yaw).relative_desc() + let yaw = yaw.as_radians(); + let bearing = self.bearing(other).as_radians(); + let rot = Rot2::radians(bearing - yaw); + relative_desc(&rot) } else { self.direction(other).into() }; @@ -483,18 +395,18 @@ impl From<&dyn PointLike> for (i32, i32) { } pub trait TransformExt { - fn yaw(&self) -> Angle; + fn yaw(&self) -> Rot2; } impl TransformExt for Transform { - fn yaw(&self) -> Angle { + fn yaw(&self) -> Rot2 { let forward = self.right(); - Angle::Radians(forward.y.atan2(forward.x)) + Rot2::radians(forward.y.atan2(forward.x)) } } pub trait GlobalTransformExt { - fn yaw(&self) -> Angle; + fn yaw(&self) -> Rot2; fn closest_points( &self, @@ -512,9 +424,9 @@ pub trait GlobalTransformExt { } impl GlobalTransformExt for GlobalTransform { - fn yaw(&self) -> Angle { + fn yaw(&self) -> Rot2 { let forward = self.right(); - Angle::Radians(forward.y.atan2(forward.x)) + Rot2::radians(forward.y.atan2(forward.x)) } fn closest_points( @@ -526,11 +438,11 @@ impl GlobalTransformExt for GlobalTransform { let scale = PHYSICS_SCALE.read().unwrap(); let pos1 = Isometry::new( (self.translation() / *scale).xy().into(), - self.yaw().radians(), + self.yaw().as_radians(), ); let pos2 = Isometry::new( (other.translation() / *scale).xy().into(), - other.yaw().radians(), + other.yaw().as_radians(), ); closest_points( &pos1, @@ -551,11 +463,11 @@ impl GlobalTransformExt for GlobalTransform { let scale = PHYSICS_SCALE.read().unwrap(); let pos1 = Isometry::new( (self.translation() / *scale).xy().into(), - self.yaw().radians(), + self.yaw().as_radians(), ); let pos2 = Isometry::new( (other.translation() / *scale).xy().into(), - other.yaw().radians(), + other.yaw().as_radians(), ); let closest = self.closest_points(collider, other, other_collider); let distance = distance( @@ -570,9 +482,10 @@ impl GlobalTransformExt for GlobalTransform { if let ClosestPoints::WithinMargin(p1, p2) = closest { let p1 = (p1.x, p1.y); let p2 = (p2.x, p2.y); - let bearing = p1.bearing(&p2); - let yaw = self.yaw(); - let direction = (bearing - yaw).relative_desc(); + let bearing = p1.bearing(&p2).as_radians(); + let yaw = self.yaw().as_radians(); + let rot = Rot2::radians(bearing - yaw); + let direction = relative_desc(&rot); format!("{direction} {distance} {tile_or_tiles}") } else { format!("{} {}", distance, tile_or_tiles) diff --git a/src/exploration.rs b/src/exploration.rs index 788c7ca..60bff1a 100644 --- a/src/exploration.rs +++ b/src/exploration.rs @@ -1,4 +1,9 @@ -use std::{error::Error, fmt::Debug, hash::Hash, marker::PhantomData}; +use std::{ + error::Error, + fmt::{Debug, Display}, + hash::Hash, + marker::PhantomData, +}; use avian2d::prelude::*; use bevy::prelude::*; @@ -182,7 +187,7 @@ fn exploration_type_changed_announcement( >, ) -> Result<(), Box> where - ExplorationType: Component + Default + Copy + Into, + ExplorationType: Component + Default + Copy + Display, { for (focused, changed) in &focused { if changed.is_added() { @@ -190,8 +195,7 @@ where } match &focused.0 { Some(v) => { - let v: String = (*v).into(); - tts.speak(v, true)?; + tts.speak(format!("{v}"), true)?; } None => { tts.speak("Everything", true)?; @@ -284,7 +288,7 @@ fn exploration_changed_announcement( spatial_query: SpatialQuery, ) -> Result<(), Box> where - ExplorationType: Component + Copy + Into, + ExplorationType: Component + Copy + Display, MapData: 'static + Clone + Default + Send + Sync, { if let Ok((coordinates, exploring, viewshed)) = explorer.get_single() { @@ -319,7 +323,7 @@ where } if tokens.is_empty() { if let Ok(t) = types.get(entity) { - tokens.push((*t).into()); + tokens.push(format!("{t}")); } } } @@ -387,7 +391,7 @@ pub struct Exploration; impl Plugin for ExplorationPlugin where - ExplorationType: 'static + Component + Default + Copy + Ord + PartialEq + Into, + ExplorationType: 'static + Component + Default + Copy + Ord + PartialEq + Display, MapData: 'static + Clone + Default + Send + Sync, { fn build(&self, app: &mut App) { diff --git a/src/navigation.rs b/src/navigation.rs index f2bc707..7e4a0ce 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -3,12 +3,12 @@ use std::{collections::HashMap, error::Error, f32::consts::PI, fmt::Debug, hash::Hash}; use avian2d::prelude::*; -use bevy::prelude::*; +use bevy::{math::CompassQuadrant, prelude::*}; use bevy_tts::Tts; use leafwing_input_manager::prelude::*; use crate::{ - core::{Angle, CardinalDirection, GlobalTransformExt, Player, TransformExt}, + core::{CardinalDirection, GlobalTransformExt, Player, TransformExt}, error::error_handler, log::Log, map::Zone, @@ -77,7 +77,7 @@ impl Default for StrafeMovementFactor { #[derive(Component, Clone, Copy, Default, Debug, Deref, DerefMut, Reflect)] #[reflect(Component)] -pub struct RotationSpeed(pub Angle); +pub struct RotationSpeed(pub Rot2); #[derive(Deref, DerefMut)] struct SnapTimer(Timer); @@ -102,7 +102,6 @@ impl Default for Speed { } fn snap( - mut tts: ResMut, mut snap_timers: ResMut, mut query: Query< ( @@ -113,37 +112,40 @@ fn snap( ), With, >, -) -> Result<(), Box> { +) { for (entity, actions, mut transform, direction) in &mut query { if snap_timers.contains_key(&entity) { continue; } else if actions.just_pressed(&NavigationAction::SnapLeft) { snap_timers.insert(entity, SnapTimer::default()); - transform.rotation = Quat::from_rotation_z(match direction { - CardinalDirection::North => PI, - CardinalDirection::East => PI / 2., - CardinalDirection::South => 0., - CardinalDirection::West => -PI / 2., + transform.rotation = Quat::from_rotation_z(match direction.0 { + CompassQuadrant::North => PI, + CompassQuadrant::East => PI / 2., + CompassQuadrant::South => 0., + CompassQuadrant::West => -PI / 2., }); } else if actions.just_pressed(&NavigationAction::SnapRight) { snap_timers.insert(entity, SnapTimer::default()); - transform.rotation = Quat::from_rotation_z(match direction { - CardinalDirection::North => 0., - CardinalDirection::East => -PI / 2., - CardinalDirection::South => PI, - CardinalDirection::West => PI / 2., + transform.rotation = Quat::from_rotation_z(match direction.0 { + CompassQuadrant::North => 0., + CompassQuadrant::East => -PI / 2., + CompassQuadrant::South => PI, + CompassQuadrant::West => PI / 2., }); } else if actions.just_pressed(&NavigationAction::SnapReverse) { snap_timers.insert(entity, SnapTimer::default()); transform.rotate(Quat::from_rotation_z(PI)); } else if actions.just_pressed(&NavigationAction::SnapCardinal) { - let yaw: Angle = direction.into(); - let yaw = yaw.radians(); - transform.rotation = Quat::from_rotation_z(yaw); - tts.speak(direction.to_string(), true)?; + println!("Direction: {direction:?}"); + let yaw: Rot2 = direction.into(); + let yaw = yaw.as_radians(); + println!("Yaw: {yaw}"); + let rotation = Quat::from_rotation_z(yaw); + if transform.rotation != rotation { + transform.rotation = rotation; + } } } - Ok(()) } fn tick_snap_timers(time: Res