use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct WorldPoint {
pub x: f32,
pub y: f32,
pub z: f32,
}
impl WorldPoint {
pub fn new(x: f32, y: f32, z: f32) -> Self {
Self { x, y, z }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct GridPoint {
pub x: i32,
pub y: i32,
}
impl GridPoint {
pub fn new(x: i32, y: i32) -> Self {
Self { x, y }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct GridSize {
pub width: u32,
pub height: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RoomBounds {
pub min: GridPoint,
pub max: GridPoint,
}
impl RoomBounds {
pub fn width(&self) -> u32 {
(i64::from(self.max.x) - i64::from(self.min.x))
.unsigned_abs()
.min(u64::from(u32::MAX)) as u32
}
pub fn height(&self) -> u32 {
(i64::from(self.max.y) - i64::from(self.min.y))
.unsigned_abs()
.min(u64::from(u32::MAX)) as u32
}
pub fn center(&self) -> GridPoint {
GridPoint {
x: ((i64::from(self.min.x) + i64::from(self.max.x)) / 2) as i32,
y: ((i64::from(self.min.y) + i64::from(self.max.y)) / 2) as i32,
}
}
pub fn contains(&self, point: GridPoint) -> bool {
point.x >= self.min.x
&& point.x < self.max.x
&& point.y >= self.min.y
&& point.y < self.max.y
}
pub fn intersects(&self, other: &RoomBounds) -> bool {
self.min.x < other.max.x
&& self.max.x > other.min.x
&& self.min.y < other.max.y
&& self.max.y > other.min.y
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum CardinalDir {
North,
South,
East,
West,
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Transform3 {
pub position: WorldPoint,
pub rotation: (f32, f32, f32),
pub scale: (f32, f32, f32),
}
impl Transform3 {
pub fn new(position: WorldPoint, rotation: (f32, f32, f32), scale: (f32, f32, f32)) -> Self {
Self {
position,
rotation,
scale,
}
}
}
impl Default for Transform3 {
fn default() -> Self {
Self {
position: WorldPoint {
x: 0.0,
y: 0.0,
z: 0.0,
},
rotation: (0.0, 0.0, 0.0),
scale: (1.0, 1.0, 1.0),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Bounds3 {
pub min: WorldPoint,
pub max: WorldPoint,
}
impl Bounds3 {
pub fn new(min: WorldPoint, max: WorldPoint) -> Self {
Self { min, max }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_room_bounds_width_height_do_not_overflow_extreme_coordinates() {
let bounds = RoomBounds {
min: GridPoint {
x: i32::MIN,
y: i32::MIN,
},
max: GridPoint {
x: i32::MAX,
y: i32::MAX,
},
};
assert_eq!(bounds.width(), u32::MAX);
assert_eq!(bounds.height(), u32::MAX);
}
#[test]
fn test_room_bounds_center_uses_wide_arithmetic() {
let bounds = RoomBounds {
min: GridPoint {
x: i32::MAX - 1,
y: i32::MAX - 1,
},
max: GridPoint {
x: i32::MAX,
y: i32::MAX,
},
};
assert_eq!(
bounds.center(),
GridPoint {
x: i32::MAX - 1,
y: i32::MAX - 1,
}
);
}
}