boxes/js/tile_update.mjs

39 lines
903 B
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 (delta_time) {
const box = get_player_box();
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 ],
];
tile_tickers.set("grass", (box, x, y, delta_time) => {
const tile = world.get_tile(box, x, y);
if (Math.random() > 0.9 ** delta_time) {
const dir = random.item(dirs);
const [ d_x, d_y ] = dir;
if (world.get_tile(box, x + d_x, y + d_y).type === "empty") {
world.set_tile(box, x + d_x, y + d_y, {
type: "grass",
});
}
};
});