@gamepark/rules-api
Version:
API to implement the rules of a board game
58 lines • 2.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSquareInDirection = exports.directions = exports.Direction = exports.getDistanceBetweenSquares = exports.areAdjacentSquares = void 0;
var enum_util_1 = require("./enum.util");
var grid_util_1 = require("./grid.util");
/**
* Check if two squares in a grid are orthogonally adjacent based on their coordinates
* @param square1 Coordinates of the first square
* @param square2 Coordinates of the second square
* @returns true if the squares are orthogonally adjacent
*/
var areAdjacentSquares = function (square1, square2) {
return (0, grid_util_1.isXYCoordinates)(square1) && (0, grid_util_1.isXYCoordinates)(square2) && (0, exports.getDistanceBetweenSquares)(square1, square2) === 1;
};
exports.areAdjacentSquares = areAdjacentSquares;
/**
* Get the distance between 2 squares in a square-grid, ie the minimum number of steps you need to take to go from square 1 to square 2 following
* orthogonally adjacent squares
* @param square1 Coordinates of the first square
* @param square2 Coordinates of the second square
* @returns the distance between square1 and square2
*/
var getDistanceBetweenSquares = function (square1, square2) {
return Math.abs(square1.x - square2.x) + Math.abs(square1.y - square2.y);
};
exports.getDistanceBetweenSquares = getDistanceBetweenSquares;
/**
* The direction (cardinal points) you can orient in a square grid
*/
var Direction;
(function (Direction) {
Direction[Direction["North"] = 1] = "North";
Direction[Direction["South"] = 2] = "South";
Direction[Direction["East"] = 3] = "East";
Direction[Direction["West"] = 4] = "West";
})(Direction || (exports.Direction = Direction = {}));
/**
* List of the 4 {@link Direction}s
*/
exports.directions = (0, enum_util_1.getEnumValues)(Direction);
/**
* Get the coordinates of the next square if you move in a square grid following a direction
* @param origin The coordinates of the square you start from
* @param direction The direction you move to
* @param distance The number of steps you move (A by default)
* @returns the coordinates of the destination square
*/
var getSquareInDirection = function (origin, direction, distance) {
if (distance === void 0) { distance = 1; }
if (origin.x === undefined || origin.y === undefined)
throw new Error('Missing x or y to getSquareInDirection');
return {
x: direction === Direction.East ? origin.x + distance : direction === Direction.West ? origin.x - distance : origin.x,
y: direction === Direction.North ? origin.y - distance : direction === Direction.South ? origin.y + distance : origin.y
};
};
exports.getSquareInDirection = getSquareInDirection;
//# sourceMappingURL=grid.squares.util.js.map