From 9799d3c6a8f0c242668203a1c70d7b6cfed3e855 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Tue, 15 Dec 2020 00:46:54 +0100 Subject: Refactor to make interaction between tools easier --- src/map/polygon_room.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/map/polygon_room.rs (limited to 'src/map/polygon_room.rs') diff --git a/src/map/polygon_room.rs b/src/map/polygon_room.rs new file mode 100644 index 0000000..12c480b --- /dev/null +++ b/src/map/polygon_room.rs @@ -0,0 +1,68 @@ +use super::Mappable; +use crate::math::{self, Rect, Polygon, Triangle, Vec2}; +use crate::scaleable::Scaleable; +use crate::transform::Transform; +use raylib::drawing::{RaylibDraw, RaylibDrawHandle}; +use raylib::ffi::Color; + +pub type PolygonRoomData = Polygon; + +pub struct PolygonRoom { + data: PolygonRoomData, + // The polygon shape, but in triangles, so the polygon does not have to be triangulated every frame. + triangulated: Vec>, +} + +impl PolygonRoom { + pub fn from_data(data: PolygonRoomData) -> Self { + Self { + data: data.clone(), + triangulated: math::triangulate(data) + } + } + + fn retriangulate(&mut self) { + self.triangulated = math::triangulate(self.data.clone()); + } +} + +impl Mappable for PolygonRoom { + fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) { + for triangle in &self.triangulated { + rld.draw_triangle( + transform.point_m_to_px(&triangle.corners()[0]), + transform.point_m_to_px(&triangle.corners()[1]), + transform.point_m_to_px(&triangle.corners()[2]), + Color { + r: 180, + g: 180, + b: 180, + a: 255, + }, + ) + } + } + + fn bounding_rect(&self) -> Rect { + Rect::bounding_rect_n(&self.data.corners()) + } + + fn as_scaleable(&self) -> Option<&dyn Scaleable> { + Some(self as &dyn Scaleable) + } +} + +impl Scaleable for PolygonRoom { + fn scale(&mut self, by: &Vec2) { + if by.x < 0. || by.y < 0. { + panic!("Cannot set dimensions with negative size"); + } + + for corner in self.data.corners_mut() { + corner.x *= by.x; + corner.y *= by.y; + } + + self.retriangulate(); + } +} -- cgit v1.2.3-70-g09d2