1.4.3 • Published 2 months ago

gen-city v1.4.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

⚡ Gen City

Npm package version Small size Building

Procedural city generation

.

Demo

Documentation

.

Install

npm i gen-city

.

Generation

Create city

const city = new City(params)
ParamDescriptionDefault
widthMap width-
heightMap height-

Generate

await city.generate(params?)
ParamDescriptionDefault
modeGeneration mode. Runtime - Random generation in runtime Seed - Generation follows the specified seedRuntime
seedGeneration seed array for seed mode-
startPositionPosition of the first node.Center of map
startDirectionsStart generation directionsLeft, Right, Top, Bottom
streetMinLengthStreet length before generating an intersection or turn10
probabilityIntersectionProbability of generating intersection0.1
probabilityTurnProbability of generating turn0.05
probabilityStreetEndProbability of generating street end0.001
buildingMinSizeMinimum building size3
buildingMaxSizeMaximum building size6
buildingMinSpaceMinimum distance between buildings1
buildingMaxSpaceMaximum distance between buildings3
buildingOffsetDistance between building and path0

.

General

Get size

const width = city.width
const height = city.height

Get seed

Return seed if city was generated with runtime mode

const seed = city.getSeed(): number[] | null

.

Nodes

Get all nodes

const nodes = city.getAllNodes(): Node[]

Get node paths

const inputPaths = node.getInputPaths(): Path[]
const outputPaths = node.getOutputPaths(): Path[]
const allPaths = node.getAllPaths(): Path[]

Get node type

Get type by count of input and output paths (Turn, Cross, End)

const type = node.getType(): NodeType

.

Paths

Get all paths

const paths = city.getAllPaths(): Path[]

Get path positions

const positions = path.getPositions(): { 
  beg: Position
  end: Position
}

Get path nodes

const nodeBeg = path.getNodeBeg(): Node
const nodeEnd = path.getNodeEnd(): Node

Get path length

const length = path.getLength(): number

Each path positions

path.each(callback: (position: Position) => void)

Remove path from nodes

path.remove()

Get path buildings

const buildings = path.getBuildings(): Building[]

Get path direction

Get direction in degrees

const direction: number = path.direction

.

Buildings

Get all buildings

const buildings = city.getAllBuildings(): Building[]

Get building vertices

Array of rectangle corners positions

const vertices: Position[] = building.vertices

Get building position

Get top left corner position

const position: Position = building.position

Get building size

const width: number = building.width
const height: number = building.height

Each building positions

building.each(callback: (position: Position) => void)

Remove building from path

building.remove()

.

Example

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
    );
  });
});
1.4.3

2 months ago

1.4.2

5 months ago

1.2.0

8 months ago

1.1.1

8 months ago

1.1.0

8 months ago

1.0.1

8 months ago

1.4.1

8 months ago

1.3.2

8 months ago

1.4.0

8 months ago

1.3.1

8 months ago

1.3.0

8 months ago

1.0.0

9 months ago