npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gamegrids

v1.0.0-alpha2

Published

a utility toolbelt for making grids for games (or apps)

Downloads

8

Readme

gamegrid logo

a library for creating 2D grids for games (or apps)

usage

npm install gamegrids

Require or import the library:

//CommonJS
const gg = require('gamegrids')
//don't forget the 's'     ^
//ES
import gg from 'gamegrids'

Or destructure only the functions you want (noting that ES needs an extra step here):

//CommonJS: 
const { create, insert, move } = require('gamegrids')

//ES: 
import gg from './dist/gamegrids.mjs' 
const { create, insert, move } = gg

introduction

Some terminology...

gg: This is the library

grid: This is your grid, you put interesting things in it

enty (or enties): These are the interesting things in your grid, typically defined by something that has information, moves, is interactive or has some other functionality you give it (p.s. - you still have to do most of the work). Each enty has at least one property called cell which represents its place in grid.

API


grid
new gg.grid(width, height)
Instantiates a new grid object & library combo (calls gg.create with supplied arguments)

let grid = new gg.grid(2,2)

  [  .  .  ]
  [  .  .  ]

  //{ width: 2, height: 2, enties: [] }

create
gg.create(width, height)
Returns a grid object with the specified width and height
(note: the forthcoming documentation will assume you are instead using the new gg.grid method as documented above)

let grid2 = gg.create(3,3)

  [  .  .  .  ]
  [  .  .  .  ]
  [  .  .  .  ]

  //{ width: 3, height: 3, enties: [] }

insert
grid.insert(label, enty, cell, extras)
Creates an enty inserted to specified cell with an optional object for extra properties.

grid.insert('h', 1)

  [  .  h  .  ]
  [  .  .  .  ]
  [  .  .  .  ]

  //{ width: 3, height: 3, enties: [ { label: 'h', cell: 1 } ] }

Arguments can be in any order: string label - the label property of the enty object enty - an plain object containing key/values, if .cell (int) is supplied the enty will be inserted there int cell - the cell to insert the enty

move
grid.move(enty, direction, enties, loopGrid, loopRow)

Move a given enty (an enty object or its label string) to the next adjacent cell in the given direction.

grid.move('h', 'south')

  [  .  .  .  ]
  [  .  h  .  ]
  [  .  .  .  ]

Enty will not be moved if destination cell is either beyond grid's edge (ie- bottom of map) unless loopGrid argument is supplied (pass extra parameter true)

Enty will also not be moved if the destination cell is occupied by an existing enty and said enty has property `{ passable : false } (ex: said enty is a tree or an NPC)

examine
grid.examine(cellNum)

Returns the first enty in a given cell.

grid.examine(4)

  [  .  .  .  ]
  [  .  h  .  ]
  [  .  .  .  ]

  //{ label: 'h', cell: 4 }

examineAll
grid.examineAll(cell)
Returns an array of all enties in a given cell

indexToRc
grid.indexToRc(cell)
Converts an index value (ie- cell number) to the equivalent row/column (array) value.

[  0  1  2  ]
[  3  4  5  ]
[  6  7  8  ]

grid.indexToRc(6)
//> [2, 0]

rcToIndex
grid.rcToIndex(row, column)
Converts a row/column pair to the equivalent array index (ie- as spread out in a single row)

grid.rcToIndex(2, 0)
//> 6

nextOpenCell
Returns the next open cell in the grid grid.nextOpenCell(startCell)

[  .  .  %  ]
[  &  #  .  ]
[  .  .  .  ]

grid.nextOpenCell(2)
//> 5

makeRegion
Returns an array of cell numbers that would fill a given region grid.makeRegion(startCell, width, height)

let grid = new gg.grid(3,3)
let region = grid.makeRegion(1, 2, 2)
//> [1, 2, 4, 5]
region.forEach((cellNum) => grid.insert(cellNum, '#'))

[  .  #  #  ]
[  .  #  #  ]
[  .  .  .  ]

more functions & examples

see test.js for more examples and gg itself for a number of other functions not yet documented.

Functional style vs portable API + grid object in 1

Functions can also accept a grid as a parameter. If a grid param is supplied a grid will be returned.

Otherwise the class-like instance/portable API will modify itself.

Ex:

//portable API / grid in one deal:
let grid = new gg.grid(6,6)
grid.insert('hero')
let grid2 = new gg.grid(12,12)
grid2.insert('zombie')
//(there is a hero in grid one but not grid2)

//alternatively, use functional style:
let grid = gg.create(6,6)
grid = grid.insert('hero')
let grid2 = gg.create(12,12)
grid2 = gg.insert('zombie')

*WIP as not all functions support the single instance/portable API technique

hacking & testing

Feel free to contribute to making this library better/faster!

git clone [email protected]:drschwabe/gamegrid.git

There is lot's of room for improvement in the performance deparment for example.

If you do make a change, please run the test suite to make sure no existing functionality breaks.

The library currently has 130 tests that check core features and various other functions. Ideally, you can also write a test that checks your newly intended functionality (if any) works too.

npm test

To build and test the ES version do:

npm run build
npm run test-es

TODO / need help

  • finish documentation (refer to ./gg.js for other functions/features not yet documented)
  • fully integrate portable API with all functions (not all functions support grid as standalone API; when in doubt use functional style outlined above)
  • write tests for functions that are not yet tested
  • write more tests for functions that are not thoroughly tested
  • consider an improved data model and/or approach to caching the grid enties ie- instead of calling populateCells
  • improve performance
  • write tests to check performance