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:
parent
a55eb310b0
commit
b886a3a965
|
@ -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));
|
draw_floor(start_x, start_y, scale, !checkerboard(start_x, start_y));
|
||||||
|
|
||||||
iter_2d(range(0, world.BOX_SIZE - 1), (x, y) => {
|
world.for_each_tile(box, (x, y, tile) => {
|
||||||
const tile = world.get_tile(box, x, y);
|
|
||||||
draw_tile(tile, x * scale + start_x, y * scale + start_y, scale);
|
draw_tile(tile, x * scale + start_x, y * scale + start_y, scale);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -33,9 +33,7 @@ on_press(" ", _ => {
|
||||||
});
|
});
|
||||||
|
|
||||||
function clear_tile (x, y) {
|
function clear_tile (x, y) {
|
||||||
world.set_tile(player.box, x, y, {
|
world.set_tile(player.box, x, y, world.empty_tile());
|
||||||
type: "empty",
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
on_press("Backspace", _ => {
|
on_press("Backspace", _ => {
|
||||||
|
|
13
js/world.mjs
13
js/world.mjs
|
@ -7,12 +7,18 @@ export const CENTER = Math.floor(BOX_SIZE / 2);
|
||||||
|
|
||||||
function create_world (size) {
|
function create_world (size) {
|
||||||
return {
|
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,
|
depth: 0,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const world = create_world(BOX_SIZE);
|
const world = create_world(BOX_SIZE);
|
||||||
|
|
||||||
|
export function empty_tile () {
|
||||||
|
return { type: "empty" };
|
||||||
|
}
|
||||||
|
|
||||||
export function create_new () {
|
export function create_new () {
|
||||||
return create_world(BOX_SIZE);
|
return create_world(BOX_SIZE);
|
||||||
}
|
}
|
||||||
|
@ -73,7 +79,10 @@ function reconstruct_parent (world, parent) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function from_json (json) {
|
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) => {
|
for_each_tile(result, (x, y, tile) => {
|
||||||
if (tile.type === "box") {
|
if (tile.type === "box") {
|
||||||
|
|
Loading…
Reference in New Issue