Add "roll downhill" logic

This commit is contained in:
Tangent 2024-01-12 22:50:21 -05:00
parent 3b701019cc
commit f4bf2f19e9

View file

@ -104,14 +104,38 @@ export function IslandApplet() {
const islands = new IslandGrid(WIDTH, HEIGHT, 128);
const basePos = islands.data.length >> 1;
const len = islands.data.length;
const basePos = len >> 1;
const width = islands.width;
function drop(pos: number) {
const lowerNeighbors: number[] = [];
function check(adjPos: number) {
if (islands.data[adjPos] < islands.data[pos]) {
lowerNeighbors.push(adjPos);
}
}
check((pos - width) % len);
check((pos - 1) % len);
check((pos + 1) % len);
check((pos + width) % len);
if (lowerNeighbors.length > 0) {
const downhill = lowerNeighbors[islands.rng() % lowerNeighbors.length];
drop(downhill);
} else {
islands.data[pos]++;
}
}
function tick() {
const islandTiles = islands.floodSearch(basePos, (tile) => tile > 0);
const pos = islandTiles[islands.rng() % islandTiles.length];
islands.data[pos]++;
drop(pos);
renderIslands(islands, cx);
}