boxes/js/tile_update.mjs

48 lines
1.1 KiB
JavaScript

import * as world from "/js/world.mjs";
import { get_player_box } from "/js/player.mjs";
import * as random from "/js/utils/random.mjs";
// lots of this code is very hacky temporary for testing things out & getting started with tile updates.
export function tick (box, delta_time) {
world.for_each_tile(box, (x, y, tile) => {
if (tile_tickers.has(tile.type)) {
tile_tickers.get(tile.type)(box, x, y, delta_time);
}
});
}
const tile_tickers = new Map();
const dirs = [
[ -1, 0 ],
[ 1, 0 ],
[ 0, -1 ],
[ 0, 1 ],
];
// this might become terrible for performance eventually.
tile_tickers.set("box", (box, x, y, delta_time) => {
tick(world.get_tile(box, x, y).box, delta_time);
});
tile_tickers.set("grass", (box, x, y, delta_time) => {
const tile = world.get_tile(box, x, y);
if (Math.random() > 0.8 ** delta_time) {
const dir = random.item(dirs);
const spaces = random.shuffle(dirs)
.map(([d_x, d_y]) => [x + d_x, y + d_y]);
for (const [new_x, new_y] of spaces) {
if (world.get_tile(box, new_x, new_y).type === "empty") {
world.set_tile(box, new_x, new_y, {
type: "grass",
});
break;
}
}
};
});