From 27baaceaea0227ebb5de5225ed8ac622ce3774b4 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Fri, 11 Feb 2022 08:44:41 -0600 Subject: [PATCH] Ditch constructors in favor of `From` conversions where it makes sense. --- src/core.rs | 63 +++++++++++++++++++---------------------------------- 1 file changed, 23 insertions(+), 40 deletions(-) diff --git a/src/core.rs b/src/core.rs index 9cb07a8..772da13 100644 --- a/src/core.rs +++ b/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 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 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 for MovementDirection { @@ -268,16 +258,10 @@ pub enum CardinalDirection { West, } -impl CardinalDirection { - pub fn new(heading: f32) -> Self { +impl From 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 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, With), + ( + Changed, + With, + ), >, ) { 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,