Add "roll downhill" logic
This commit is contained in:
parent
3b701019cc
commit
f4bf2f19e9
1 changed files with 26 additions and 2 deletions
28
island.ts
28
island.ts
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue