Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Grid2D<TElement>

A two dimensional grid.

example
const grid = new Grid2D<Entity|undefined>(20, 15, {
    defaultValueProvider: () => undefined,
    destructorCallback: (entity) => entity && entity.releaseToPool()
});

// Sets the value at the specified position
grid.set(0, 0, undefined);

// Gets a value at the specified position
console.log(grid.get(0, 0));

// Checks if a given position is valid
console.log(grid.isValidPosition(90, 90));

// Populate all grid position with the same value
grid.setAll(undefined);

// Populate all grid positions by a callback
grid.setAllCallback((x, y) => levelBlueprint.createEntityFor(x, y));

// Iterate over every position in the grid
grid.forEach(entity => entity.update());

// 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);

Type parameters

  • TElement

    identifies the data stored in the Grid. By default this class does not allow nullable or optional values, but this can be accomplished by using Union types, eg. Grid2D<Entity|undefined>.

Hierarchy

  • Grid2D

Index

Constructors

constructor

  • throws

    {Error} Thrown when either width or height is non-integer or less than 1.

    Parameters

    • width: number

      Width of the grid, must be an integer larger than 0.

    • height: number

      Height of the grid, must be an integer larger than 0.

    • config: Grid2DConfig<TElement> | Grid2DValueProvider<TElement>

      The creation callback or the full configuration. In order to easily set the the same value for every space just use () => undefined.

    Returns Grid2D

Accessors

UNSAFE_internalArray

  • get UNSAFE_internalArray(): keyof TElement[][]
  • Returns the array internally used by the Grid. It's asdvisable to not modify the return value of this getter, only ever reference it for faster access or quick serialization.

    This is an unsafe method because the way the array is stored internally may be changed without warning.

    format

    The structure used is a jagged array (an array of arrays) where the first dimension is X and second is Y. For example: the equivalent of get(1,2) is UNSAFE_internalArray[1][2]. For example: Structure of a 2x3 grid is:

    [
      [0x0, 0x1, 0x2]
      [1x0, 1x1, 1x2]
    ]

    Returns keyof TElement[][]

height

  • get height(): number
  • The current height of the grid

    Returns number

width

  • get width(): number
  • The current width of the grid

    Returns number

Methods

forEach

  • Iterates over every square in the array.

    Parameters

    • callback: Grid2DIterateCallback<TElement>

      Function called with every element and its position in the grid.

    Returns void

get

  • get(x: number, y: number): TElement
  • Retrieved the element stored at the specified space.

    throws

    {Error} Thrown when X or Y is outside bounds of the grid.

    Parameters

    • x: number

      X position in the grid to read from.

    • y: number

      Y position in the grid to read from.

    Returns TElement

    The element at the specified position in the grid.

isValidPosition

  • isValidPosition(x: number, y: number): boolean
  • Checks whether a given position falls inside the bounds of the grid.

    Parameters

    • x: number

      X position in the grid to check.

    • y: number

      Y position in the grid to check.

    Returns boolean

    True if the position is inside the grid.

resize

  • resize(newWidth: number, newHeight: number): void
  • Resizes the grid. If any of the values are "lost" due to shrinking, destructorCallback from the configuration will be called (if specified).

    throws

    {Error} Thrown when either newWidth or newHeight is non-integer or less than 1.

    Parameters

    • newWidth: number

      New width of the grid, must be an integer larger than 0.

    • newHeight: number

      New height of the grid, must be an integer larger than 0.

    Returns void

set

  • set(x: number, y: number, element: TElement): void
  • Stores an element at the specified space.

    throws

    {Error} Thrown when X or Y is outside bounds of the grid.

    Parameters

    • x: number

      X position in the grid to write to.

    • y: number

      Y position in the grid to write to.

    • element: TElement

      Element to store

    Returns void

setAll

  • setAll(value: TElement): void
  • Sets all spaces in the grid to the specified value.

    Parameters

    • value: TElement

      The new value

    Returns void

setAllCallback

  • Sets all spaces in the grid to a value returned from the callback.

    Parameters

    • callback: Grid2DValueProvider<TElement>

      A function that accepts the X and Y coordinate of the grid square and returns the new value for that square.

    Returns void

toArray

  • toArray(): TElement[][]
  • Creates a copy of the internal array.

    Returns TElement[][]

    A jagged array (an array of arrays) where the first dimension is X and second is Y. For example: the equivalent of get(1,2) is UNSAFE_internalArray[1][2]. For example: Structure of a 2x3 grid is:

    [
      [0x0, 0x1, 0x2]
      [1x0, 1x1, 1x2]
    ]

Generated using TypeDoc