UNPKG

@gamepark/rules-api

Version:

API to implement the rules of a board game

59 lines (58 loc) 2.16 kB
import { HexGridSystem } from './grid.hex.util'; import { XYCoordinates } from './grid.util'; /** * Configuration of a Polyhex */ export interface PolyhexConfig<T = any> { /** * System for the representation of the polyhex in a grid */ system: HexGridSystem; /** * Minimum x coordinates of the polyhex (arrays cannot have negative index) */ xMin: number; /** * Minimum y coordinates of the polyhex (arrays cannot have negative index) */ yMin: number; /** * Function to evaluate whether a value is empty or not in the grid. * By default, undefined and null values are considered as empty. * @param value Value to evaluate * @return true if the value should be considered as empty */ isEmpty: (value: T) => boolean; } /** * Class to work on Polyhex (multiple hexagons linked together) */ export declare class Polyhex<T = any> implements PolyhexConfig<T> { grid: (T | undefined)[][]; system: HexGridSystem; xMin: number; yMin: number; isEmpty: (value: (T | undefined)) => boolean; constructor(grid: (T | undefined)[][], config?: Partial<PolyhexConfig>); get xMax(): number; get yMax(): number; getValue(coordinates: XYCoordinates): T | undefined; /** * Utility function to merge multiple polyhex together to create a bigger polyhex: it merges given polyhex grid in current polyhex. * @param polyhex Polyhex to merge in the current polyhex * @param location Location to merge the new polyhex in * @param onOverlap Callback function when a non-empty value is erased */ merge(polyhex: (T | undefined)[][], location?: { x?: number; y?: number; rotation?: number; }, onOverlap?: (x: number, y: number) => void): void; /** * Get the minimum distance from given hexagon to a hexagon of the polyhex that matches given predicate * @param hex Starting hexagon * @param predicate The predicate to match (not empty by default) * @returns the minimum distance found */ getDistance(hex: XYCoordinates, predicate?: (value: T | undefined) => boolean): number; }