pinto-wfcengine
v0.8.0
Published
A TypeScript implementation of the Wave Function Collapse (WFC) algorithm for procedural content generation on 2D grids.
Downloads
22
Readme
pinto-wfcengine
A TypeScript implementation of the Wave Function Collapse (WFC) algorithm for procedural content generation on 2D grids.
Features
- Wave Function Collapse Algorithm: Efficiently generates patterns on a 2D grid using block types and adjacency rules.
- Customizable Blocks and Rules: Define your own block types (with SVG content) and specify adjacency constraints.
- Supports Square and Hex Grids: Flexible grid configuration for different neighbor topologies.
- Serialization: Save and restore grid states to/from JSON.
- Deterministic or Random Generation: Plug in your own random number generator for reproducible results.
- Unit Tested: Includes tests for core algorithm and serialization.
import { WaveCollapseController } from "pinto-wfcengine";
// Define blocks and adjacency rules
const blocks = [
{ id: "land", svgContent: "<rect ... />" },
{ id: "water", svgContent: "<rect ... />" },
// ...
];
const rules = {
land: [["land", "water"], ...], // directions: [up, right, down, left]
// ...
};
const gridConfig = /* SquareGrid or HexGrid from types */;
// Create controller
const controller = new WaveCollapseController(10, 10, blocks, rules, gridConfig);
// Run collapse
controller.fullCollapse(() => Math.random());
// Access grid state
const grid = controller.getGrid();Scripts
npm run build– Build the TypeScript source to JavaScript (outputs todist/).npm run dev– Watch and rebuild the project on changes.npm test– Run the test suite using Jest.
