Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rotation functions and diagonal/orthogonal check functions to Direction #461

Merged
merged 4 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
Unreleased
==========

### Additions:

- Add new geometry helper functions to `Direction`: `is_diagonal()`, `is_orthogonal()`,
`multi_rot()`, `rot_cw()`, and `rot_ccw()`

### Bugfixes:

- Fix incorrect setter name on `visualize_path_style` causing the setting to not work
- `OwnedStructure`, `OwnedStructureObject`, and `OwnedStructureProperties`'s `my` method now
correctly handles the value being undefined.
Expand Down
80 changes: 80 additions & 0 deletions src/constants/small_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{borrow::Cow, fmt};
use enum_iterator::Sequence;
use js_sys::JsString;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use serde::{
de::{Error as _, Unexpected},
Deserialize, Serialize,
Expand Down Expand Up @@ -115,6 +116,85 @@ pub enum Direction {
TopLeft = 8,
}

impl Direction {
/// Whether the direction is orthogonal.
///
/// Example usage:
///
/// ```
/// use screeps::Direction::*;
///
/// assert_eq!(Top.is_orthogonal(), true);
/// assert_eq!(TopRight.is_orthogonal(), false);
/// ```
pub fn is_orthogonal(self) -> bool {
use Direction::*;

matches!(self, Top | Right | Bottom | Left)
}

/// Whether the direction is diagonal.
///
/// Example usage:
///
/// ```
/// use screeps::Direction::*;
///
/// assert_eq!(Top.is_diagonal(), false);
/// assert_eq!(TopRight.is_diagonal(), true);
/// ```
pub fn is_diagonal(self) -> bool {
!self.is_orthogonal()
}

/// Rotate the direction by a specified number of steps clockwise if
/// positive or counter-clockwise if negative.
///
/// Example usage:
///
/// ```
/// use screeps::Direction::*;
///
/// assert_eq!(Top.multi_rot(1), TopRight);
/// assert_eq!(Top.multi_rot(2), Right);
/// assert_eq!(Top.multi_rot(-1), TopLeft);
/// assert_eq!(Top.multi_rot(-2), Left);
/// assert_eq!(Top.multi_rot(64), Top);
/// ```
pub fn multi_rot(self, times: i8) -> Self {
let raw_dir = ((self as u8) - 1).wrapping_add_signed(times) % 8 + 1;
// unwrap should be optimized away, as the integer we ended up with
// is always a valid value
Self::from_u8(raw_dir).unwrap()
}

/// Rotate the direction clockwise by one step.
///
/// Example usage:
///
/// ```
/// use screeps::Direction::*;
///
/// assert_eq!(Top.rot_cw(), TopRight);
/// ```
pub fn rot_cw(self) -> Self {
self.multi_rot(1)
}

/// Rotate the direction counter-clockwise by one step.
///
/// Example usage:
///
/// ```
/// use screeps::Direction::*;
///
/// assert_eq!(Top.rot_ccw(), TopLeft);
/// ```
pub fn rot_ccw(self) -> Self {
self.multi_rot(-1)
}
}

impl From<Direction> for (i32, i32) {
/// Returns the change in (x, y) when moving in each direction.
#[inline]
Expand Down