UNPKG

@gamepark/rules-api

Version:

API to implement the rules of a board game

244 lines 9.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAdjacentHexagons = exports.getPolyhexSpaces = exports.hexTranslate = exports.getHexagonsAtDistance = exports.getDistanceBetweenHex = exports.hexRotate = exports.hexFromAxial = exports.hexToAxial = exports.axialToEvenR = exports.evenRToAxial = exports.axialToOddR = exports.oddRToAxial = exports.axialToEvenQ = exports.evenQToAxial = exports.axialToOddQ = exports.oddQToAxial = exports.HexGridSystem = void 0; /** * The different coordinates systems available to describe a Hexagonal Grid. * See https://www.redblobgames.com/grids/hexagons/#coordinates */ var HexGridSystem; (function (HexGridSystem) { HexGridSystem[HexGridSystem["Axial"] = 0] = "Axial"; HexGridSystem[HexGridSystem["OddQ"] = 1] = "OddQ"; HexGridSystem[HexGridSystem["EvenQ"] = 2] = "EvenQ"; HexGridSystem[HexGridSystem["OddR"] = 3] = "OddR"; HexGridSystem[HexGridSystem["EvenR"] = 4] = "EvenR"; })(HexGridSystem || (exports.HexGridSystem = HexGridSystem = {})); /** * Converts {@link HexGridSystem.OddQ} coordinates into {@link HexGridSystem.Axial} coordinates */ var oddQToAxial = function (_a) { var x = _a.x, y = _a.y; return ({ x: x, y: y - (x - (x & 1)) / 2 }); }; exports.oddQToAxial = oddQToAxial; /** * Converts {@link HexGridSystem.Axial} coordinates into {@link HexGridSystem.OddQ} coordinates */ var axialToOddQ = function (_a) { var x = _a.x, y = _a.y; return ({ x: x, y: y + (x - (x & 1)) / 2 }); }; exports.axialToOddQ = axialToOddQ; /** * Converts {@link HexGridSystem.EvenQ} coordinates into {@link HexGridSystem.Axial} coordinates */ var evenQToAxial = function (_a) { var x = _a.x, y = _a.y; return ({ x: x, y: y - (x + (x & 1)) / 2 }); }; exports.evenQToAxial = evenQToAxial; /** * Converts {@link HexGridSystem.Axial} coordinates into {@link HexGridSystem.EvenQ} coordinates */ var axialToEvenQ = function (_a) { var x = _a.x, y = _a.y; return ({ x: x, y: y + (x + (x & 1)) / 2 }); }; exports.axialToEvenQ = axialToEvenQ; /** * Converts {@link HexGridSystem.OddR} coordinates into {@link HexGridSystem.Axial} coordinates */ var oddRToAxial = function (_a) { var x = _a.x, y = _a.y; return ({ y: y, x: x - (y - (y & 1)) / 2 }); }; exports.oddRToAxial = oddRToAxial; /** * Converts {@link HexGridSystem.Axial} coordinates into {@link HexGridSystem.OddR} coordinates */ var axialToOddR = function (_a) { var x = _a.x, y = _a.y; return ({ y: y, x: x + (y - (y & 1)) / 2 }); }; exports.axialToOddR = axialToOddR; /** * Converts {@link HexGridSystem.EvenR} coordinates into {@link HexGridSystem.Axial} coordinates */ var evenRToAxial = function (_a) { var x = _a.x, y = _a.y; return ({ y: y, x: x - (y + (y & 1)) / 2 }); }; exports.evenRToAxial = evenRToAxial; /** * Converts {@link HexGridSystem.Axial} coordinates into {@link HexGridSystem.EvenR} coordinates */ var axialToEvenR = function (_a) { var x = _a.x, y = _a.y; return ({ y: y, x: x + (y + (y & 1)) / 2 }); }; exports.axialToEvenR = axialToEvenR; /** * Convert any {@link HexGridSystem} coordinates to {@link HexGridSystem.Axial} * @param hex Coordinates to convert * @param system System of the coordinates * @return Coordinates in {@link HexGridSystem.Axial} */ function hexToAxial(hex, system) { switch (system) { case HexGridSystem.OddQ: return (0, exports.oddQToAxial)(hex); case HexGridSystem.EvenQ: return (0, exports.evenQToAxial)(hex); case HexGridSystem.OddR: return (0, exports.oddRToAxial)(hex); case HexGridSystem.EvenR: return (0, exports.evenRToAxial)(hex); default: return hex; } } exports.hexToAxial = hexToAxial; /** * Convert {@link HexGridSystem.Axial} coordinates to any other {@link HexGridSystem} * @param hex Axial coordinates to convert * @param system Destination coordinates system * @return Coordinates converted in the description system */ function hexFromAxial(hex, system) { switch (system) { case HexGridSystem.OddQ: return (0, exports.axialToOddQ)(hex); case HexGridSystem.EvenQ: return (0, exports.axialToEvenQ)(hex); case HexGridSystem.OddR: return (0, exports.axialToOddR)(hex); case HexGridSystem.EvenR: return (0, exports.axialToEvenR)(hex); default: return hex; } } exports.hexFromAxial = hexFromAxial; /** * Rotate hexagonal coordinates. * @param vector Vector to rotate * @param rotation Number of 60 degrees rotations to apply (1 = 60°, 2 = 120°...) * @param system The coordinates system used * @return the rotated vector */ function hexRotate(vector, rotation, system) { if (rotation === void 0) { rotation = 0; } if (system === void 0) { system = HexGridSystem.Axial; } if (system !== HexGridSystem.Axial) return hexFromAxial(hexRotate(hexToAxial(vector, system), rotation), system); switch (rotation % 6) { case 1: return { x: -vector.y, y: vector.x + vector.y }; case 2: // noinspection JSSuspiciousNameCombination return { x: -vector.x - vector.y, y: vector.x }; case 3: return { x: -vector.x, y: -vector.y }; case 4: // noinspection JSSuspiciousNameCombination return { x: vector.y, y: -vector.x - vector.y }; case 5: return { x: vector.x + vector.y, y: -vector.x }; default: return vector; } } exports.hexRotate = hexRotate; /** * Get the distance between 2 hexagons, i.e. the minimum number of hexagons to cross to got from hex1 to hex2. * @param hex1 First hexagon coordinates * @param hex2 Second hexagon coordinates * @param system The coordinates system used * @return the distance between the hexagons */ function getDistanceBetweenHex(hex1, hex2, system) { if (system === void 0) { system = HexGridSystem.Axial; } if (system !== HexGridSystem.Axial) { return getDistanceBetweenHex(hexToAxial(hex1, system), hexToAxial(hex2, system)); } return (Math.abs(hex1.x - hex2.x) + Math.abs(hex1.x - hex2.x + hex1.y - hex2.y) + Math.abs(hex1.y - hex2.y)) / 2; } exports.getDistanceBetweenHex = getDistanceBetweenHex; /** * Get all the hexagons that are exactly at a specific distance from a given hexagon. * @param hex Coordinates of the hexagon * @param distance Distance of the hexagons we want * @param system The coordinates system used * @return the list of the hexagons found at distance from given hex */ function getHexagonsAtDistance(hex, distance, system) { if (system === void 0) { system = HexGridSystem.Axial; } if (system !== HexGridSystem.Axial) { return getHexagonsAtDistance(hexToAxial(hex, system), distance, HexGridSystem.Axial).map(function (hex) { return hexFromAxial(hex, system); }); } if (distance <= 0) return [hex]; var result = []; var currentHex = { x: hex.x - distance, y: hex.y }; var vectors = [{ x: 1, y: -1 }, { x: 1, y: 0 }, { x: 0, y: 1 }, { x: -1, y: 1 }, { x: -1, y: 0 }, { x: 0, y: -1 }]; for (var _i = 0, vectors_1 = vectors; _i < vectors_1.length; _i++) { var vector = vectors_1[_i]; for (var j = 0; j < distance; j++) { result.push(currentHex); currentHex = hexTranslate(currentHex, vector); } } return result; } exports.getHexagonsAtDistance = getHexagonsAtDistance; /** * Translate hexagonal coordinates by a vector. * @param hex Coordinates of the hexagon to translate * @param vector Vector of the translation * @param system The coordinates system used * @return The coordinates of the hexagon after the translation */ function hexTranslate(hex, vector, system) { if (system === void 0) { system = HexGridSystem.Axial; } if (system !== HexGridSystem.Axial) return hexFromAxial(hexTranslate(hexToAxial(hex, system), hexToAxial(vector, system)), system); return ({ x: hex.x + vector.x, y: hex.y + vector.y }); } exports.hexTranslate = hexTranslate; /** * Get the coordinates that will be covered by a polyhex tile when at a specific grid location. * @param polyhex Coordinates occupied by the polyhex without any rotation in given coordinates system * @param location Location of the polyhex on the grid (x, y, and rotation) * @param system The coordinates system used for the polyhex description and the location * @return coordinates in the grid covered when the polyhex has this location */ function getPolyhexSpaces(polyhex, location, system) { var _a, _b; if (system === void 0) { system = HexGridSystem.Axial; } var vector = { x: (_a = location.x) !== null && _a !== void 0 ? _a : 0, y: (_b = location.y) !== null && _b !== void 0 ? _b : 0 }; return polyhex .map(function (hex) { var _a; return hexRotate(hex, (_a = location.rotation) !== null && _a !== void 0 ? _a : 0, system); }) .map(function (hex) { return hexTranslate(hex, vector, system); }); } exports.getPolyhexSpaces = getPolyhexSpaces; /** * Get the coordinates of the 6 hexagons adjacent to a given hexagon. * @param hex Coordinates of the hexagon * @param system Coordinates system */ function getAdjacentHexagons(hex, system) { if (system === void 0) { system = HexGridSystem.Axial; } if (system !== HexGridSystem.Axial) { return getAdjacentHexagons(hexToAxial(hex, system)).map((function (hex) { return hexFromAxial(hex, system); })); } return [ { x: hex.x + 1, y: hex.y }, { x: hex.x, y: hex.y + 1 }, { x: hex.x - 1, y: hex.y + 1 }, { x: hex.x - 1, y: hex.y }, { x: hex.x, y: hex.y - 1 }, { x: hex.x + 1, y: hex.y - 1 } ]; } exports.getAdjacentHexagons = getAdjacentHexagons; //# sourceMappingURL=grid.hex.util.js.map