Procedural city generation
.
Documentation
.
npm i gen-city
.
const city = new City(params)
Param | Description | Default |
---|---|---|
width |
Map width | - |
height |
Map height | - |
await city.generate(params?)
Param | Description | Default |
---|---|---|
mode |
Generation mode. * Runtime - Random generation in runtime* Seed - Generation follows the specified seed |
Runtime |
seed |
Generation seed array for seed mode | - |
startPosition |
Position of the first node. | Center of map |
startDirections |
Start generation directions | Left, Right, Top, Bottom |
streetMinLength |
Street length before generating an intersection or turn | 10 |
probabilityIntersection |
Probability of generating intersection | 0.1 |
probabilityTurn |
Probability of generating turn | 0.05 |
probabilityStreetEnd |
Probability of generating street end | 0.001 |
buildingMinSize |
Minimum building size | 3 |
buildingMaxSize |
Maximum building size | 6 |
buildingMinSpace |
Minimum distance between buildings | 1 |
buildingMaxSpace |
Maximum distance between buildings | 3 |
buildingOffset |
Distance between building and path | 0 |
.
const width = city.width
const height = city.height
Return seed if city was generated with runtime mode
const seed = city.getSeed(): number[] | null
.
const nodes = city.getAllNodes(): Node[]
const inputPaths = node.getInputPaths(): Path[]
const outputPaths = node.getOutputPaths(): Path[]
const allPaths = node.getAllPaths(): Path[]
Get type by count of input and output paths (Turn, Cross, End)
const type = node.getType(): NodeType
.
const paths = city.getAllPaths(): Path[]
const positions = path.getPositions(): {
beg: Position
end: Position
}
const nodeBeg = path.getNodeBeg(): Node
const nodeEnd = path.getNodeEnd(): Node
const length = path.getLength(): number
path.each(callback: (position: Position) => void)
path.remove()
const buildings = path.getBuildings(): Building[]
Get direction in degrees
const direction: number = path.direction
.
const buildings = city.getAllBuildings(): Building[]
Array of rectangle corners positions
const vertices: Position[] = building.vertices
Get top left corner position
const position: Position = building.position
const width: number = building.width
const height: number = building.height
building.each(callback: (position: Position) => void)
building.remove()
.
const city = new City({
width: 200,
height: 200,
});
city.generate({
streetMinLength: 15,
}).then(() => {
// Draw roads
ctx.beginPath();
city.getAllPaths().forEach((path) => {
const positions = path.getPositions();
ctx.moveTo(positions.beg.x, positions.beg.y);
ctx.lineTo(positions.end.x, positions.end.y);
});
ctx.stroke();
// Draw buildings
city.getAllBuildings().forEach((building) => {
ctx.fillRect(
building.position.x,
building.position.y,
building.width,
building.height
);
});
});