Skip to content

Spatial Hash Grid that extends Map & incrementally assigns IDs

License

Notifications You must be signed in to change notification settings

eekelof/ICellMap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ICellMap

npm npm GitHub

Spatial Hash Grid that extends Map & incrementally assigns IDs

Features

  • ✔️ Elements are stored on a 2D grid
  • ✔️ Filter nearby elements
  • ✔️ Extends Map
  • ✔️ Incrementally assigns IDs
  • 🟦 Written in TypeScript

Installation

Using npm

npm i @wingit/icellmap

Using bun

bun i @wingit/icellmap

Usage

import ICellMap from "@wingit/icellmap";

// create a ICellMap(width, height, cell size)
const units = new ICellMap(100, 100, 10);

// add a unit
// set id & hash to -1, it is used internally and gets reassigned when added
const unit = { id: -1, hash: -1, pos: {x: 0, y: 0}, name: "Bob" };
const id = units.add(unit);

console.log(id); // 0
console.log(unit.id); // 0

// delete a unit
units.delete(id);

// update a unit
unit.pos.x = 35;
units.update(unit);

// get cells in range
const cellsInRange = g.ig.units.query(pos, radius);
for (const cell of cellsInRange) {
    for (const u of cell){
        console.log(u);
    }
}

License

MIT

About

Spatial Hash Grid that extends Map & incrementally assigns IDs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published