diff options
| author | Arne Dußin | 2021-01-27 14:01:50 +0100 |
|---|---|---|
| committer | Arne Dußin | 2021-02-02 22:16:15 +0100 |
| commit | f92e9f6f07b1e3834c2ca58ce3510734819d08e4 (patch) | |
| tree | 20e3d3afce342a56ae98f6c20491482ccd2b5c6b /src/world/room.rs | |
| parent | c60a6d07efb120724b308e29e8e70f27c87c952d (diff) | |
| download | graf_karto-f92e9f6f07b1e3834c2ca58ce3510734819d08e4.tar.gz graf_karto-f92e9f6f07b1e3834c2ca58ce3510734819d08e4.zip | |
Rework graf karto to fit the client/server structure
Diffstat (limited to 'src/world/room.rs')
| -rw-r--r-- | src/world/room.rs | 52 |
1 files changed, 52 insertions, 0 deletions
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<f64>, +} + +impl Room { + /// Create a new room with the given shape. + pub fn new(shape: Polygon<f64>) -> Self { + Self { shape } + } + + /// Get the polygon this room is based on. + pub fn shape(&self) -> &Polygon<f64> { + &self.shape + } +} + +impl Component for Room { + fn bounding_rect(&self) -> Rect<f64> { + 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<f64>) { + for corner in self.shape.corners_mut() { + *corner = matrix + .transform_point(&Point2::new(corner.x, corner.y)) + .into(); + } + } +} |
