identifies the data stored in the spatial grid.
Width of the grid, must be an integer larger than zero
Height of the grid, must be an integer larger than zero
Width of the bucket, must be an integer larger than zero
Height of the bucket, must be an integer larger than zero
Optional configuration
Height of a single bucket
Width of a single bucket
Height of the grid
Width of the grid
Number of elements stored in the grid
Removes every element from the grid.
Iterates over every element in the grid
Retrieves all elements that occupy the specified position
When provided instead of creating a new array to store the elements they will be pushed on top of this array. Useful both as an out param and to avoid unnecessary instantiation of arrays
An array of elements. When out
param is provided it will be returned
The same as insertAt
but x
and y
are taken from the element
An element that has x
and y
fields
Inserts an element into the grid at specified position.
X position to insert at
Y position to insert at
ELement to insert
A shortcut for removeAt
followed by insertAt
The same as insertAt
but x
and y
are taken from the element
An element that has x
and y
fields
Removes all elements occupying the specified position.
X position to remove from
Y position to remove from
Removes an element from the grid.
X position to remove from
Y position to remove from
Element to remove
Resizes the grid dimensions and the dimensions of the buckets
New width of the grid, uses the current gridWidth
if undefined
.
New height of the grid, uses the current gridHeight
if undefined
.
New width of the bucket, uses the current bucketWidth
if undefined
.
New height of the bucket, uses the current bucketHeight
if undefined
.
Generated using TypeDoc
A two dimensional grid that allows multiple elements to occupy the same space and divides its space into buckets in order to speed up operations. It's a combiation of spatial hash and a 2D grid created specifically for grid-based games.
The specific way it works is:
const grid = new SpatialGrid2D<Entity>(20, 15, 3, 3, { destructorCallback: (entity) => entity && entity.releaseToPool() }); // Inserts a bunch of entities at a specific spot grid.insertAt(3, 3, new GoldPiece()); grid.insertAt(3, 3, new GoldPiece()); grid.insertAt(3, 3, new GoldPiece()); // Will return the three inserted GoldPieces console.log(grid.get(3, 3)); // Removes everything from a given space grid.removeAllAt(3, 3); // Removes a specific item from a specific position grid.removeAt(1, 2, player); // Shrinks one dimension and expands another - the removed positions will call `destructorCallback` // and the new ones will be populated by a call to `defaultValueProvider` grid.resize(15, 20); // Changes the buckets' dimensions grid.resize(undefined, undefined, 2, 2);