Options
All
  • Public
  • Public/Protected
  • All
Menu

Class SpatialGrid2D<TElement>

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:

  • The grid is divided into buckets, each bucket the size specific in the constructor
  • When an element is inserted into the grid it's added to a bucket that matches its position
  • When retrieving elements at a certain position only one bucket has to be iterated over to find all the items
example
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);
typ

Type parameters

  • TElement

    identifies the data stored in the spatial grid.

Hierarchy

  • SpatialGrid2D

Index

Constructors

constructor

  • Parameters

    • gridWidth: number

      Width of the grid, must be an integer larger than zero

    • gridHeight: number

      Height of the grid, must be an integer larger than zero

    • bucketWidth: number

      Width of the bucket, must be an integer larger than zero

    • bucketHeight: number

      Height of the bucket, must be an integer larger than zero

    • Optional config: SpatialGrid2DConfig<TElement>

      Optional configuration

    Returns SpatialGrid2D

Accessors

bucketHeight

  • get bucketHeight(): number

bucketWidth

  • get bucketWidth(): number

gridHeight

  • get gridHeight(): number

gridWidth

  • get gridWidth(): number

size

  • get size(): number

Methods

clear

  • clear(): void

forEach

get

  • get(x: number, y: number, out?: TElement[]): TElement[]
  • Retrieves all elements that occupy the specified position

    throws

    {Error} Thrown when x or y is out of bounds

    Parameters

    • x: number
    • y: number
    • Optional out: TElement[]

      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

    Returns TElement[]

    An array of elements. When out param is provided it will be returned

insert

  • The same as insertAt but x and y are taken from the element

    throws

    Throws the same exceptions as insertAt

    Parameters

    • element: TElement & WithPosition

      An element that has x and y fields

    Returns void

insertAt

  • insertAt(x: number, y: number, element: TElement): void
  • Inserts an element into the grid at specified position.

    throws

    {Error} Thrown when position is out of bounds.

    Parameters

    • x: number

      X position to insert at

    • y: number

      Y position to insert at

    • element: TElement

      ELement to insert

    Returns void

move

  • move(fromX: number, fromY: number, toX: number, toY: number, element: TElement): void
  • A shortcut for removeAt followed by insertAt

    warning

    The operation is not atomic. If the element is removed successfully but the insertion fails the item will no longer exist in the grid.

    throws

    {Error} Throws the same errors as removeAt and insertAt

    Parameters

    • fromX: number
    • fromY: number
    • toX: number
    • toY: number
    • element: TElement

    Returns void

remove

  • The same as insertAt but x and y are taken from the element

    throws

    Throws the same exceptions as removeAt

    Parameters

    • element: TElement & WithPosition

      An element that has x and y fields

    Returns void

removeAllAt

  • removeAllAt(x: number, y: number): void
  • Removes all elements occupying the specified position.

    throws

    {Error} Thrown when position is out of bounds.

    Parameters

    • x: number

      X position to remove from

    • y: number

      Y position to remove from

    Returns void

removeAt

  • removeAt(x: number, y: number, element: TElement): void
  • Removes an element from the grid.

    throws

    {Error} Thrown when position is out of bounds.

    throws

    {Error} Thrown when element does not reside in the specified position.

    Parameters

    • x: number

      X position to remove from

    • y: number

      Y position to remove from

    • element: TElement

      Element to remove

    Returns void

resize

  • resize(gridWidth?: number, gridHeight?: number, bucketWidth?: number, bucketHeight?: number): void
  • Resizes the grid dimensions and the dimensions of the buckets

    Parameters

    • Optional gridWidth: number

      New width of the grid, uses the current gridWidth if undefined.

    • Optional gridHeight: number

      New height of the grid, uses the current gridHeight if undefined.

    • Optional bucketWidth: number

      New width of the bucket, uses the current bucketWidth if undefined.

    • Optional bucketHeight: number

      New height of the bucket, uses the current bucketHeight if undefined.

    Returns void

Generated using TypeDoc