Ditch constructors in favor of From
conversions where it makes sense.
This commit is contained in:
parent
288b1605b9
commit
27baaceaea
63
src/core.rs
63
src/core.rs
|
@ -14,7 +14,9 @@ use once_cell::sync::Lazy;
|
|||
use rand::prelude::*;
|
||||
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)]
|
||||
pub struct Coordinates(pub (f32, f32));
|
||||
|
||||
|
@ -189,16 +191,10 @@ pub enum MovementDirection {
|
|||
NorthNorthwest,
|
||||
}
|
||||
|
||||
impl MovementDirection {
|
||||
pub fn new(heading: f32) -> Self {
|
||||
impl From<Angle> for MovementDirection {
|
||||
fn from(angle: Angle) -> Self {
|
||||
let heading = angle.degrees();
|
||||
use MovementDirection::*;
|
||||
let mut heading = heading;
|
||||
while heading >= 360. {
|
||||
heading -= 360.;
|
||||
}
|
||||
while heading < 0. {
|
||||
heading += 360.;
|
||||
}
|
||||
match heading {
|
||||
h if h < 11.5 => East,
|
||||
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.
|
||||
#[allow(clippy::from_over_into)]
|
||||
impl Into<String> for MovementDirection {
|
||||
|
@ -268,16 +258,10 @@ pub enum CardinalDirection {
|
|||
West,
|
||||
}
|
||||
|
||||
impl CardinalDirection {
|
||||
pub fn new(heading: f32) -> Self {
|
||||
impl From<Angle> for CardinalDirection {
|
||||
fn from(angle: Angle) -> Self {
|
||||
let heading = angle.degrees();
|
||||
use CardinalDirection::*;
|
||||
let mut heading = heading;
|
||||
while heading >= 360. {
|
||||
heading -= 360.;
|
||||
}
|
||||
while heading < 0. {
|
||||
heading += 360.;
|
||||
}
|
||||
match heading {
|
||||
h if h <= 45. => East,
|
||||
h if h <= 135. => North,
|
||||
|
@ -286,20 +270,17 @@ impl CardinalDirection {
|
|||
_ => 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 {
|
||||
fn from(angle: Angle) -> Self {
|
||||
CardinalDirection::new(angle.degrees())
|
||||
impl From<&CardinalDirection> for Angle {
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -667,7 +648,10 @@ fn copy_coordinates_to_transform(
|
|||
fn copy_rigid_body_position_to_coordinates(
|
||||
mut query: Query<
|
||||
(&mut Coordinates, &RigidBodyPositionComponent),
|
||||
(Changed<RigidBodyPositionComponent>, With<RigidBodyPositionSync>),
|
||||
(
|
||||
Changed<RigidBodyPositionComponent>,
|
||||
With<RigidBodyPositionSync>,
|
||||
),
|
||||
>,
|
||||
) {
|
||||
for (mut coordinates, position) in query.iter_mut() {
|
||||
|
@ -733,8 +717,7 @@ impl Plugin for CorePlugin {
|
|||
.add_startup_system(setup)
|
||||
.add_system_to_stage(
|
||||
CoreStage::PostUpdate,
|
||||
copy_coordinates_to_transform
|
||||
.before(TransformSystem::TransformPropagate),
|
||||
copy_coordinates_to_transform.before(TransformSystem::TransformPropagate),
|
||||
)
|
||||
.add_system_to_stage(
|
||||
CoreStage::PostUpdate,
|
||||
|
|
Loading…
Reference in New Issue
Block a user