diff options
Diffstat (limited to 'src/map')
| -rw-r--r-- | src/map/mappable.rs | 8 | ||||
| -rw-r--r-- | src/map/polygon_room.rs | 26 | ||||
| -rw-r--r-- | src/map/rect_room.rs | 18 | ||||
| -rw-r--r-- | src/map/wall.rs | 29 |
4 files changed, 39 insertions, 42 deletions
diff --git a/src/map/mappable.rs b/src/map/mappable.rs index b348c4b..323361a 100644 --- a/src/map/mappable.rs +++ b/src/map/mappable.rs @@ -2,8 +2,8 @@ //! dimension on the map and may be scaleable use crate::math::Rect; -use crate::scaleable::Scaleable; use crate::transform::Transform; +use crate::transformable::NonRigidTransformable; use raylib::drawing::RaylibDrawHandle; pub trait Mappable { @@ -21,7 +21,11 @@ pub trait Mappable { /// Get the rectangle that contains the mappable object in its entirety without excess. fn bounding_rect(&self) -> Rect<f64>; - fn as_scaleable(&self) -> Option<&dyn Scaleable> { + fn as_non_rigid(&self) -> Option<&dyn NonRigidTransformable> { + None + } + + fn as_non_rigid_mut(&mut self) -> Option<&mut dyn NonRigidTransformable> { None } } diff --git a/src/map/polygon_room.rs b/src/map/polygon_room.rs index a209a7c..65eeaab 100644 --- a/src/map/polygon_room.rs +++ b/src/map/polygon_room.rs @@ -1,9 +1,10 @@ use super::Mappable; use crate::colours::DEFAULT_COLOURS; -use crate::math::{self, Polygon, Rect, Triangle, Vec2}; -use crate::scaleable::Scaleable; +use crate::math::{self, Polygon, Rect, Triangle}; use crate::transform::Transform; +use crate::transformable::NonRigidTransformable; use raylib::drawing::{RaylibDraw, RaylibDrawHandle}; +use nalgebra::{Matrix3, Point2}; pub type PolygonRoomData = Polygon<f64>; @@ -56,20 +57,21 @@ impl Mappable for PolygonRoom { Rect::bounding_rect_n(&self.data.corners()) } - fn as_scaleable(&self) -> Option<&dyn Scaleable> { - Some(self as &dyn Scaleable) + fn as_non_rigid(&self) -> Option<&dyn NonRigidTransformable> { + Some(self as &dyn NonRigidTransformable) } -} -impl Scaleable for PolygonRoom { - fn scale(&mut self, by: &Vec2<f64>) { - if by.x < 0. || by.y < 0. { - panic!("Cannot set dimensions with negative size"); - } + fn as_non_rigid_mut(&mut self) -> Option<&mut dyn NonRigidTransformable> { + Some(self as &mut dyn NonRigidTransformable) + } +} +impl NonRigidTransformable for PolygonRoom { + fn apply_matrix(&mut self, matrix: &Matrix3<f64>) { for corner in self.data.corners_mut() { - corner.x *= by.x; - corner.y *= by.y; + *corner = matrix + .transform_point(&Point2::new(corner.x, corner.y)) + .into(); } self.retriangulate(); diff --git a/src/map/rect_room.rs b/src/map/rect_room.rs index 5008c63..ee184fb 100644 --- a/src/map/rect_room.rs +++ b/src/map/rect_room.rs @@ -1,7 +1,6 @@ use crate::colours::DEFAULT_COLOURS; use crate::map::Mappable; -use crate::math::{Rect, Vec2}; -use crate::scaleable::Scaleable; +use crate::math::Rect; use crate::transform::Transform; use raylib::drawing::{RaylibDraw, RaylibDrawHandle}; use serde::Serialize; @@ -47,21 +46,6 @@ impl Mappable for RectRoom { fn bounding_rect(&self) -> Rect<f64> { self.data.clone() } - - fn as_scaleable(&self) -> Option<&dyn Scaleable> { - Some(self as &dyn Scaleable) - } -} - -impl Scaleable for RectRoom { - fn scale(&mut self, by: &Vec2<f64>) { - if by.x < 0. || by.y < 0. { - panic!("Cannot set dimensions with negative size"); - } - - self.data.x *= by.x; - self.data.y *= by.y; - } } impl Deref for RectRoom { diff --git a/src/map/wall.rs b/src/map/wall.rs index 22393bb..d18096b 100644 --- a/src/map/wall.rs +++ b/src/map/wall.rs @@ -1,10 +1,11 @@ use super::Mappable; use crate::colours::DEFAULT_COLOURS; use crate::math::{LineSegment, Rect, Vec2}; -use crate::scaleable::Scaleable; use crate::transform::Transform; +use crate::transformable::NonRigidTransformable; use raylib::drawing::{RaylibDraw, RaylibDrawHandle}; use std::ops::{Deref, DerefMut}; +use nalgebra::{Matrix3, Point2}; pub type WallData = LineSegment<f64>; @@ -81,18 +82,24 @@ impl Mappable for Wall { fn bounding_rect(&self) -> Rect<f64> { Rect::bounding_rect(self.data.start, self.data.end) } -} -impl Scaleable for Wall { - fn scale(&mut self, by: &Vec2<f64>) { - if by.x <= 0. || by.y <= 0. { - panic!("Cannot set dimensions with negative size"); - } + 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) + } +} - self.data.start.x *= by.x; - self.data.start.y *= by.y; - self.data.end.x *= by.x; - self.data.end.y *= by.y; +impl NonRigidTransformable for Wall { + fn apply_matrix(&mut self, matrix: &Matrix3<f64>) { + self.data.start = matrix + .transform_point(&self.data.start.into()) + .into(); + self.data.end = matrix + .transform_point(&self.data.end.into()) + .into(); } } |
