Update save game serialization to add previous maps

This commit is contained in:
Timothy Warren 2022-01-06 10:00:42 -05:00
parent b4fc2ba28f
commit 6cb4406080
3 changed files with 29 additions and 2 deletions

View File

@ -295,3 +295,8 @@ pub struct SerializeMe;
pub struct SerializationHelper { pub struct SerializationHelper {
pub map: crate::map::Map, pub map: crate::map::Map,
} }
#[derive(Component, Serialize, Deserialize, Clone)]
pub struct DMSerializationHelper {
pub map: super::map::MasterDungeonMap,
}

View File

@ -446,6 +446,7 @@ fn main() -> ::rltk::BError {
Confusion, Confusion,
Consumable, Consumable,
Door, Door,
DMSerializationHelper,
EntityMoved, EntityMoved,
EntryTrigger, EntryTrigger,
Equippable, Equippable,

View File

@ -10,7 +10,7 @@ use ::specs::saveload::{
}; };
use crate::components::*; use crate::components::*;
use crate::map::Map; use crate::map::{Map, MasterDungeonMap};
macro_rules! serialize_individually { macro_rules! serialize_individually {
($ecs:expr, $ser:expr, $data:expr, $( $type:ty),*,) => { ($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) { pub fn save_game(ecs: &mut World) {
// Create helper // Create helper
let mapcopy = ecs.get_mut::<Map>().unwrap().clone(); let mapcopy = ecs.get_mut::<Map>().unwrap().clone();
let dungeon_master = ecs.get_mut::<MasterDungeonMap>().unwrap().clone();
let savehelper = ecs let savehelper = ecs
.create_entity() .create_entity()
.with(SerializationHelper { map: mapcopy }) .with(SerializationHelper { map: mapcopy })
.marked::<SimpleMarker<SerializeMe>>() .marked::<SimpleMarker<SerializeMe>>()
.build(); .build();
let savehelper2 = ecs
.create_entity()
.with(DMSerializationHelper {
map: dungeon_master,
})
.marked::<SimpleMarker<SerializeMe>>()
.build();
// Actually serialize // Actually serialize
{ {
@ -61,6 +69,7 @@ pub fn save_game(ecs: &mut World) {
Confusion, Confusion,
Consumable, Consumable,
Door, Door,
DMSerializationHelper,
EntityMoved, EntityMoved,
EntryTrigger, EntryTrigger,
Equippable, Equippable,
@ -105,6 +114,8 @@ pub fn save_game(ecs: &mut World) {
// Clean up // Clean up
ecs.delete_entity(savehelper) ecs.delete_entity(savehelper)
.expect("Failed to clean up savehelper component"); .expect("Failed to clean up savehelper component");
ecs.delete_entity(savehelper2)
.expect("Failed to clean up savehelper2 component");
} }
pub fn does_save_exist() -> bool { pub fn does_save_exist() -> bool {
@ -162,6 +173,7 @@ pub fn load_game(ecs: &mut World) {
Confusion, Confusion,
Consumable, Consumable,
Door, Door,
DMSerializationHelper,
EntityMoved, EntityMoved,
EntryTrigger, EntryTrigger,
Equippable, Equippable,
@ -204,9 +216,11 @@ pub fn load_game(ecs: &mut World) {
} }
let mut deleteme: Option<Entity> = None; let mut deleteme: Option<Entity> = None;
let mut deleteme2: Option<Entity> = None;
{ {
let entities = ecs.entities(); let entities = ecs.entities();
let helper = ecs.read_storage::<SerializationHelper>(); let helper = ecs.read_storage::<SerializationHelper>();
let helper2 = ecs.read_storage::<DMSerializationHelper>();
let player = ecs.read_storage::<Player>(); let player = ecs.read_storage::<Player>();
let position = ecs.read_storage::<Position>(); let position = ecs.read_storage::<Position>();
@ -216,10 +230,15 @@ pub fn load_game(ecs: &mut World) {
worldmap.tile_content = vec![Vec::new(); (worldmap.height * worldmap.width) as usize]; worldmap.tile_content = vec![Vec::new(); (worldmap.height * worldmap.width) as usize];
deleteme = Some(e); deleteme = Some(e);
} }
for (e, h) in (&entities, &helper2).join() {
let mut dungeon_master = ecs.write_resource::<MasterDungeonMap>();
*dungeon_master = h.map.clone();
deleteme2 = Some(e);
}
for (e, _p, pos) in (&entities, &player, &position).join() { for (e, _p, pos) in (&entities, &player, &position).join() {
let mut ppos = ecs.write_resource::<Point>(); let mut ppos = ecs.write_resource::<Point>();
*ppos = Point::new(pos.x, pos.y); *ppos = Point::from(*pos);
let mut player_resource = ecs.write_resource::<Entity>(); let mut player_resource = ecs.write_resource::<Entity>();
*player_resource = e; *player_resource = e;
@ -228,6 +247,8 @@ pub fn load_game(ecs: &mut World) {
ecs.delete_entity(deleteme.unwrap()) ecs.delete_entity(deleteme.unwrap())
.expect("Unable to delete helper entity"); .expect("Unable to delete helper entity");
ecs.delete_entity(deleteme2.unwrap())
.expect("Unable to delete helper2 entity");
} }
pub fn delete_save() { pub fn delete_save() {