improve grass growth.

grass now checks all adjacent tiles when attempting to grow.
This commit is contained in:
HyperOnion 2023-06-28 14:35:07 +02:00
parent b93cc3210c
commit b5574e33fa
2 changed files with 21 additions and 6 deletions

View File

@ -26,13 +26,18 @@ const dirs = [
tile_tickers.set("grass", (box, x, y, delta_time) => {
const tile = world.get_tile(box, x, y);
if (Math.random() > 0.9 ** delta_time) {
if (Math.random() > 0.8 ** 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",
});
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;
}
}
};
});

View File

@ -9,3 +9,13 @@ export function integer (min, max) {
export function item (array) {
return array[integer(0, array.length)];
}
export function shuffle (array) {
const copy = [ ...array ];
const result = [];
while (copy.length > 0) {
const index = integer(0, copy.length);
result.push(copy.splice(index, 1)[0]);
}
return result;
}