fix world save & load bug.

previously, serialization of world data couldn't handle boxes, since
they contain references to their parents, meaning worlds are cyclic.
this commit fixes this, and changes world loading to correctly re-assign
parents to boxes after deserializing them.

this bug is what happens when you commit without testing for more than a
few seconds.
This commit is contained in:
transoptimal 2023-06-24 18:10:58 +02:00
parent 5549817f6c
commit f64679ca43
1 changed files with 6 additions and 3 deletions

View File

@ -63,18 +63,21 @@ export function to_json () {
}
function reconstruct_parent (world, parent) {
world.parent = parent;
for_each_tile(world, (x, y, tile) => {
if (tile.type !== "box") return;
tile.parent = parent;
reconstruct_parent(tile.box, world);
});
}
export function from_json (json) {
const result = JSON.parse(json);
for_each_tile(result, (x, y, tile) => {
if (tile.type !== "box") return;
reconstruct_parent(tile.box, result);
if (tile.type === "box") {
reconstruct_parent(tile.box, result);
}
});
return result;
}