Skip to content

BelkaDev/gen-city

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

âš¡ Gen City

Npm package version Small size Building

Procedural city generation

.

npm i gen-city

.

Generation

Create city

const city = new City(params)
Param Description Default
width Map width -
height Map height -

Generate

await city.generate(params?)
Param Description Default
mode Generation mode RUNTIME
seed Generation seed array for mode SEED -
startPosition Start generation position 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 size of bulding size 3
buildingMaxSize Maximum size of bulding size 6
buildingMinSpace Minimum distance between buildings 1
buildingMaxSpace Maximum distance between buildings 3

.

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

Get matrix

Get all tiles (Node, Path, Building) as matrix

const matrix = city.getMatrix(): MatrixTile[][]

Get tile at matrix position

const tile = city.getAt(position: Position): MatrixTile | null

.

Nodes

Get all nodes

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

Get node at matrix position

const node = city.getNodeAt(position: Position): Node | null

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 at matrix position

const path = city.getPathAt(position: Position): Path | null

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

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 at matrix position

const building = city.getBuildingAt(position: Position): Building | null

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

.

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

About

🧩 Procedural city generation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 95.7%
  • JavaScript 4.3%