diff --git a/js/graphics.mjs b/js/graphics.mjs index cbda3ac..ccfc739 100644 --- a/js/graphics.mjs +++ b/js/graphics.mjs @@ -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); }); diff --git a/js/player.mjs b/js/player.mjs index e3fdbc1..dec8cd3 100644 --- a/js/player.mjs +++ b/js/player.mjs @@ -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", _ => { diff --git a/js/world.mjs b/js/world.mjs index 0ae1f14..e8f8bd8 100644 --- a/js/world.mjs +++ b/js/world.mjs @@ -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") {