@2d-game-grid/square
Version:
A simple square grid made for games
95 lines (94 loc) • 3.58 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SquareCell = void 0;
const core_1 = require("@2d-game-grid/core");
const SquareNeighbors_js_1 = require("./SquareNeighbors.js");
const getDistance_js_1 = require("./algorithms/distance/getDistance.js");
const getPath_js_1 = require("./algorithms/pathfinding/getPath.js");
const listCellsInDistance_js_1 = require("./algorithms/distance/listCellsInDistance.js");
const listReachableCells_js_1 = require("./algorithms/pathfinding/listReachableCells.js");
const SquareEdges_js_1 = require("./SquareEdges.js");
const SquareCorners_js_1 = require("./SquareCorners.js");
/**
* A Cell is part of a grid. It contains meta information like its coordinates inside the grid and the corresponding value.
*/
class SquareCell extends core_1.Cell {
grid;
/**
* An instance of the cells' neighbors
*/
neighbors;
/**
* An instance of the cells' edges
*/
edges;
/**
* An instance of the cells' corners
*/
corners;
/**
*
* @param grid The grid this cell is part of
* @param coordinate The coordinate in the grid
* @param value The value of the cell
*/
constructor(grid, coordinate, value) {
super(coordinate, value);
this.grid = grid;
this.neighbors = new SquareNeighbors_js_1.SquareNeighbors(grid, this);
this.edges = new SquareEdges_js_1.SquareEdges(grid, this);
this.corners = new SquareCorners_js_1.SquareCorners(grid, this);
}
/**
* @param target The coordinate that the distance should be calculated for
* @param algorithm The used algorithm for the distance calculation
* @returns The distance
*/
getDistance(target, algorithm = 'MANHATTAN') {
return (0, getDistance_js_1.getDistance)(this, target, algorithm);
}
/**
* @param maxDistance The maximum distance (including) to a cell that should be returned
* @param algorithm The used algorithm for the distance calculation
* @returns All cells that are in the distance (excluding this cell)
*/
listCellsInDistance(maxDistance, algorithm = 'MANHATTAN') {
return (0, listCellsInDistance_js_1.listCellsInDistance)(this, maxDistance, algorithm);
}
/**
* @param target The coordinate that the path should end
* @param options The options to customize the pathfinding
* @returns The shortest path including this cell and the target cell
*/
getPath(target, options) {
return (0, getPath_js_1.getPath)(this.grid, this, target, options);
}
/**
* @param maxPathSteps The maximum amount of steps to a cell that should be returned (start and end cell included)
* @param options The options to customize the pathfinding
* @returns All cells that are reachable (excluding the start cell)
*/
listReachableCells(maxPathSteps, options) {
return (0, listReachableCells_js_1.listReachableCells)(this, maxPathSteps, options);
}
/**
* @returns The row of the cell
*/
getRow() {
return this.grid.getRow(this.row);
}
/**
@returns The column of the cell
*/
getColumn() {
return this.grid.getColumn(this.col);
}
/**
* @param cloneValue A custom function to clone the value of this cell (defaults to copying the value)
* @returns The cloned cell
*/
clone(cloneValue = (value) => value) {
return new SquareCell(this.grid, this, cloneValue(this.value));
}
}
exports.SquareCell = SquareCell;