2023-06-24 15:24:57 +00:00
|
|
|
import { iter_2d, range } from "./utils/range.mjs";
|
|
|
|
|
2023-06-23 09:37:25 +00:00
|
|
|
export const BOX_SIZE = 9;
|
2023-06-23 10:05:40 +00:00
|
|
|
export const CENTER = Math.floor(BOX_SIZE / 2);
|
2023-06-23 09:37:25 +00:00
|
|
|
|
|
|
|
|
2023-06-23 10:05:40 +00:00
|
|
|
|
|
|
|
function create_world (size) {
|
2023-06-24 08:03:32 +00:00
|
|
|
return {
|
|
|
|
tiles: Array(BOX_SIZE).fill(0).map(_ => Array(BOX_SIZE).fill(0)),
|
|
|
|
};
|
2023-06-23 10:05:40 +00:00
|
|
|
}
|
|
|
|
const world = create_world(BOX_SIZE);
|
2023-06-23 09:37:25 +00:00
|
|
|
|
2023-06-24 08:03:32 +00:00
|
|
|
export function set_tile (world, x, y, tile) {
|
2023-06-24 10:46:52 +00:00
|
|
|
if (out_of_bounds(x, y)) return;
|
2023-06-24 08:03:32 +00:00
|
|
|
world.tiles[x][y] = tile;
|
2023-06-23 09:37:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 08:03:32 +00:00
|
|
|
export function get_tile (world, x, y) {
|
2023-06-24 10:46:52 +00:00
|
|
|
if (out_of_bounds(x, y)) return { type: "out_of_bounds" };
|
2023-06-24 08:03:32 +00:00
|
|
|
return world.tiles[x][y];
|
2023-06-23 10:05:40 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 08:03:32 +00:00
|
|
|
export function create_box (parent) {
|
|
|
|
return {
|
|
|
|
...create_world(BOX_SIZE),
|
|
|
|
parent,
|
|
|
|
};
|
2023-06-23 10:05:40 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 07:52:53 +00:00
|
|
|
export function get_root () {
|
|
|
|
return world;
|
|
|
|
}
|
2023-06-24 10:46:52 +00:00
|
|
|
|
|
|
|
export function out_of_bounds (x, y) {
|
|
|
|
return x < 0 || x >= BOX_SIZE ||
|
|
|
|
y < 0 || y >= BOX_SIZE
|
|
|
|
}
|
2023-06-24 15:24:57 +00:00
|
|
|
|
|
|
|
export function replace_root (new_world) {
|
|
|
|
// maybe unnecessary to go over each tile instead of just each column.
|
|
|
|
iter_2d(range(0, BOX_SIZE - 1), (x, y) => {
|
|
|
|
set_tile(world, x, y, get_tile(new_world, x, y));
|
|
|
|
});
|
|
|
|
}
|