grafjs
Version:
2D graph for chunk-based games.
51 lines • 1.85 kB
Markdown
# GrafJS
[](https://npmjs.org/package/grafjs)
[](https://travis-ci.org/KonradLinkowski/GrafJS)
[](https://david-dm.org/KonradLinkowski/GrafJS)
[](https://david-dm.org/KonradLinkowski/GrafJS#info=devDependencies)
[](https://snyk.io/test/github/KonradLinkowski/GrafJS?targetFile=package.json)
[](https://codeclimate.com/github/KonradLinkowski/GrafJS/maintainability)
## Features
2D graph for chunk-based games.
- No dependencies
- Easy to use
- Lightweight
## Installing
```bash
$ npm install grafjs
```
## Example
Import
```js
const Graf = require('grafjs')
```
Creating a board where each chunk is 20 x 20.
```js
// creates 20 x 20 grid filled with 0's
const grid = new Array(20).fill(null).map(a => new Array(20).fill(0))
// creates the board with a default value for each chunk
const board = new Graf(grid)
// adds examplary chunk
board.addChunk(0, 0)
```
Getting specified chunk and adjacent chunks.
```js
// returns chunk at position (1, 3)
const chunk = board.getChunk(1, 3)
const left = chunk.getLeft()
const right = chunk.getRight()
const up = chunk.getUp()
const down = chunk.getDown()
```
Getting and setting chunk's content.
```js
// gets value
const content = chunk.getValue()
// sets value
chunk.setValue([1])
```
Removing a chunk
```js
// removes the chunk at (3, 4)
board.removeChunk(3, 4)
```