From 61d255a420c9d977b46670e7fa9e7735d2acf819 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Wed, 6 Jan 2021 21:32:48 +0100 Subject: Add CLI with save feature --- src/map/data.rs | 31 ++++++++++++++++++++++++++++++- src/map/mod.rs | 16 ++++++++++++++++ src/map/polygon_room.rs | 9 +++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) (limited to 'src/map') diff --git a/src/map/data.rs b/src/map/data.rs index 1031d3c..b7719cd 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,6 +35,35 @@ 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>(&mut self, path: P) -> io::Result { let file = File::open(&path)?; diff --git a/src/map/mod.rs b/src/map/mod.rs index 88a7e6c..28025ad 100644 --- a/src/map/mod.rs +++ b/src/map/mod.rs @@ -146,4 +146,20 @@ 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)) } + + pub fn rect_rooms(&self) -> &Vec { + &self.rect_rooms + } + + pub fn polygon_rooms(&self) -> &Vec { + &self.polygon_rooms + } + + pub fn walls(&self) -> &Vec { + &self.walls + } + + pub fn icons(&self) -> &Vec { + &self.icons + } } diff --git a/src/map/polygon_room.rs b/src/map/polygon_room.rs index fd4122e..a57f5e4 100644 --- a/src/map/polygon_room.rs +++ b/src/map/polygon_room.rs @@ -8,6 +8,7 @@ use crate::transform::Transform; use crate::transformable::NonRigidTransformable; use nalgebra::{Matrix3, Point2}; use raylib::drawing::{RaylibDraw, RaylibDrawHandle}; +use std::ops::Deref; /// Data type for the Polygon room. pub type PolygonRoomData = Polygon; @@ -85,3 +86,11 @@ impl NonRigidTransformable for PolygonRoom { self.retriangulate(); } } + +impl Deref for PolygonRoom { + type Target = PolygonRoomData; + + fn deref(&self) -> &Self::Target { + &self.data + } +} -- cgit v1.2.3-70-g09d2