From f92e9f6f07b1e3834c2ca58ce3510734819d08e4 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Wed, 27 Jan 2021 14:01:50 +0100 Subject: Rework graf karto to fit the client/server structure --- src/world/room.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/world/room.rs (limited to 'src/world/room.rs') diff --git a/src/world/room.rs b/src/world/room.rs new file mode 100644 index 0000000..fed8890 --- /dev/null +++ b/src/world/room.rs @@ -0,0 +1,52 @@ +//! 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(); + } + } +} -- cgit v1.2.3-70-g09d2