From 6cb44060807b92b2888ced9e353247ea515ef13d Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 6 Jan 2022 10:00:42 -0500 Subject: [PATCH] Update save game serialization to add previous maps --- src/components.rs | 5 +++++ src/main.rs | 1 + src/saveload_system.rs | 25 +++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/components.rs b/src/components.rs index bef2089..5ffe155 100644 --- a/src/components.rs +++ b/src/components.rs @@ -295,3 +295,8 @@ pub struct SerializeMe; pub struct SerializationHelper { pub map: crate::map::Map, } + +#[derive(Component, Serialize, Deserialize, Clone)] +pub struct DMSerializationHelper { + pub map: super::map::MasterDungeonMap, +} diff --git a/src/main.rs b/src/main.rs index d14ea06..d066272 100644 --- a/src/main.rs +++ b/src/main.rs @@ -446,6 +446,7 @@ fn main() -> ::rltk::BError { Confusion, Consumable, Door, + DMSerializationHelper, EntityMoved, EntryTrigger, Equippable, diff --git a/src/saveload_system.rs b/src/saveload_system.rs index bb97b7c..1017764 100644 --- a/src/saveload_system.rs +++ b/src/saveload_system.rs @@ -10,7 +10,7 @@ use ::specs::saveload::{ }; use crate::components::*; -use crate::map::Map; +use crate::map::{Map, MasterDungeonMap}; macro_rules! serialize_individually { ($ecs:expr, $ser:expr, $data:expr, $( $type:ty),*,) => { @@ -33,11 +33,19 @@ pub fn save_game(_ecs: &mut World) {} pub fn save_game(ecs: &mut World) { // Create helper let mapcopy = ecs.get_mut::().unwrap().clone(); + let dungeon_master = ecs.get_mut::().unwrap().clone(); let savehelper = ecs .create_entity() .with(SerializationHelper { map: mapcopy }) .marked::>() .build(); + let savehelper2 = ecs + .create_entity() + .with(DMSerializationHelper { + map: dungeon_master, + }) + .marked::>() + .build(); // Actually serialize { @@ -61,6 +69,7 @@ pub fn save_game(ecs: &mut World) { Confusion, Consumable, Door, + DMSerializationHelper, EntityMoved, EntryTrigger, Equippable, @@ -105,6 +114,8 @@ pub fn save_game(ecs: &mut World) { // Clean up ecs.delete_entity(savehelper) .expect("Failed to clean up savehelper component"); + ecs.delete_entity(savehelper2) + .expect("Failed to clean up savehelper2 component"); } pub fn does_save_exist() -> bool { @@ -162,6 +173,7 @@ pub fn load_game(ecs: &mut World) { Confusion, Consumable, Door, + DMSerializationHelper, EntityMoved, EntryTrigger, Equippable, @@ -204,9 +216,11 @@ pub fn load_game(ecs: &mut World) { } let mut deleteme: Option = None; + let mut deleteme2: Option = None; { let entities = ecs.entities(); let helper = ecs.read_storage::(); + let helper2 = ecs.read_storage::(); let player = ecs.read_storage::(); let position = ecs.read_storage::(); @@ -216,10 +230,15 @@ pub fn load_game(ecs: &mut World) { worldmap.tile_content = vec![Vec::new(); (worldmap.height * worldmap.width) as usize]; deleteme = Some(e); } + for (e, h) in (&entities, &helper2).join() { + let mut dungeon_master = ecs.write_resource::(); + *dungeon_master = h.map.clone(); + deleteme2 = Some(e); + } for (e, _p, pos) in (&entities, &player, &position).join() { let mut ppos = ecs.write_resource::(); - *ppos = Point::new(pos.x, pos.y); + *ppos = Point::from(*pos); let mut player_resource = ecs.write_resource::(); *player_resource = e; @@ -228,6 +247,8 @@ pub fn load_game(ecs: &mut World) { ecs.delete_entity(deleteme.unwrap()) .expect("Unable to delete helper entity"); + ecs.delete_entity(deleteme2.unwrap()) + .expect("Unable to delete helper2 entity"); } pub fn delete_save() {