2021-12-10 20:16:48 -05:00
|
|
|
use std::fs;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::path::Path;
|
|
|
|
|
2021-12-24 10:38:44 -05:00
|
|
|
use ::rltk::Point;
|
|
|
|
use ::specs::error::NoError;
|
|
|
|
use ::specs::prelude::*;
|
|
|
|
use ::specs::saveload::{
|
2021-11-08 13:58:40 -05:00
|
|
|
DeserializeComponents, MarkedBuilder, SerializeComponents, SimpleMarker, SimpleMarkerAllocator,
|
|
|
|
};
|
2021-12-10 20:16:48 -05:00
|
|
|
|
|
|
|
use crate::components::*;
|
2021-12-24 10:38:44 -05:00
|
|
|
use crate::map::Map;
|
2021-11-08 13:58:40 -05:00
|
|
|
|
|
|
|
macro_rules! serialize_individually {
|
2021-11-18 15:25:29 -05:00
|
|
|
($ecs:expr, $ser:expr, $data:expr, $( $type:ty),*,) => {
|
2021-11-08 13:58:40 -05:00
|
|
|
$(
|
|
|
|
SerializeComponents::<NoError, SimpleMarker<SerializeMe>>::serialize(
|
|
|
|
&( $ecs.read_storage::<$type>(), ),
|
|
|
|
&$data.0,
|
|
|
|
&$data.1,
|
|
|
|
&mut $ser,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
)*
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-09 10:49:39 -05:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
pub fn save_game(_ecs: &mut World) {}
|
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-11-08 13:58:40 -05:00
|
|
|
pub fn save_game(ecs: &mut World) {
|
|
|
|
// Create helper
|
2021-12-24 10:38:44 -05:00
|
|
|
let mapcopy = ecs.get_mut::<Map>().unwrap().clone();
|
2021-11-08 13:58:40 -05:00
|
|
|
let savehelper = ecs
|
|
|
|
.create_entity()
|
|
|
|
.with(SerializationHelper { map: mapcopy })
|
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
|
|
|
.build();
|
|
|
|
|
|
|
|
// Actually serialize
|
|
|
|
{
|
|
|
|
let data = (
|
|
|
|
ecs.entities(),
|
|
|
|
ecs.read_storage::<SimpleMarker<SerializeMe>>(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let writer = File::create("./savegame.json").unwrap();
|
2021-12-24 10:38:44 -05:00
|
|
|
let mut serializer = ::serde_json::Serializer::new(writer);
|
2021-11-08 13:58:40 -05:00
|
|
|
serialize_individually!(
|
|
|
|
ecs,
|
|
|
|
serializer,
|
|
|
|
data,
|
|
|
|
Position,
|
|
|
|
Renderable,
|
|
|
|
Player,
|
|
|
|
Viewshed,
|
|
|
|
Monster,
|
|
|
|
Name,
|
|
|
|
BlocksTile,
|
|
|
|
CombatStats,
|
|
|
|
SufferDamage,
|
|
|
|
WantsToMelee,
|
|
|
|
Item,
|
|
|
|
Consumable,
|
|
|
|
Ranged,
|
|
|
|
InflictsDamage,
|
|
|
|
AreaOfEffect,
|
|
|
|
Confusion,
|
|
|
|
ProvidesHealing,
|
|
|
|
InBackpack,
|
|
|
|
WantsToPickupItem,
|
|
|
|
WantsToUseItem,
|
|
|
|
WantsToDropItem,
|
2021-11-15 09:19:22 -05:00
|
|
|
SerializationHelper,
|
2021-11-15 11:32:09 -05:00
|
|
|
Equippable,
|
|
|
|
Equipped,
|
|
|
|
MeleePowerBonus,
|
|
|
|
DefenseBonus,
|
2021-11-16 11:33:58 -05:00
|
|
|
WantsToRemoveItem,
|
2021-11-17 16:23:01 -05:00
|
|
|
ParticleLifetime,
|
2021-11-18 15:25:29 -05:00
|
|
|
HungerClock,
|
|
|
|
ProvidesFood,
|
2021-11-29 14:39:15 -05:00
|
|
|
MagicMapper,
|
2021-11-29 16:00:07 -05:00
|
|
|
Hidden,
|
|
|
|
EntryTrigger,
|
|
|
|
EntityMoved,
|
2021-12-01 09:46:34 -05:00
|
|
|
SingleActivation,
|
2021-12-17 14:07:14 -05:00
|
|
|
BlocksVisibility,
|
|
|
|
Door,
|
2021-12-24 13:23:56 -05:00
|
|
|
Bystander,
|
2021-11-08 13:58:40 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
ecs.delete_entity(savehelper)
|
|
|
|
.expect("Failed to clean up savehelper component");
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn does_save_exist() -> bool {
|
|
|
|
Path::new("./savegame.json").exists()
|
|
|
|
}
|
2021-11-09 10:29:23 -05:00
|
|
|
|
2021-11-15 13:55:31 -05:00
|
|
|
macro_rules! deserialize_individually {
|
2021-11-18 15:25:29 -05:00
|
|
|
($ecs:expr, $de:expr, $data:expr, $( $type:ty),*,) => {
|
2021-11-15 13:55:31 -05:00
|
|
|
$(
|
|
|
|
DeserializeComponents::<NoError, _>::deserialize(
|
|
|
|
&mut ( &mut $ecs.write_storage::<$type>(), ),
|
2021-11-16 10:45:39 -05:00
|
|
|
&$data.0, // entities
|
2021-11-15 13:55:31 -05:00
|
|
|
&mut $data.1, // marker
|
|
|
|
&mut $data.2, // allocater
|
|
|
|
&mut $de,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
)*
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-09 10:29:23 -05:00
|
|
|
pub fn load_game(ecs: &mut World) {
|
|
|
|
{
|
|
|
|
// Delete everything
|
|
|
|
let mut to_delete = Vec::new();
|
|
|
|
for e in ecs.entities().join() {
|
|
|
|
to_delete.push(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
for del in to_delete.iter() {
|
|
|
|
ecs.delete_entity(*del).expect("Failed to delete entity");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = fs::read_to_string("./savegame.json").unwrap();
|
2021-12-24 10:38:44 -05:00
|
|
|
let mut de = ::serde_json::Deserializer::from_str(&data);
|
2021-11-09 10:29:23 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut d = (
|
|
|
|
&mut ecs.entities(),
|
|
|
|
&mut ecs.write_storage::<SimpleMarker<SerializeMe>>(),
|
|
|
|
&mut ecs.write_resource::<SimpleMarkerAllocator<SerializeMe>>(),
|
|
|
|
);
|
|
|
|
|
|
|
|
deserialize_individually!(
|
|
|
|
ecs,
|
|
|
|
de,
|
|
|
|
d,
|
|
|
|
Position,
|
|
|
|
Renderable,
|
|
|
|
Player,
|
|
|
|
Viewshed,
|
|
|
|
Monster,
|
|
|
|
Name,
|
|
|
|
BlocksTile,
|
|
|
|
CombatStats,
|
|
|
|
SufferDamage,
|
|
|
|
WantsToMelee,
|
|
|
|
Item,
|
|
|
|
Consumable,
|
|
|
|
Ranged,
|
|
|
|
InflictsDamage,
|
|
|
|
AreaOfEffect,
|
|
|
|
Confusion,
|
|
|
|
ProvidesHealing,
|
|
|
|
InBackpack,
|
|
|
|
WantsToPickupItem,
|
|
|
|
WantsToUseItem,
|
|
|
|
WantsToDropItem,
|
2021-11-15 09:19:22 -05:00
|
|
|
SerializationHelper,
|
2021-11-15 11:32:09 -05:00
|
|
|
Equippable,
|
|
|
|
Equipped,
|
|
|
|
MeleePowerBonus,
|
|
|
|
DefenseBonus,
|
2021-11-16 11:33:58 -05:00
|
|
|
WantsToRemoveItem,
|
2021-11-17 16:23:01 -05:00
|
|
|
ParticleLifetime,
|
2021-11-18 15:25:29 -05:00
|
|
|
HungerClock,
|
|
|
|
ProvidesFood,
|
2021-11-29 14:39:15 -05:00
|
|
|
MagicMapper,
|
2021-11-29 16:00:07 -05:00
|
|
|
Hidden,
|
|
|
|
EntryTrigger,
|
|
|
|
EntityMoved,
|
2021-12-01 09:46:34 -05:00
|
|
|
SingleActivation,
|
2021-12-17 14:07:14 -05:00
|
|
|
BlocksVisibility,
|
|
|
|
Door,
|
2021-12-24 13:23:56 -05:00
|
|
|
Bystander,
|
2021-11-09 10:29:23 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut deleteme: Option<Entity> = None;
|
|
|
|
{
|
|
|
|
let entities = ecs.entities();
|
|
|
|
let helper = ecs.read_storage::<SerializationHelper>();
|
|
|
|
let player = ecs.read_storage::<Player>();
|
|
|
|
let position = ecs.read_storage::<Position>();
|
|
|
|
|
|
|
|
for (e, h) in (&entities, &helper).join() {
|
2021-12-24 10:38:44 -05:00
|
|
|
let mut worldmap = ecs.write_resource::<Map>();
|
2021-11-09 10:29:23 -05:00
|
|
|
*worldmap = h.map.clone();
|
2021-12-17 16:35:30 -05:00
|
|
|
worldmap.tile_content = vec![Vec::new(); (worldmap.height * worldmap.width) as usize];
|
2021-11-09 10:29:23 -05:00
|
|
|
deleteme = Some(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (e, _p, pos) in (&entities, &player, &position).join() {
|
2021-12-24 10:38:44 -05:00
|
|
|
let mut ppos = ecs.write_resource::<Point>();
|
|
|
|
*ppos = Point::new(pos.x, pos.y);
|
2021-11-09 10:29:23 -05:00
|
|
|
|
|
|
|
let mut player_resource = ecs.write_resource::<Entity>();
|
|
|
|
*player_resource = e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ecs.delete_entity(deleteme.unwrap())
|
2021-11-15 13:55:31 -05:00
|
|
|
.expect("Unable to delete helper entity");
|
2021-11-09 10:29:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete_save() {
|
|
|
|
if Path::new("./savegame.json").exists() {
|
|
|
|
std::fs::remove_file("./savegame.json").expect("Failed to delete save file.");
|
|
|
|
}
|
|
|
|
}
|