mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-17 17:51:24 +01:00
refactor: drawCultures
This commit is contained in:
parent
6a3ef0f790
commit
cbe6da0949
4 changed files with 70 additions and 33 deletions
40
src/layers/renderers/utilts.ts
Normal file
40
src/layers/renderers/utilts.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import {connectVertices} from "scripts/connectVertices";
|
||||
|
||||
export function getPaths(
|
||||
cellNeighbors: number[][],
|
||||
cellVertices: number[][],
|
||||
vertices: IGraphVertices,
|
||||
getType: (cellId: number) => number
|
||||
) {
|
||||
const paths: Dict<string> = {};
|
||||
|
||||
function addPath(index: number, points: TPoints) {
|
||||
if (!paths[index]) paths[index] = "";
|
||||
paths[index] += "M" + points.join("L") + "Z";
|
||||
}
|
||||
|
||||
const checkedCells = new Uint8Array(cellNeighbors.length);
|
||||
for (let cellId = 0; cellId < cellNeighbors.length; cellId++) {
|
||||
if (checkedCells[cellId]) continue;
|
||||
if (!getType(cellId)) continue;
|
||||
checkedCells[cellId] = 1;
|
||||
|
||||
const type = getType(cellId);
|
||||
const ofSameType = (cellId: number) => getType(cellId) === type;
|
||||
|
||||
const isOnborder = cellNeighbors[cellId].some(cellId => !ofSameType(cellId));
|
||||
if (!isOnborder) continue;
|
||||
|
||||
const startingVertex = cellVertices[cellId].find(v => vertices.c[v].some(cellId => !ofSameType(cellId)));
|
||||
if (startingVertex === undefined) throw new Error(`getPath: starting vertex for cell ${cellId} is not found`);
|
||||
|
||||
const chain = connectVertices({vertices, startingVertex, ofSameType, checkedCellsMutable: checkedCells});
|
||||
|
||||
if (chain.length < 3) continue;
|
||||
const points = chain.map(v => vertices.p[v]);
|
||||
|
||||
addPath(type, points);
|
||||
}
|
||||
|
||||
return paths;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue