aboutsummaryrefslogtreecommitdiff
path: root/src/map
diff options
context:
space:
mode:
Diffstat (limited to 'src/map')
-rw-r--r--src/map/data.rs33
-rw-r--r--src/map/mod.rs49
-rw-r--r--src/map/polygon_room.rs14
-rw-r--r--src/map/rect_room.rs2
4 files changed, 93 insertions, 5 deletions
diff --git a/src/map/data.rs b/src/map/data.rs
index 1031d3c..f7ec484 100644
--- a/src/map/data.rs
+++ b/src/map/data.rs
@@ -1,6 +1,6 @@
//! Module containing the raw map data version of the map.
-use super::{IconData, PolygonRoomData, RectRoomData, WallData};
+use super::{IconData, Map, PolygonRoomData, RectRoomData, WallData};
use ron::de::from_reader;
use ron::ser::{to_string_pretty, PrettyConfig};
use serde::{Deserialize, Serialize};
@@ -35,8 +35,37 @@ impl MapData {
}
}
+ /// Creates a data struct from the Map. It is important to note, that this data element is not
+ /// bound to the Map in any way after this, so changing anything won't change anything in the map.
+ /// It is useful however to for instance serialize this map without extra rendering information
+ /// included.
+ pub fn extract_data(map: &Map) -> Self {
+ Self {
+ rect_rooms: map
+ .rect_rooms()
+ .iter()
+ .map(|r| (r as &RectRoomData).clone())
+ .collect(),
+ polygon_rooms: map
+ .polygon_rooms()
+ .iter()
+ .map(|p| (p as &PolygonRoomData).clone())
+ .collect(),
+ walls: map
+ .walls()
+ .iter()
+ .map(|w| (w as &WallData).clone())
+ .collect(),
+ icons: map
+ .icons()
+ .iter()
+ .map(|i| (i as &IconData).clone())
+ .collect(),
+ }
+ }
+
/// Load the map data from a file. Fails if the file does not exist or cannot be correctly parsed.
- pub fn load_from_file<P: AsRef<Path>>(&mut self, path: P) -> io::Result<Self> {
+ pub fn load_from_file<P: AsRef<Path>>(path: P) -> io::Result<Self> {
let file = File::open(&path)?;
let data: Self = match from_reader(file) {
Ok(data) => data,
diff --git a/src/map/mod.rs b/src/map/mod.rs
index 88a7e6c..70f65b3 100644
--- a/src/map/mod.rs
+++ b/src/map/mod.rs
@@ -146,4 +146,53 @@ impl Map {
.chain(self.walls.iter_mut().map(|w| w as &mut dyn Mappable))
.chain(self.icons.iter_mut().map(|i| i as &mut dyn Mappable))
}
+
+ /// Get the rectangular rooms of this map.
+ pub fn rect_rooms(&self) -> &Vec<RectRoom> {
+ &self.rect_rooms
+ }
+
+ /// Get the polygon rooms of this map.
+ pub fn polygon_rooms(&self) -> &Vec<PolygonRoom> {
+ &self.polygon_rooms
+ }
+
+ /// Get the walls of this map.
+ pub fn walls(&self) -> &Vec<Wall> {
+ &self.walls
+ }
+
+ /// Get the icons of this map.
+ pub fn icons(&self) -> &Vec<Icon> {
+ &self.icons
+ }
+
+ /// Replace the internal map data with the data provided. (Load and replace)
+ pub fn set_data(&mut self, data: MapData) {
+ // Remove all data.
+ self.icons.clear();
+ self.polygon_rooms.clear();
+ self.rect_rooms.clear();
+ self.walls.clear();
+
+ // Add all data from the map data.
+ self.add_data(data);
+ }
+
+ /// Add the data provided to the current data on the map. All elements will remain, with the
+ /// additional elements being pushed also.
+ pub fn add_data(&mut self, data: MapData) {
+ for i in data.icons {
+ self.push_icon(Icon::from_data(i, self.icon_renderer.clone()))
+ }
+ for p in data.polygon_rooms {
+ self.push_polygon_room(p);
+ }
+ for r in data.rect_rooms {
+ self.push_rect_room(r);
+ }
+ for w in data.walls {
+ self.push_wall(w);
+ }
+ }
}
diff --git a/src/map/polygon_room.rs b/src/map/polygon_room.rs
index fd4122e..2a29436 100644
--- a/src/map/polygon_room.rs
+++ b/src/map/polygon_room.rs
@@ -6,8 +6,10 @@ use crate::colours::DEFAULT_COLOURS;
use crate::math::{self, Polygon, Rect, Triangle};
use crate::transform::Transform;
use crate::transformable::NonRigidTransformable;
+use crate::FLOAT_MARGIN;
use nalgebra::{Matrix3, Point2};
use raylib::drawing::{RaylibDraw, RaylibDrawHandle};
+use std::ops::Deref;
/// Data type for the Polygon room.
pub type PolygonRoomData = Polygon<f64>;
@@ -25,7 +27,7 @@ impl PolygonRoom {
pub fn from_data(data: PolygonRoomData) -> Self {
Self {
data: data.clone(),
- triangulated: math::triangulate(data),
+ triangulated: math::triangulate(data, FLOAT_MARGIN),
selected: false,
}
}
@@ -33,7 +35,7 @@ impl PolygonRoom {
// When the internal polygon changes, it must be retriangulated to be drawn on the screen
// properly, so this function must be called any time that happens.
fn retriangulate(&mut self) {
- self.triangulated = math::triangulate(self.data.clone());
+ self.triangulated = math::triangulate(self.data.clone(), FLOAT_MARGIN);
}
}
@@ -85,3 +87,11 @@ impl NonRigidTransformable for PolygonRoom {
self.retriangulate();
}
}
+
+impl Deref for PolygonRoom {
+ type Target = PolygonRoomData;
+
+ fn deref(&self) -> &Self::Target {
+ &self.data
+ }
+}
diff --git a/src/map/rect_room.rs b/src/map/rect_room.rs
index 6ed3ed6..ae10327 100644
--- a/src/map/rect_room.rs
+++ b/src/map/rect_room.rs
@@ -49,7 +49,7 @@ impl Mappable for RectRoom {
}
fn bounding_rect(&self) -> Rect<f64> {
- self.data.clone()
+ self.data
}
}