//! Polygon rooms are the standard rooms in graf karto. They can take the form of anything that a //! [Polygon](crate::math::Polygon) can have. use super::Component; use crate::math::{Polygon, Rect}; use crate::transformable::NonRigidTransformable; use nalgebra::{Matrix3, Point2}; use serde::{Deserialize, Serialize}; /// A polygon shaped room, which can be placed and modified in the world. #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Room { shape: Polygon, } impl Room { /// Create a new room with the given shape. pub fn new(shape: Polygon) -> Self { Self { shape } } /// Get the polygon this room is based on. pub fn shape(&self) -> &Polygon { &self.shape } } impl Component for Room { fn bounding_rect(&self) -> Rect { Rect::bounding_rect_n(&self.shape.corners()) } fn as_non_rigid(&self) -> Option<&dyn NonRigidTransformable> { Some(self as &dyn NonRigidTransformable) } fn as_non_rigid_mut(&mut self) -> Option<&mut dyn NonRigidTransformable> { Some(self as &mut dyn NonRigidTransformable) } } impl NonRigidTransformable for Room { // XXX: This produces undefined behaviour when using a mirroring matrix, since // the polygon corners must be sorted in counterclockwise direction. fn apply_matrix(&mut self, matrix: &Matrix3) { for corner in self.shape.corners_mut() { *corner = matrix .transform_point(&Point2::new(corner.x, corner.y)) .into(); } } }