2.4.4 • Published 6 months ago

cyberheroic-maps1 v2.4.4

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

Cyber Heroic Maps

Procedural generation 2D map with biomes

.

Install

npm i cyberheroic-maps

.

Generator

Create world generator

const generator = new WorldGenerator<T>(params)
  • params {
    • width - Map width
    • height - Map height
  • }

.

Layers

Add layer to generator

const layer: WorldLayer = generator.addLayer(params?)
  • params {
    • frequencyChange - Frequency of biomes change
      • Default: 0.3, Min: 0.0, Max: 1.0
    • borderSmoothness - Smoothness of biomes borders
      • Default: 0.5, Min: 0.0, Max: 1.0
    • heightRedistribution - Redistribution of biomes height
      • Default: 1.0, Min: 0.5, Max: 1.5
    • heightAveraging - Averaging of biomes height
      • Default: true
    • falloff - Scale of falloff area
      • Default: 0.0
  • }

Get generator layers

const layers: WorldLayers[] = generator.getLayers()

Clear all generator layers

generator.clearLayers()

.

Biomes

Add biome to layer

for (const { params, data } of ...) {
  const biome: WorldBiome = layer.addBiome(params, data)
}
  • params {
    • lowerBound - Lower biome bound
      • Default: 0.0
    • upperBound - Upper biome bound
      • Default: 1.0
  • }
  • data - Data of biome

Get layer biomes

const biomes: WorldBiome[] = layer.getBiomes()

Clear all layer biomes

layer.clearBiomes()

.

Generation

Generate world

const world: World = generator.generate(params?)
  • params {
    • seed - Generation seed
      • Default: Autogenerate
    • seedSize - Size of seed array
      • Default: 512
  • }

.

World

Get matrix of biomes data

const matrix: T[][] = world.getMatrix()

Each all positions

world.each(callback)
  • callback(
    • position - Position at matrix
    • data - Biome data
  • )

Get biome data at position

const data: T = world.getAt(position)
  • position {
    • x - X position at matrix
    • y - Y position at matrix
  • }

Replace biome data at position

world.replaceAt(position, data)
  • position {
    • x - X position at matrix
    • y - Y position at matrix
  • }
  • data - New data of biome

Get current world generation seed

const seed: number[] = world.seed

Get world width

const width: number = world.width

Get world height

const height: number = world.height

.

Example

const TILE_SIZE = 2;
const BIOMES = [
  { // WATER
    params: { lowerBound: 0.0, upperBound: 0.2 },
    data: { color: 'blue' },
  },
  { // GRASS
    params: { lowerBound: 0.2, upperBound: 0.7 },
    data: { color: 'green' },
  },
  { // MOUNTS
    params: { lowerBound: 0.7 },
    data: { color: 'gray' },
  },
];

const generator = new WorldGenerator({
  width: 100,
  height: 100,
});

const layer = generator.addLayer();

for (const { params, data } of BIOMES) {
  layer.addBiome(params, data);
}

const world = generator.generate();

world.each((position, biome) => {
  const tileX = position.x * TILE_SIZE;
  const tileY = position.y * TILE_SIZE;

  ctx.fillStyle = biome.color;
  ctx.fillRect(tileX, tileY, TILE_SIZE, TILE_SIZE);
});
2.4.3

6 months ago

2.4.2

6 months ago

2.4.4

6 months ago

3.0.4

7 months ago