pub struct RoomXY {
pub x: RoomCoordinate,
pub y: RoomCoordinate,
}
Expand description
An X/Y pair representing a given coordinate relative to any room.
Fields§
§x: RoomCoordinate
§y: RoomCoordinate
Implementations§
source§impl RoomXY
impl RoomXY
sourcepub unsafe fn unchecked_new(x: u8, y: u8) -> Self
pub unsafe fn unchecked_new(x: u8, y: u8) -> Self
Create a RoomXY
from a pair of u8
, without checking whether it’s in
the range of valid values.
§Safety
Calling this method with x >= ROOM_SIZE
or y >= ROOM_SIZE
can result
in undefined behaviour when the resulting RoomXY
is used.
sourcepub const fn is_room_edge(self) -> bool
pub const fn is_room_edge(self) -> bool
Get whether this coordinate pair represents an edge position (0 or 49 for either coordinate)
sourcepub fn checked_add(self, rhs: (i8, i8)) -> Option<RoomXY>
pub fn checked_add(self, rhs: (i8, i8)) -> Option<RoomXY>
Get the coordinate adjusted by a certain value, returning None
if the
result is outside the valid room area.
Example usage:
use screeps::local::RoomXY;
let zero = unsafe { RoomXY::unchecked_new(0, 0) };
let one = unsafe { RoomXY::unchecked_new(1, 1) };
let forty_nine = unsafe { RoomXY::unchecked_new(49, 49) };
assert_eq!(zero.checked_add((1, 1)), Some(one));
assert_eq!(zero.checked_add((-1, 0)), None);
assert_eq!(zero.checked_add((49, 49)), Some(forty_nine));
assert_eq!(forty_nine.checked_add((1, 1)), None);
sourcepub fn saturating_add(self, rhs: (i8, i8)) -> RoomXY
pub fn saturating_add(self, rhs: (i8, i8)) -> RoomXY
Get the coordinate adjusted by a certain value, saturating at the edges of the room if the result would be outside the valid room area.
Example usage:
use screeps::local::RoomXY;
let zero = unsafe { RoomXY::unchecked_new(0, 0) };
let one = unsafe { RoomXY::unchecked_new(1, 1) };
let forty_nine = unsafe { RoomXY::unchecked_new(49, 49) };
assert_eq!(zero.saturating_add((1, 1)), one);
assert_eq!(zero.saturating_add((-1, 0)), zero);
assert_eq!(zero.saturating_add((49, 49)), forty_nine);
assert_eq!(zero.saturating_add((i8::MAX, i8::MAX)), forty_nine);
assert_eq!(forty_nine.saturating_add((1, 1)), forty_nine);
assert_eq!(forty_nine.saturating_add((i8::MIN, i8::MIN)), zero);
sourcepub fn checked_add_direction(self, rhs: Direction) -> Option<RoomXY>
pub fn checked_add_direction(self, rhs: Direction) -> Option<RoomXY>
Get the neighbor of a given RoomXY
in the given direction, returning
None
if the result is outside the valid room area.
Example usage:
use screeps::{constants::Direction::*, local::RoomXY};
let zero = unsafe { RoomXY::unchecked_new(0, 0) };
let one = unsafe { RoomXY::unchecked_new(1, 1) };
let forty_nine = unsafe { RoomXY::unchecked_new(49, 49) };
assert_eq!(zero.checked_add_direction(BottomRight), Some(one));
assert_eq!(zero.checked_add_direction(TopLeft), None);
assert_eq!(one.checked_add_direction(TopLeft), Some(zero));
assert_eq!(forty_nine.checked_add_direction(BottomRight), None);
sourcepub fn saturating_add_direction(self, rhs: Direction) -> RoomXY
pub fn saturating_add_direction(self, rhs: Direction) -> RoomXY
Get the neighbor of a given RoomXY
in the given direction, saturating
at the edges if the result is outside the valid room area.
Example usage:
use screeps::{constants::Direction::*, local::RoomXY};
let zero = unsafe { RoomXY::unchecked_new(0, 0) };
let one = unsafe { RoomXY::unchecked_new(1, 1) };
let forty_nine = unsafe { RoomXY::unchecked_new(49, 49) };
assert_eq!(zero.saturating_add_direction(BottomRight), one);
assert_eq!(zero.saturating_add_direction(TopLeft), zero);
assert_eq!(one.saturating_add_direction(TopLeft), zero);
assert_eq!(forty_nine.saturating_add_direction(BottomRight), forty_nine);