UNPKG

gridboard

Version:

A Grid-Based Games Framework For TypeScript/JavaScript

164 lines (156 loc) 5.22 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var Grid = /** @class */ (function () { function Grid(x, y, board) { this.i = y * board.width + x; this.x = x; this.y = y; this.board = board; } Grid.prototype.getGridByRelativeCoordinate = function (dx, dy, converter) { var _a, _b, _c, _d; if (typeof dy !== "number") { converter = dy; _a = dx, dx = _a[0], dy = _a[1]; } if (typeof dx !== "number") { _b = dx, dx = _b[0], dy = _b[1]; } if (converter) { if (typeof converter === "object") { _c = converter.convertRelativeCoordinate(dx, dy), dx = _c[0], dy = _c[1]; } else { _d = converter(dx, dy), dx = _d[0], dy = _d[1]; converter = undefined; } } var x = this.x + dx; var y = this.y + dy; return this.board.getGridByAbsoluteCoordinate(x, y, converter); }; return Grid; }()); Grid.prototype.getGridTo = Grid.prototype.getGridByRelativeCoordinate; var GridBoard = /** @class */ (function () { function GridBoard(width, height) { this.grids = []; this.width = width; this.height = height; for (var x = 0; x < width; x++) { for (var y = 0; y < height; y++) { var grid = new Grid(x, y, this); this.grids[grid.i] = grid; } } } GridBoard.prototype.getGridByAbsoluteCoordinate = function (x, y, converter) { var _a, _b, _c, _d; if (typeof y !== "number") { converter = y; _a = x, x = _a[0], y = _a[1]; } if (typeof x !== "number") { _b = x, x = _b[0], y = _b[1]; } if (converter) { if (typeof converter === "object") { _c = converter.convertAbsoluteCoordinate(x, y), x = _c[0], y = _c[1]; } else { _d = converter(x, y), x = _d[0], y = _d[1]; } } var isOverBoundary = (x < 0 || x >= this.width || y < 0 || y >= this.height); if (isOverBoundary) { return null; } var i = y * this.width + x; return this.grids[i]; }; return GridBoard; }()); GridBoard.prototype.getGridAt = GridBoard.prototype.getGridByAbsoluteCoordinate; var Direction = function (codes) { if (typeof codes !== "string") { return Direction(codes[0]); } if (codes in Direction) { return Direction[codes]; } var dx = 0; var dy = 0; for (var _i = 0, _a = codes.split(""); _i < _a.length; _i++) { var code = _a[_i]; switch (code) { case "F": dy--; break; case "B": dy++; break; case "L": dx--; break; case "R": dx++; break; } } return Direction[codes] = [dx, dy]; }; var Orientation; (function (Orientation) { Orientation[Orientation["FBLR"] = 0] = "FBLR"; Orientation[Orientation["BFLR"] = 1] = "BFLR"; Orientation[Orientation["FBRL"] = 2] = "FBRL"; Orientation[Orientation["BFRL"] = 3] = "BFRL"; Orientation[Orientation["LRFB"] = 4] = "LRFB"; Orientation[Orientation["LRBF"] = 5] = "LRBF"; Orientation[Orientation["RLFB"] = 6] = "RLFB"; Orientation[Orientation["RLBF"] = 7] = "RLBF"; })(Orientation || (Orientation = {})); var Orientation$1 = Orientation; var OrientationCoordinateConverter = /** @class */ (function () { function OrientationCoordinateConverter(board, orientation) { this.board = board; this.isAxisNeedSwap = (4 & orientation) > 0; this.isXAxisOrderByDescending = (2 & orientation) > 0; this.isYAxisOrderByDescending = (1 & orientation) > 0; } OrientationCoordinateConverter.prototype.convertAbsoluteCoordinate = function (x, y) { var _a; if (this.isAxisNeedSwap) { _a = [x, y], y = _a[0], x = _a[1]; } if (this.isXAxisOrderByDescending) { x = this.board.width - 1 - x; } if (this.isYAxisOrderByDescending) { y = this.board.height - 1 - y; } return [x, y]; }; OrientationCoordinateConverter.prototype.convertRelativeCoordinate = function (dx, dy) { var _a; if (this.isAxisNeedSwap) { _a = [dx, dy], dy = _a[0], dx = _a[1]; } if (this.isXAxisOrderByDescending) { dx = -dx; } if (this.isYAxisOrderByDescending) { dy = -dy; } return [dx, dy]; }; return OrientationCoordinateConverter; }()); exports.Direction = Direction; exports.Grid = Grid; exports.GridBoard = GridBoard; exports.Orientation = Orientation$1; exports.OrientationCoordinateConverter = OrientationCoordinateConverter;