Observed since 20 Oct, 19:10 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
A simple square grid made for games

A simple square grid made for games with built-in features like:
Missing a feature? Create an issue!
Install 2d-game-grid to your project with one of the following commands:
npm install 2d-game-grid
yarn add 2d-game-grid
Both examples will create the exact same grid
import {Grid} from '2d-game-grid';
const grid = new Grid({
grid: [
['0-0', '0-1', '0-2', '0-3'],
['1-0', '1-1', '1-2', '1-3'],
['2-0', '2-1', '2-2', '2-3']
]
});
import {Grid} from '2d-game-grid';
const grid = new Grid({
width: 4,
height: 3,
initializeCellValue: ({row, col}) => `${row}-${col}`,
});
const cell = grid.getCell({row: 1, col: 2});
console.log(cell.value) // '1-2'
const row = grid.getRow(1);
console.log(row.cells.map(cell => cell.value)) // ['1-0', '1-1', '1-2', '1-3']
const column = grid.getColumn(1);
console.log(column.cells.map(cell => cell.value)) // ['0-1', '1-1', '2-1']
const cell = grid.getCell({row: 1, col: 2});
const leftNeighbor = cell.neighbors.get('LEFT');
console.log(leftNeighbor.value); // '1-1'
const cell = grid.getCell({row: 1, col: 2});
const distance = cell.getDistance({row: 1, col: 0});
console.log(distance); // 2
const cell = grid.getCell({row: 0, col: 3});
const cells = cell.listCellsInDistance(2);
console.log(cells.map(cell => cell.value)); // ['1-2', '2-3', '0-1', '0-2', '1-3']
The pathfinding uses the pathfinding package.
const cell = grid.getCell({row: 1, col: 2});
const path = cell.getPath({row: 0, col: 0});
console.log(path.map(cell => cell.value)); // ['1-2', '0-1', '0-0']
Hint: The returned path will always include the start and the end.
You can decide which diagonal movements should be allowed:




You can either choose between the following heuristic algorithms:
Or pass your own heuristic function:
cell.getPath({row: 1, col: 0}, {
heuristic: (cell) => {/* your implementation */}
});
const cell = grid.getCell({row: 0, col: 3});
const cells = cell.listReachableCells(3);
console.log(cells.map(cell => cell.value)); // ['1-1', '2-0', '0-2', '0-1', '1-0']
const gridA = new Grid({grid: [["A"]]})
const gridB = new Grid({grid: [["B"]]})
gridA.extend(gridB, 'LEFT') // B A
gridA.extend(gridB, 'RIGHT') // A B
const cells = grid.crop({row: 0, col: 1}, {row: 1, col: 2}).cells;
console.log(cells.map(cell => cell.value)); // [['0-1', '0-2'], ['1-1', '1-2']]
grid.onCellValueChanged((event) => {/* your implementation */});
grid.getRow(1).onCellValueChanged((event) => {/* your implementation */});
grid.getColumn(2).onCellValueChanged((event) => {/* your implementation */});
grid.getCell({row: 1, col: 2}).onValueChanged((event) => {/* your implementation */});
const cell = grid.getCell({row: 1, col: 2});
cell.value = 'new value'; // updating this value will trigger all callbacks of the "Register" example
To trigger this event correctly, your value should be treated immutable. If you're using an object or array as value, you have to replace the object/array to trigger the callbacks.
// object
cell.value.text = 'foo'; // will NOT trigger any callback
cell.value = {...cell.value, text: 'foo'}; // will trigger the callbacks
// array
cell.value.push('foo'); // will NOT trigger any callback
cell.value = [...cell.value, 'foo']; // will trigger the callbacks
The onCellValueChanged() and onValueChanged() functions return a function to unregister.
When you execute this unregister function, the callback will not be called anymore.
const unregister = grid.onCellValueChanged((event) => {/* your implementation */});
unregister();
Feel free to create issues or merge requests
Modify the version in package.json. When running the pipeline on master a new package will be deployed with that version.
Download counts and popularity will appear here as the collector receives them.
| Version | Published | Direct dependencies | Open version |
|---|---|---|---|
| 1.0.6Registry latest | 3 Aug 2023 | 12 | |
| 1.0.5 | 18 Jul 2023 | 11 | |
| 1.0.4 | 14 Jul 2023 | 11 | |
| 1.0.3 | 14 Jul 2023 | 11 | |
| 1.0.2 | 14 Jul 2023 | 11 | |
| 1.0.1 | 13 Jul 2023 | 11 | |
| 1.0.0 | 13 Jul 2023 | 11 | |
| 0.0.8 | 13 Jul 2023 | 11 | |
| 0.0.7 | 12 Jul 2023 | 10 | |
| 0.0.6 | 12 Jul 2023 | 10 | |
| 0.0.5 | 12 Jul 2023 | 10 | |
| 0.0.4 | 12 Jul 2023 | 10 | |
| 0.0.3 | 9 Jul 2023 | 10 | |
| 0.0.2 | 9 Jul 2023 | 10 | |
| 0.0.1 | 8 Jul 2023 | 8 |
This describes our collected data; it is not a security assessment.
Observed since 20 Oct, 19:10 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package:f78cfbb6cba8df12a55348d6fb429c4495d56af3e166d9e2443cdb65b4a11322