Ditch constructors in favor of From conversions where it makes sense.

This commit is contained in:
Nolan Darilek 2022-02-11 08:44:41 -06:00
parent 288b1605b9
commit 27baaceaea

View File

@ -14,7 +14,9 @@ use once_cell::sync::Lazy;
use rand::prelude::*; use rand::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Component, Clone, Copy, Debug, Default, Deref, DerefMut, PartialEq, PartialOrd, Reflect)] #[derive(
Component, Clone, Copy, Debug, Default, Deref, DerefMut, PartialEq, PartialOrd, Reflect,
)]
#[reflect(Component)] #[reflect(Component)]
pub struct Coordinates(pub (f32, f32)); pub struct Coordinates(pub (f32, f32));
@ -189,16 +191,10 @@ pub enum MovementDirection {
NorthNorthwest, NorthNorthwest,
} }
impl MovementDirection { impl From<Angle> for MovementDirection {
pub fn new(heading: f32) -> Self { fn from(angle: Angle) -> Self {
let heading = angle.degrees();
use MovementDirection::*; use MovementDirection::*;
let mut heading = heading;
while heading >= 360. {
heading -= 360.;
}
while heading < 0. {
heading += 360.;
}
match heading { match heading {
h if h < 11.5 => East, h if h < 11.5 => East,
h if h < 34.0 => EastNortheast, h if h < 34.0 => EastNortheast,
@ -221,12 +217,6 @@ impl MovementDirection {
} }
} }
impl From<Angle> for MovementDirection {
fn from(angle: Angle) -> Self {
MovementDirection::new(angle.degrees())
}
}
// Converting from strings into directions doesn't make sense. // Converting from strings into directions doesn't make sense.
#[allow(clippy::from_over_into)] #[allow(clippy::from_over_into)]
impl Into<String> for MovementDirection { impl Into<String> for MovementDirection {
@ -268,16 +258,10 @@ pub enum CardinalDirection {
West, West,
} }
impl CardinalDirection { impl From<Angle> for CardinalDirection {
pub fn new(heading: f32) -> Self { fn from(angle: Angle) -> Self {
let heading = angle.degrees();
use CardinalDirection::*; use CardinalDirection::*;
let mut heading = heading;
while heading >= 360. {
heading -= 360.;
}
while heading < 0. {
heading += 360.;
}
match heading { match heading {
h if h <= 45. => East, h if h <= 45. => East,
h if h <= 135. => North, h if h <= 135. => North,
@ -286,20 +270,17 @@ impl CardinalDirection {
_ => East, _ => East,
} }
} }
pub fn angle(&self) -> Angle {
match self {
CardinalDirection::North => Angle::Radians(PI / 2.),
CardinalDirection::East => Angle::Radians(0.),
CardinalDirection::South => Angle::Radians(-PI / 2.),
CardinalDirection::West => Angle::Radians(PI),
}
}
} }
impl From<Angle> for CardinalDirection { impl From<&CardinalDirection> for Angle {
fn from(angle: Angle) -> Self { fn from(direction: &CardinalDirection) -> Self {
CardinalDirection::new(angle.degrees()) use CardinalDirection::*;
match direction {
North => Angle::Radians(PI / 2.),
East => Angle::Radians(0.),
South => Angle::Radians(PI * 1.5),
West => Angle::Radians(PI),
}
} }
} }
@ -667,7 +648,10 @@ fn copy_coordinates_to_transform(
fn copy_rigid_body_position_to_coordinates( fn copy_rigid_body_position_to_coordinates(
mut query: Query< mut query: Query<
(&mut Coordinates, &RigidBodyPositionComponent), (&mut Coordinates, &RigidBodyPositionComponent),
(Changed<RigidBodyPositionComponent>, With<RigidBodyPositionSync>), (
Changed<RigidBodyPositionComponent>,
With<RigidBodyPositionSync>,
),
>, >,
) { ) {
for (mut coordinates, position) in query.iter_mut() { for (mut coordinates, position) in query.iter_mut() {
@ -733,8 +717,7 @@ impl Plugin for CorePlugin {
.add_startup_system(setup) .add_startup_system(setup)
.add_system_to_stage( .add_system_to_stage(
CoreStage::PostUpdate, CoreStage::PostUpdate,
copy_coordinates_to_transform copy_coordinates_to_transform.before(TransformSystem::TransformPropagate),
.before(TransformSystem::TransformPropagate),
) )
.add_system_to_stage( .add_system_to_stage(
CoreStage::PostUpdate, CoreStage::PostUpdate,