51 lines
1.0 KiB
JavaScript
51 lines
1.0 KiB
JavaScript
import { iter_2d, range } from "./utils/range.mjs";
|
|
|
|
export const BOX_SIZE = 9;
|
|
export const CENTER = Math.floor(BOX_SIZE / 2);
|
|
|
|
|
|
|
|
function create_world (size) {
|
|
return {
|
|
tiles: Array(size).fill(0).map(_ => Array(size).fill(0)),
|
|
};
|
|
}
|
|
const world = create_world(BOX_SIZE);
|
|
|
|
export function create_new () {
|
|
return create_world(BOX_SIZE);
|
|
}
|
|
|
|
export function set_tile (world, x, y, tile) {
|
|
if (out_of_bounds(x, y)) return;
|
|
world.tiles[x][y] = tile;
|
|
}
|
|
|
|
export function get_tile (world, x, y) {
|
|
if (out_of_bounds(x, y)) return { type: "out_of_bounds" };
|
|
return world.tiles[x][y];
|
|
}
|
|
|
|
export function create_box (parent) {
|
|
return {
|
|
...create_world(BOX_SIZE),
|
|
parent,
|
|
};
|
|
}
|
|
|
|
export function get_root () {
|
|
return world;
|
|
}
|
|
|
|
export function out_of_bounds (x, y) {
|
|
return x < 0 || x >= BOX_SIZE ||
|
|
y < 0 || y >= BOX_SIZE
|
|
}
|
|
|
|
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));
|
|
});
|
|
}
|