Catalog npm
Back to packageRELEASE DETAILS

1.0.4

npm

2d-game-grid

A simple grid made for games

2ddistancegamegridpathfindingsquare
Published14 Jul 2023
Direct dependencies11
Transitive dependenciesNot calculated

README

Current package documentation

Always

2D Game Grid

A simple square grid made for games with built-in features like:

  • get the shortest path between cells
  • list reachable cells (pathfinding)
  • list cell neighbors
  • get distance between cells
  • list cells in distance

Missing a feature? Create an issue!

Examples & Demos

Usage

Installation

Install 2d-game-grid to your project with one of the following commands:

npm

npm install 2d-game-grid

yarn

yarn add 2d-game-grid

Create a grid

Both examples will create the exact same grid

Use pre-generated cells

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']
  ]
});

Generate cells on initialization

import {Grid} from '2d-game-grid';

const grid = new Grid({
  width: 4,
  height: 3,
  initializeCellValue: ({row, col}) => `${row}-${col}`,
});

Get cell

const cell = grid.getCell({row: 1, col: 2});
console.log(cell.value) // '1-2'

Get cells of a row

const row = grid.getRow(1);
console.log(row.cells.map(cell => cell.value)) // ['1-0', '1-1', '1-2', '1-3']

Get cells of a column

const column = grid.getColumn(1);
console.log(column.cells.map(cell => cell.value)) // ['0-1', '1-1', '2-1']

Get neighbors of a cell

const cell = grid.getCell({row: 1, col: 2});
const leftNeighbor = cell.neighbors.get('LEFT');
console.log(leftNeighbor.value); // '1-1'

Get the distance between cells

const cell = grid.getCell({row: 1, col: 2});
const distance = cell.getDistance({row: 1, col: 0});
console.log(distance); // 2

List all cells in distance

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']

Algorithms

Get the shortest path between cells

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.

Algorithms

Diagonal movement

You can decide which diagonal movements should be allowed:

  • Always
    Always
  • Never
    Never
  • If at most one obstacle
    If at most one obstacle
  • Only when no obstacles
    Only when no obstacles

Heuristic

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 */}
});

List all reachable cells (pathfinding)

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']

Extend grid with another grid

const gridA = new Grid({grid: [["A"]]})
const gridB = new Grid({grid: [["B"]]})

gridA.extend(gridB, 'LEFT')  // B A
gridA.extend(gridB, 'RIGHT') // A B

Crop grid

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']]

Listen for cell value changes

Register

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 */});

Trigger

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

Unregister

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();

Collaboration

Feel free to create issues or merge requests

Release package

Modify the version in package.json. When running the pipeline on master a new package will be deployed with that version.

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 11

Native requirements
11 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
pathfinding
Environment
{
  "local_name": "pathfinding",
  "alias": false,
  "peer_metadata": null
}
^0.4.18runtime
jest
Environment
{
  "local_name": "jest",
  "alias": false,
  "peer_metadata": null
}
^29.6.1development
eslint
Environment
{
  "local_name": "eslint",
  "alias": false,
  "peer_metadata": null
}
^8.44.0development
ts-jest
Environment
{
  "local_name": "ts-jest",
  "alias": false,
  "peer_metadata": null
}
^29.1.1development
ts-loader
Environment
{
  "local_name": "ts-loader",
  "alias": false,
  "peer_metadata": null
}
^9.4.4development
typescript
Environment
{
  "local_name": "typescript",
  "alias": false,
  "peer_metadata": null
}
^5.1.6development
@types/jest
Environment
{
  "local_name": "@types/jest",
  "alias": false,
  "peer_metadata": null
}
^29.5.2development
@types/pathfinding
Environment
{
  "local_name": "@types/pathfinding",
  "alias": false,
  "peer_metadata": null
}
^0.0.6development
eslint-plugin-jsdoc
Environment
{
  "local_name": "eslint-plugin-jsdoc",
  "alias": false,
  "peer_metadata": null
}
^46.4.3development
@typescript-eslint/parser
Environment
{
  "local_name": "@typescript-eslint/parser",
  "alias": false,
  "peer_metadata": null
}
^5.61.0development
@typescript-eslint/eslint-plugin
Environment
{
  "local_name": "@typescript-eslint/eslint-plugin",
  "alias": false,
  "peer_metadata": null
}
^5.61.0development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "shasum": "1170c31c64a7f12e15b507d61996770a8919e870",
  "tarball": "https://registry.npmjs.org/2d-game-grid/-/2d-game-grid-1.0.4.tgz",
  "fileCount": 52,
  "integrity": "sha512-gkkKr+u4QKHv7lw9WgoX7g7FrO0wfz/O7QUdHa+DkdCge1KdtxwDwyZ0UPrWFx0G4o7woUHEZaBjRTrcf2Oc4w==",
  "signatures": [
    {
      "sig": "MEQCIECeEx8TDr/gYX389s8U1R0/xrC92BqE26LbQHw4baO1AiAleAlkMxMnmNdL8N8yrMY/5PYoHQJYK/aZfhz7528Y/Q==",
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"
    }
  ],
  "unpackedSize": 46120
}

Repository

NOT_FOUND
mstuercke/2d-game-grid

Shared repository data · GitLab

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 19:10 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:cdc7aea4635cd56048fd3149ad81ac8aa2adef6f4c176a155794566ddc1ece7c

Other collected versions

1.0.63 Aug1.0.518 Jul1.0.314 Jul1.0.214 Jul1.0.113 Jul1.0.013 JulView all collected versions →