make representation of empty tiles consistent.

previously, world generation would represent empty tiles as the number
0, while other code would expect them to be an object with its `type`
property set to `"empty"`. the object representation is now used
consistently.
This commit is contained in:
HyperOnion 2023-06-28 15:04:09 +02:00
parent a55eb310b0
commit b886a3a965
3 changed files with 13 additions and 7 deletions

View File

@ -37,8 +37,7 @@ export function draw_world (box, start_x = 0, start_y = 0, scale = BASE_SCALE) {
draw_floor(start_x, start_y, scale, !checkerboard(start_x, start_y));
iter_2d(range(0, world.BOX_SIZE - 1), (x, y) => {
const tile = world.get_tile(box, x, y);
world.for_each_tile(box, (x, y, tile) => {
draw_tile(tile, x * scale + start_x, y * scale + start_y, scale);
});

View File

@ -33,9 +33,7 @@ on_press(" ", _ => {
});
function clear_tile (x, y) {
world.set_tile(player.box, x, y, {
type: "empty",
});
world.set_tile(player.box, x, y, world.empty_tile());
}
on_press("Backspace", _ => {

View File

@ -7,12 +7,18 @@ export const CENTER = Math.floor(BOX_SIZE / 2);
function create_world (size) {
return {
tiles: Array(size).fill(0).map(_ => Array(size).fill(0)),
tiles: Array(size).fill(0).map(_ =>
Array(size).fill(0).map(_ => empty_tile())
),
depth: 0,
};
}
const world = create_world(BOX_SIZE);
export function empty_tile () {
return { type: "empty" };
}
export function create_new () {
return create_world(BOX_SIZE);
}
@ -73,7 +79,10 @@ function reconstruct_parent (world, parent) {
}
export function from_json (json) {
const result = JSON.parse(json);
const result = JSON.parse(json, (key, value) => {
if (value === undefined || value === 0 || value === null) return empty_tile();
return value;
});
for_each_tile(result, (x, y, tile) => {
if (tile.type === "box") {