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>
.
Width of the grid, must be an integer larger than 0.
Height of the grid, must be an integer larger than 0.
The creation callback or the full configuration.
In order to easily set the the same value for every space just use () => undefined
.
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.
The current height of the grid
The current width of the grid
Iterates over every square in the array.
Function called with every element and its position in the grid.
Retrieved the element stored at the specified space.
X position in the grid to read from.
Y position in the grid to read from.
The element at the specified position in the grid.
Checks whether a given position falls inside the bounds of the grid.
X position in the grid to check.
Y position in the grid to check.
True if the position is inside the grid.
Resizes the grid. If any of the values are "lost" due to shrinking, destructorCallback
from the configuration
will be called (if specified).
New width of the grid, must be an integer larger than 0.
New height of the grid, must be an integer larger than 0.
Stores an element at the specified space.
X position in the grid to write to.
Y position in the grid to write to.
Element to store
Sets all spaces in the grid to the specified value.
The new value
Sets all spaces in the grid to a value returned from the callback.
A function that accepts the X and Y coordinate of the grid square and returns the new value for that square.
Creates a copy of the internal array.
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
A two dimensional grid.
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);