UNPKG

@rickosborne/hexgrid

Version:

Rick Osborne's collection of hexagonal grid-related code.

56 lines (55 loc) 2.25 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var hex_reachable_exports = {}; __export(hex_reachable_exports, { hexReachable: () => hexReachable }); module.exports = __toCommonJS(hex_reachable_exports); var import_foundation = require("@rickosborne/foundation"); const hexReachable = /* @__PURE__ */ __name((start, steps, directions, adder, identity, isBlocked, onPoint) => { const visited = /* @__PURE__ */ new Set(); visited.add(identity(start)); const reachable = []; reachable.push({ distance: 0, point: start }); const fringes = []; fringes.push([start]); const memoizedIsBlocked = (0, import_foundation.memoizeBinary)(isBlocked); const memoizedIdentity = (0, import_foundation.memoizeUnary)(identity); for (let distance = 1; distance <= steps; distance++) { const out = []; fringes.push(out); for (let prior of fringes[distance - 1]) { for (let direction of directions) { const point = adder(prior, direction); const id = memoizedIdentity(point); if (!visited.has(id) && !memoizedIsBlocked(point, id)) { visited.add(id); out.push(point); reachable.push({ direction, distance, point, prior }); if (onPoint != null) { onPoint(point, distance, prior, direction); } } } } } return reachable; }, "hexReachable"); //# sourceMappingURL=hex-reachable.cjs.map