Save and Load

<
>
September 11, 2021

I added support for saving and loading games. For this I introduced

pub struct PersistedGame {
    pub planet: PersistedPlanet,
    ...
}

which holds all data of a game that should be serialized. Converting the field’s types if necessary (certain collections, ids instead of references).
I also added the necessary conversions to and from PersistedGame.

impl Into<PersistedGame> for &Game {
    fn into(self) -> PersistedGame {
        PersistedGame {
            planet: (&self.planet).into(),
            ...
        }
    }
}
...
impl Game {
    ...
    pub fn from_persisted(
        x: PersistedGame,
        shapes: GlShapes,
        ...
    ) -> Self {
        ...
    }
}

With serde it’s then trivial to save PersistedGame to for example a .json file.
In the future I will also need to version the save games or even support conversions of older save games. But this early in development the current approach is good enough.