From b5574e33fa07566b65e38cd6837658ec1aaac084 Mon Sep 17 00:00:00 2001 From: HyperOnion <> Date: Wed, 28 Jun 2023 14:35:07 +0200 Subject: [PATCH] improve grass growth. grass now checks all adjacent tiles when attempting to grow. --- js/tile_update.mjs | 17 +++++++++++------ js/utils/random.mjs | 10 ++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/js/tile_update.mjs b/js/tile_update.mjs index 8cbfd44..1b9ba76 100644 --- a/js/tile_update.mjs +++ b/js/tile_update.mjs @@ -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; + } } }; }); diff --git a/js/utils/random.mjs b/js/utils/random.mjs index 1143e30..1f629e8 100644 --- a/js/utils/random.mjs +++ b/js/utils/random.mjs @@ -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; +}