Remove Angle
and standardize direction types.
This commit is contained in:
parent
c9c45b2569
commit
827cae1a4b
273
src/core.rs
273
src/core.rs
|
@ -2,11 +2,14 @@ use std::{
|
||||||
cmp::{max, min},
|
cmp::{max, min},
|
||||||
f32::consts::PI,
|
f32::consts::PI,
|
||||||
fmt::Display,
|
fmt::Display,
|
||||||
ops::Sub,
|
|
||||||
sync::RwLock,
|
sync::RwLock,
|
||||||
};
|
};
|
||||||
|
|
||||||
use bevy::{app::PluginGroupBuilder, math::FloatOrd, prelude::*};
|
use bevy::{
|
||||||
|
app::PluginGroupBuilder,
|
||||||
|
math::{CompassOctant, CompassQuadrant, FloatOrd},
|
||||||
|
prelude::*,
|
||||||
|
};
|
||||||
use bevy_rapier2d::{
|
use bevy_rapier2d::{
|
||||||
parry::query::{closest_points, distance, ClosestPoints},
|
parry::query::{closest_points, distance, ClosestPoints},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
|
@ -16,103 +19,63 @@ use once_cell::sync::Lazy;
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Reflect)]
|
fn relative_desc(rot: &Rot2) -> String {
|
||||||
pub enum Angle {
|
|
||||||
Degrees(f32),
|
|
||||||
Radians(f32),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Angle {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::Radians(0.)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
let mode = RELATIVE_DIRECTION_MODE.read().unwrap();
|
||||||
match self.degrees() {
|
match rot.as_radians() {
|
||||||
v if v <= 15. => "ahead",
|
v if v <= PI / 12. && v > -PI / 12. => "ahead",
|
||||||
v if v <= 45. => {
|
v if v <= PI / 4. && v > PI / 12. => {
|
||||||
if *mode == RelativeDirectionMode::ClockFacing {
|
if *mode == RelativeDirectionMode::ClockFacing {
|
||||||
"11:00"
|
"11:00"
|
||||||
} else {
|
} else {
|
||||||
"ahead and left"
|
"ahead and left"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
v if v <= 75. => {
|
v if v <= 3. * PI / 8. && v > PI / 4. => {
|
||||||
if *mode == RelativeDirectionMode::ClockFacing {
|
if *mode == RelativeDirectionMode::ClockFacing {
|
||||||
"10:00"
|
"10:00"
|
||||||
} else {
|
} else {
|
||||||
"left and ahead"
|
"left and ahead"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
v if v <= 105. => "left",
|
v if v <= 5. * PI / 8. && v > 3. * PI / 8. => "left",
|
||||||
v if v <= 135. => {
|
v if v <= 3. * PI / 4. && v > 5. * PI / 8. => {
|
||||||
if *mode == RelativeDirectionMode::ClockFacing {
|
if *mode == RelativeDirectionMode::ClockFacing {
|
||||||
"8:00"
|
"8:00"
|
||||||
} else {
|
} else {
|
||||||
"left and behind"
|
"left and behind"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
v if v <= 165. => {
|
v if v <= 11. * PI / 12. && v > 3. * PI / 4. => {
|
||||||
if *mode == RelativeDirectionMode::ClockFacing {
|
if *mode == RelativeDirectionMode::ClockFacing {
|
||||||
"7:00"
|
"7:00"
|
||||||
} else {
|
} else {
|
||||||
"behind and left"
|
"behind and left"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
v if v <= 195. => "behind",
|
v if v <= PI && v > 11. * PI / 12. || v > -PI && v <= -11. * PI / 12. => "behind",
|
||||||
v if v <= 225. => {
|
v if v <= -3. * PI / 4. && v > -11. * PI / 12. => {
|
||||||
if *mode == RelativeDirectionMode::ClockFacing {
|
if *mode == RelativeDirectionMode::ClockFacing {
|
||||||
"5:00"
|
"5:00"
|
||||||
} else {
|
} else {
|
||||||
"behind and right"
|
"behind and right"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
v if v <= 255. => {
|
v if v <= -5. * PI / 8. && v > -3. * PI / 4. => {
|
||||||
if *mode == RelativeDirectionMode::ClockFacing {
|
if *mode == RelativeDirectionMode::ClockFacing {
|
||||||
"4:00"
|
"4:00"
|
||||||
} else {
|
} else {
|
||||||
"right and behind"
|
"right and behind"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
v if v <= 285. => "right",
|
v if v <= -3. * PI / 8. && v > -5. * PI / 8. => "right",
|
||||||
v if v <= 315. => {
|
v if v <= -PI / 4. && v > -3. * PI / 8. => {
|
||||||
if *mode == RelativeDirectionMode::ClockFacing {
|
if *mode == RelativeDirectionMode::ClockFacing {
|
||||||
"2:00"
|
"2:00"
|
||||||
} else {
|
} else {
|
||||||
"right and ahead"
|
"right and ahead"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
v if v <= 345. => {
|
v if v <= -PI / 12. && v > -PI / 4. => {
|
||||||
if *mode == RelativeDirectionMode::ClockFacing {
|
if *mode == RelativeDirectionMode::ClockFacing {
|
||||||
"1:00"
|
"1:00"
|
||||||
} else {
|
} else {
|
||||||
|
@ -122,69 +85,25 @@ impl Angle {
|
||||||
_ => "ahead",
|
_ => "ahead",
|
||||||
}
|
}
|
||||||
.into()
|
.into()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sub for Angle {
|
#[derive(Clone, Copy, Debug, Eq, PartialEq, Deref, DerefMut, Reflect)]
|
||||||
type Output = Self;
|
pub struct MovementDirection(CompassOctant);
|
||||||
|
|
||||||
fn sub(self, rhs: Self) -> Self::Output {
|
impl From<Rot2> for MovementDirection {
|
||||||
match self {
|
fn from(rot: Rot2) -> Self {
|
||||||
Angle::Degrees(v1) => match rhs {
|
use CompassOctant::*;
|
||||||
Angle::Degrees(v2) => Angle::Degrees(v1 - v2),
|
MovementDirection(match rot.as_radians() {
|
||||||
Angle::Radians(v2) => Angle::Degrees(v1 - v2.to_degrees()),
|
h if h > -PI / 8. && h <= PI / 8. => East,
|
||||||
},
|
h if h > PI / 8. && h <= 3. * PI / 8. => NorthEast,
|
||||||
Angle::Radians(v1) => match rhs {
|
h if h > 3. * PI / 8. && h <= 5. * PI / 8. => North,
|
||||||
Angle::Degrees(v2) => Angle::Radians(v1 - v2.to_radians()),
|
h if h > 5. * PI / 8. && h <= 7. * PI / 8. => NorthWest,
|
||||||
Angle::Radians(v2) => Angle::Radians(v1 - v2),
|
h if h > 7. * PI / 8. || h <= -7. * PI / 8. => East,
|
||||||
},
|
h if h > -7. * PI / 8. && h <= -5. * PI / 8. => SouthEast,
|
||||||
}
|
h if h > -5. * PI / 8. && h <= -3. * PI / 8. => South,
|
||||||
}
|
h if h > -3. * PI / 8. && h <= -PI / 8. => SouthWest,
|
||||||
}
|
_ => West,
|
||||||
|
})
|
||||||
#[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<Angle> 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,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -192,25 +111,18 @@ impl From<Angle> for MovementDirection {
|
||||||
#[allow(clippy::from_over_into)]
|
#[allow(clippy::from_over_into)]
|
||||||
impl Into<String> for MovementDirection {
|
impl Into<String> for MovementDirection {
|
||||||
fn into(self) -> String {
|
fn into(self) -> String {
|
||||||
use MovementDirection::*;
|
use CompassOctant::*;
|
||||||
match self {
|
match self.0 {
|
||||||
North => "north".to_string(),
|
North => "north",
|
||||||
NorthNortheast => "north northeast".to_string(),
|
NorthEast => "northeast",
|
||||||
Northeast => "northeast".to_string(),
|
East => "east",
|
||||||
EastNortheast => "east northeast".to_string(),
|
SouthEast => "southeast",
|
||||||
East => "east".to_string(),
|
South => "south",
|
||||||
EastSoutheast => "east southeast".to_string(),
|
SouthWest => "southwest",
|
||||||
Southeast => "southeast".to_string(),
|
West => "west",
|
||||||
SouthSoutheast => "south southeast".to_string(),
|
NorthWest => "northwest",
|
||||||
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(),
|
|
||||||
}
|
}
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -221,38 +133,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)]
|
#[reflect(Component)]
|
||||||
pub enum CardinalDirection {
|
pub struct CardinalDirection(pub CompassQuadrant);
|
||||||
#[default]
|
|
||||||
North,
|
|
||||||
East,
|
|
||||||
South,
|
|
||||||
West,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Angle> for CardinalDirection {
|
impl Default for CardinalDirection {
|
||||||
fn from(angle: Angle) -> Self {
|
fn default() -> Self {
|
||||||
let heading = angle.degrees();
|
Self(CompassQuadrant::East)
|
||||||
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 From<&CardinalDirection> for Angle {
|
impl From<Rot2> 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 {
|
fn from(direction: &CardinalDirection) -> Self {
|
||||||
use CardinalDirection::*;
|
use CompassQuadrant::*;
|
||||||
match direction {
|
match direction.0 {
|
||||||
North => Angle::Radians(PI / 2.),
|
North => Rot2::radians(PI / 2.),
|
||||||
East => Angle::Radians(0.),
|
East => Rot2::radians(0.),
|
||||||
South => Angle::Radians(PI * 1.5),
|
South => Rot2::radians(-PI / 2.),
|
||||||
West => Angle::Radians(PI),
|
West => Rot2::radians(PI),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -261,8 +171,8 @@ impl From<&CardinalDirection> for Angle {
|
||||||
#[allow(clippy::from_over_into)]
|
#[allow(clippy::from_over_into)]
|
||||||
impl Into<String> for CardinalDirection {
|
impl Into<String> for CardinalDirection {
|
||||||
fn into(self) -> String {
|
fn into(self) -> String {
|
||||||
use CardinalDirection::*;
|
use CompassQuadrant::*;
|
||||||
match self {
|
match self.0 {
|
||||||
North => "north".to_string(),
|
North => "north".to_string(),
|
||||||
East => "east".to_string(),
|
East => "east".to_string(),
|
||||||
South => "south".to_string(),
|
South => "south".to_string(),
|
||||||
|
@ -342,24 +252,26 @@ pub trait PointLike {
|
||||||
self.distance_squared(other).sqrt()
|
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 y = other.y() - self.y();
|
||||||
let x = other.x() - self.x();
|
let x = other.x() - self.x();
|
||||||
Angle::Radians(y.atan2(x))
|
Rot2::radians(y.atan2(x))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn direction(&self, other: &dyn PointLike) -> MovementDirection {
|
fn direction(&self, other: &dyn PointLike) -> MovementDirection {
|
||||||
self.bearing(other).into()
|
self.bearing(other).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn direction_and_distance(&self, other: &dyn PointLike, yaw: Option<Angle>) -> String {
|
fn direction_and_distance(&self, other: &dyn PointLike, yaw: Option<Rot2>) -> String {
|
||||||
let mut tokens: Vec<String> = vec![];
|
let mut tokens: Vec<String> = vec![];
|
||||||
let distance = self.distance(other).round() as i32;
|
let distance = self.distance(other).round() as i32;
|
||||||
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 bearing = self.bearing(other);
|
let yaw = yaw.as_radians();
|
||||||
(bearing - yaw).relative_desc()
|
let bearing = self.bearing(other).as_radians();
|
||||||
|
let rot = Rot2::radians(bearing - yaw);
|
||||||
|
relative_desc(&rot)
|
||||||
} else {
|
} else {
|
||||||
self.direction(other).into()
|
self.direction(other).into()
|
||||||
};
|
};
|
||||||
|
@ -481,18 +393,18 @@ impl From<&dyn PointLike> for (i32, i32) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait TransformExt {
|
pub trait TransformExt {
|
||||||
fn yaw(&self) -> Angle;
|
fn yaw(&self) -> Rot2;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TransformExt for Transform {
|
impl TransformExt for Transform {
|
||||||
fn yaw(&self) -> Angle {
|
fn yaw(&self) -> Rot2 {
|
||||||
let forward = self.right();
|
let forward = self.right();
|
||||||
Angle::Radians(forward.y.atan2(forward.x))
|
Rot2::radians(forward.y.atan2(forward.x))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait GlobalTransformExt {
|
pub trait GlobalTransformExt {
|
||||||
fn yaw(&self) -> Angle;
|
fn yaw(&self) -> Rot2;
|
||||||
|
|
||||||
fn closest_points(
|
fn closest_points(
|
||||||
&self,
|
&self,
|
||||||
|
@ -510,9 +422,9 @@ pub trait GlobalTransformExt {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GlobalTransformExt for GlobalTransform {
|
impl GlobalTransformExt for GlobalTransform {
|
||||||
fn yaw(&self) -> Angle {
|
fn yaw(&self) -> Rot2 {
|
||||||
let forward = self.right();
|
let forward = self.right();
|
||||||
Angle::Radians(forward.y.atan2(forward.x))
|
Rot2::radians(forward.y.atan2(forward.x))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn closest_points(
|
fn closest_points(
|
||||||
|
@ -524,11 +436,11 @@ impl GlobalTransformExt for GlobalTransform {
|
||||||
let scale = PHYSICS_SCALE.read().unwrap();
|
let scale = PHYSICS_SCALE.read().unwrap();
|
||||||
let pos1 = Isometry::new(
|
let pos1 = Isometry::new(
|
||||||
(self.translation() / *scale).xy().into(),
|
(self.translation() / *scale).xy().into(),
|
||||||
self.yaw().radians(),
|
self.yaw().as_radians(),
|
||||||
);
|
);
|
||||||
let pos2 = Isometry::new(
|
let pos2 = Isometry::new(
|
||||||
(other.translation() / *scale).xy().into(),
|
(other.translation() / *scale).xy().into(),
|
||||||
other.yaw().radians(),
|
other.yaw().as_radians(),
|
||||||
);
|
);
|
||||||
closest_points(&pos1, &*collider.raw, &pos2, &*other_collider.raw, f32::MAX).unwrap()
|
closest_points(&pos1, &*collider.raw, &pos2, &*other_collider.raw, f32::MAX).unwrap()
|
||||||
}
|
}
|
||||||
|
@ -542,11 +454,11 @@ impl GlobalTransformExt for GlobalTransform {
|
||||||
let scale = PHYSICS_SCALE.read().unwrap();
|
let scale = PHYSICS_SCALE.read().unwrap();
|
||||||
let pos1 = Isometry::new(
|
let pos1 = Isometry::new(
|
||||||
(self.translation() / *scale).xy().into(),
|
(self.translation() / *scale).xy().into(),
|
||||||
self.yaw().radians(),
|
self.yaw().as_radians(),
|
||||||
);
|
);
|
||||||
let pos2 = Isometry::new(
|
let pos2 = Isometry::new(
|
||||||
(other.translation() / *scale).xy().into(),
|
(other.translation() / *scale).xy().into(),
|
||||||
other.yaw().radians(),
|
other.yaw().as_radians(),
|
||||||
);
|
);
|
||||||
let closest = self.closest_points(collider, other, other_collider);
|
let closest = self.closest_points(collider, other, other_collider);
|
||||||
let distance = distance(&pos1, &*collider.raw, &pos2, &*other_collider.raw).unwrap() as u32;
|
let distance = distance(&pos1, &*collider.raw, &pos2, &*other_collider.raw).unwrap() as u32;
|
||||||
|
@ -555,9 +467,10 @@ impl GlobalTransformExt for GlobalTransform {
|
||||||
if let ClosestPoints::WithinMargin(p1, p2) = closest {
|
if let ClosestPoints::WithinMargin(p1, p2) = closest {
|
||||||
let p1 = (p1.x, p1.y);
|
let p1 = (p1.x, p1.y);
|
||||||
let p2 = (p2.x, p2.y);
|
let p2 = (p2.x, p2.y);
|
||||||
let bearing = p1.bearing(&p2);
|
let bearing = p1.bearing(&p2).as_radians();
|
||||||
let yaw = self.yaw();
|
let yaw = self.yaw().as_radians();
|
||||||
let direction = (bearing - yaw).relative_desc();
|
let rot = Rot2::radians(bearing - yaw);
|
||||||
|
let direction = relative_desc(&rot);
|
||||||
format!("{direction} {distance} {tile_or_tiles}")
|
format!("{direction} {distance} {tile_or_tiles}")
|
||||||
} else {
|
} else {
|
||||||
format!("{} {}", distance, tile_or_tiles)
|
format!("{} {}", distance, tile_or_tiles)
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
use std::{collections::HashMap, error::Error, f32::consts::PI, fmt::Debug, hash::Hash};
|
use std::{collections::HashMap, error::Error, f32::consts::PI, fmt::Debug, hash::Hash};
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::{math::CompassQuadrant, prelude::*};
|
||||||
use bevy_rapier2d::prelude::*;
|
use bevy_rapier2d::prelude::*;
|
||||||
use bevy_tts::Tts;
|
use bevy_tts::Tts;
|
||||||
use leafwing_input_manager::prelude::*;
|
use leafwing_input_manager::prelude::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
core::{Angle, Area, CardinalDirection, GlobalTransformExt, Player, TransformExt},
|
core::{Area, CardinalDirection, GlobalTransformExt, Player, TransformExt},
|
||||||
error::error_handler,
|
error::error_handler,
|
||||||
log::Log,
|
log::Log,
|
||||||
utils::target_and_other,
|
utils::target_and_other,
|
||||||
|
@ -74,7 +74,7 @@ impl Default for StrafeMovementFactor {
|
||||||
|
|
||||||
#[derive(Component, Clone, Copy, Default, Debug, Deref, DerefMut, Reflect)]
|
#[derive(Component, Clone, Copy, Default, Debug, Deref, DerefMut, Reflect)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct RotationSpeed(pub Angle);
|
pub struct RotationSpeed(pub Rot2);
|
||||||
|
|
||||||
#[derive(Deref, DerefMut)]
|
#[derive(Deref, DerefMut)]
|
||||||
struct SnapTimer(Timer);
|
struct SnapTimer(Timer);
|
||||||
|
@ -116,26 +116,28 @@ fn snap(
|
||||||
continue;
|
continue;
|
||||||
} else if actions.just_pressed(&NavigationAction::SnapLeft) {
|
} else if actions.just_pressed(&NavigationAction::SnapLeft) {
|
||||||
snap_timers.insert(entity, SnapTimer::default());
|
snap_timers.insert(entity, SnapTimer::default());
|
||||||
transform.rotation = Quat::from_rotation_z(match direction {
|
transform.rotation = Quat::from_rotation_z(match direction.0 {
|
||||||
CardinalDirection::North => PI,
|
CompassQuadrant::North => PI,
|
||||||
CardinalDirection::East => PI / 2.,
|
CompassQuadrant::East => PI / 2.,
|
||||||
CardinalDirection::South => 0.,
|
CompassQuadrant::South => 0.,
|
||||||
CardinalDirection::West => -PI / 2.,
|
CompassQuadrant::West => -PI / 2.,
|
||||||
});
|
});
|
||||||
} else if actions.just_pressed(&NavigationAction::SnapRight) {
|
} else if actions.just_pressed(&NavigationAction::SnapRight) {
|
||||||
snap_timers.insert(entity, SnapTimer::default());
|
snap_timers.insert(entity, SnapTimer::default());
|
||||||
transform.rotation = Quat::from_rotation_z(match direction {
|
transform.rotation = Quat::from_rotation_z(match direction.0 {
|
||||||
CardinalDirection::North => 0.,
|
CompassQuadrant::North => 0.,
|
||||||
CardinalDirection::East => -PI / 2.,
|
CompassQuadrant::East => -PI / 2.,
|
||||||
CardinalDirection::South => PI,
|
CompassQuadrant::South => PI,
|
||||||
CardinalDirection::West => PI / 2.,
|
CompassQuadrant::West => PI / 2.,
|
||||||
});
|
});
|
||||||
} else if actions.just_pressed(&NavigationAction::SnapReverse) {
|
} else if actions.just_pressed(&NavigationAction::SnapReverse) {
|
||||||
snap_timers.insert(entity, SnapTimer::default());
|
snap_timers.insert(entity, SnapTimer::default());
|
||||||
transform.rotate(Quat::from_rotation_z(PI));
|
transform.rotate(Quat::from_rotation_z(PI));
|
||||||
} else if actions.just_pressed(&NavigationAction::SnapCardinal) {
|
} else if actions.just_pressed(&NavigationAction::SnapCardinal) {
|
||||||
let yaw: Angle = direction.into();
|
println!("Direction: {direction:?}");
|
||||||
let yaw = yaw.radians();
|
let yaw: Rot2 = direction.into();
|
||||||
|
let yaw = yaw.as_radians();
|
||||||
|
println!("Yaw: {yaw}");
|
||||||
transform.rotation = Quat::from_rotation_z(yaw);
|
transform.rotation = Quat::from_rotation_z(yaw);
|
||||||
tts.speak(direction.to_string(), true)?;
|
tts.speak(direction.to_string(), true)?;
|
||||||
}
|
}
|
||||||
|
@ -220,7 +222,7 @@ fn controls(
|
||||||
if rapier_context
|
if rapier_context
|
||||||
.cast_shape(
|
.cast_shape(
|
||||||
transform.translation.truncate(),
|
transform.translation.truncate(),
|
||||||
transform.yaw().radians(),
|
transform.yaw().as_radians(),
|
||||||
pair,
|
pair,
|
||||||
collider,
|
collider,
|
||||||
ShapeCastOptions {
|
ShapeCastOptions {
|
||||||
|
@ -240,7 +242,7 @@ fn controls(
|
||||||
if !snap_timers.contains_key(&entity) {
|
if !snap_timers.contains_key(&entity) {
|
||||||
if let Some(rotation_speed) = rotation_speed {
|
if let Some(rotation_speed) = rotation_speed {
|
||||||
let delta =
|
let delta =
|
||||||
-rotation_speed.radians() * actions.clamped_value(&NavigationAction::Rotate);
|
rotation_speed.as_radians() * actions.clamped_value(&NavigationAction::Rotate);
|
||||||
actions.set_value(&NavigationAction::SetAngularVelocity, delta);
|
actions.set_value(&NavigationAction::SetAngularVelocity, delta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -288,17 +290,12 @@ fn remove_direction(
|
||||||
|
|
||||||
fn speak_direction(
|
fn speak_direction(
|
||||||
mut tts: ResMut<Tts>,
|
mut tts: ResMut<Tts>,
|
||||||
player: Query<
|
player: Query<&CardinalDirection, (With<Player>, Changed<CardinalDirection>)>,
|
||||||
(&CardinalDirection, Ref<CardinalDirection>),
|
|
||||||
(With<Player>, Changed<CardinalDirection>),
|
|
||||||
>,
|
|
||||||
) -> Result<(), Box<dyn Error>> {
|
) -> Result<(), Box<dyn Error>> {
|
||||||
if let Ok((direction, change)) = player.get_single() {
|
if let Ok(direction) = player.get_single() {
|
||||||
if !change.is_added() {
|
|
||||||
let direction: String = (*direction).into();
|
let direction: String = (*direction).into();
|
||||||
tts.speak(direction, true)?;
|
tts.speak(direction, true)?;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -175,11 +175,9 @@ fn calculate_path(
|
||||||
>,
|
>,
|
||||||
) {
|
) {
|
||||||
for (entity, handle, destination, coordinates, shape, cost_map) in &query {
|
for (entity, handle, destination, coordinates, shape, cost_map) in &query {
|
||||||
trace
|
trace!("{entity}: destination: {destination:?}");
|
||||||
!("{entity}: destination: {destination:?}");
|
|
||||||
if coordinates.i32() == **destination {
|
if coordinates.i32() == **destination {
|
||||||
trace
|
trace!("{entity}: remove1");
|
||||||
!("{entity}: remove1");
|
|
||||||
commands
|
commands
|
||||||
.entity(entity)
|
.entity(entity)
|
||||||
.remove::<Path>()
|
.remove::<Path>()
|
||||||
|
@ -229,8 +227,7 @@ fn calculate_path(
|
||||||
shape_clone,
|
shape_clone,
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
trace
|
trace!("{entity}: remove2");
|
||||||
!("{entity}: remove2");
|
|
||||||
commands
|
commands
|
||||||
.entity(entity)
|
.entity(entity)
|
||||||
.insert(Calculating(task))
|
.insert(Calculating(task))
|
||||||
|
@ -255,8 +252,7 @@ fn poll_tasks(
|
||||||
trace!("{entity:?}: Path: {path:?}");
|
trace!("{entity:?}: Path: {path:?}");
|
||||||
commands.entity(entity).insert(path);
|
commands.entity(entity).insert(path);
|
||||||
} else {
|
} else {
|
||||||
trace
|
trace!(
|
||||||
!(
|
|
||||||
"{entity:?}: path: no path from {:?} to {:?}",
|
"{entity:?}: path: no path from {:?} to {:?}",
|
||||||
transform.translation.truncate().i32(),
|
transform.translation.truncate().i32(),
|
||||||
**destination
|
**destination
|
||||||
|
@ -349,7 +345,7 @@ fn negotiate_path(
|
||||||
if rapier_context
|
if rapier_context
|
||||||
.cast_shape(
|
.cast_shape(
|
||||||
start,
|
start,
|
||||||
transform.yaw().radians(),
|
transform.yaw().as_radians(),
|
||||||
direction,
|
direction,
|
||||||
collider,
|
collider,
|
||||||
ShapeCastOptions {
|
ShapeCastOptions {
|
||||||
|
@ -362,8 +358,7 @@ fn negotiate_path(
|
||||||
)
|
)
|
||||||
.is_some()
|
.is_some()
|
||||||
{
|
{
|
||||||
trace
|
trace!("{entity:?} is stuck");
|
||||||
!("{entity:?} is stuck");
|
|
||||||
// TODO: Remove when we have an actual character controller.
|
// TODO: Remove when we have an actual character controller.
|
||||||
next.x = next.x.trunc();
|
next.x = next.x.trunc();
|
||||||
next.y = next.y.trunc();
|
next.y = next.y.trunc();
|
||||||
|
@ -376,8 +371,7 @@ fn negotiate_path(
|
||||||
transform.rotation = Quat::from_rotation_z(angle);
|
transform.rotation = Quat::from_rotation_z(angle);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
trace
|
trace!("{entity:?}: empty path, cleaning");
|
||||||
!("{entity:?}: empty path, cleaning");
|
|
||||||
if let Some(mut actions) = actions {
|
if let Some(mut actions) = actions {
|
||||||
trace!("{entity:?}: Disabling pathfind because at destination");
|
trace!("{entity:?}: Disabling pathfind because at destination");
|
||||||
actions.reset_all();
|
actions.reset_all();
|
||||||
|
@ -421,8 +415,7 @@ fn actions(
|
||||||
*current_dest = dest;
|
*current_dest = dest;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
trace
|
trace!("{entity:?}: Adding destination, zeroing velocity");
|
||||||
!("{entity:?}: Adding destination, zeroing velocity");
|
|
||||||
navigation_action.set_axis_pair(&NavigationAction::SetLinearVelocity, Vec2::ZERO);
|
navigation_action.set_axis_pair(&NavigationAction::SetLinearVelocity, Vec2::ZERO);
|
||||||
commands.entity(entity).insert(dest);
|
commands.entity(entity).insert(dest);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user