UNPKG

@gamepark/rules-api

Version:

API to implement the rules of a board game

102 lines 4.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Polyhex = void 0; var grid_hex_util_1 = require("./grid.hex.util"); /** * Class to work on Polyhex (multiple hexagons linked together) */ var Polyhex = /** @class */ (function () { function Polyhex(grid, config) { var _a, _b, _c, _d; this.grid = grid; this.system = grid_hex_util_1.HexGridSystem.Axial; this.xMin = 0; this.yMin = 0; this.isEmpty = function (value) { return value == null; }; // true for null and undefined this.system = (_a = config === null || config === void 0 ? void 0 : config.system) !== null && _a !== void 0 ? _a : this.system; this.xMin = (_b = config === null || config === void 0 ? void 0 : config.xMin) !== null && _b !== void 0 ? _b : this.xMin; this.yMin = (_c = config === null || config === void 0 ? void 0 : config.yMin) !== null && _c !== void 0 ? _c : this.yMin; this.isEmpty = (_d = config === null || config === void 0 ? void 0 : config.isEmpty) !== null && _d !== void 0 ? _d : this.isEmpty; } Object.defineProperty(Polyhex.prototype, "xMax", { get: function () { return Math.max.apply(Math, this.grid.map(function (line) { return line.length; })) + this.xMin - 1; }, enumerable: false, configurable: true }); Object.defineProperty(Polyhex.prototype, "yMax", { get: function () { return this.grid.length + this.yMin - 1; }, enumerable: false, configurable: true }); Polyhex.prototype.getValue = function (coordinates) { var _a; return (_a = this.grid[coordinates.y - this.yMin]) === null || _a === void 0 ? void 0 : _a[coordinates.x - this.xMin]; }; /** * 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 */ Polyhex.prototype.merge = function (polyhex, location, onOverlap) { var _a, _b; if (location === void 0) { location = {}; } if (onOverlap === void 0) { onOverlap = function () { }; } for (var y = 0; y < polyhex.length; y++) { for (var x = 0; x < polyhex[y].length; x++) { if (!this.isEmpty(polyhex[y][x])) { var rotatedCoordinates = (0, grid_hex_util_1.hexRotate)({ x: x, y: y }, location.rotation, this.system); var coordinates = (0, grid_hex_util_1.hexTranslate)(rotatedCoordinates, { x: (_a = location.x) !== null && _a !== void 0 ? _a : 0, y: (_b = location.y) !== null && _b !== void 0 ? _b : 0 }, this.system); while (coordinates.y < this.yMin) { this.grid.unshift([]); this.yMin--; } while (coordinates.x < this.xMin) { for (var _i = 0, _c = this.grid; _i < _c.length; _i++) { var line1 = _c[_i]; line1.unshift(undefined); } this.xMin--; } if (!this.grid[coordinates.y - this.yMin]) { this.grid[coordinates.y - this.yMin] = []; } if (this.grid[coordinates.y - this.yMin][coordinates.x - this.xMin]) { onOverlap(x, y); } this.grid[coordinates.y - this.yMin][coordinates.x - this.xMin] = polyhex[y][x]; } } } }; /** * 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 */ Polyhex.prototype.getDistance = function (hex, predicate) { var _this = this; if (predicate === void 0) { predicate = function (value) { return !_this.isEmpty(value); }; } var distance = 0; var maxDistance = this.xMax + this.yMax - this.xMin - this.yMin; while (distance < maxDistance) { var hexagonsAtDistance = (0, grid_hex_util_1.getHexagonsAtDistance)(hex, distance, this.system); if (hexagonsAtDistance.some(function (hex) { return predicate(_this.getValue(hex)); })) { return distance; } else { distance++; } } return Infinity; }; return Polyhex; }()); exports.Polyhex = Polyhex; //# sourceMappingURL=polyhex.util.js.map