entityGrid
The EntityGrid class acts as a spatial partitioning grid to efficiently keep track of entity locations in the world. It maps entities into grid cells based on their positions and bounding sizes.
EntityGrid private
Bound to game.world as game.world.entityGrid.
Properties
| Property | Type | Description |
|---|---|---|
cellEntities | Array<Record<string, boolean>> | An array of cells, each containing a map of UIDs to true for entities within the cell. |
entityMap | Record<string, number[]> | A map mapping each entity UID to an array of cell indexes it occupies. |
oneGridSize | object | Default grid size object: { width: 1, height: 1 }. |
width | number | Total width of the grid. |
height | number | Total height of the grid. |
cellSize | number | Size of each grid cell. |
rows | number | Total number of rows in the grid. |
columns | number | Total number of columns in the grid. |
totalCells | number | Total number of cells (rows * columns). |
Methods
constructor()
function constructor(width: number, height: number, cellSize: number): voidInitializes the grid structure based on world dimensions and cell size.
getEntitiesInCell()
function getEntitiesInCell(index: number): Record<string, boolean> | falseReturns a map of entity UIDs present in the given cell index, or false if the index does not exist.
updateEntity()
function updateEntity(entity: NetworkEntity): voidUpdates the entity's position within the grid. Computes the cells the entity should occupy and updates the cell mappings if it has moved to different cells.
removeEntity()
function removeEntity(uid: string): voidRemoves the specified entity UID from the grid.
getCellIndexes()
function getCellIndexes(x: number, y: number, gridSize: object): (number | false)[]Calculates and returns the array of cell indexes that intersect with the given position and size. Out-of-bounds cells return false.
getCellCoords()
function getCellCoords(index: number): { x: number, y: number }Converts a 1D cell index into 2D grid coordinates (x, y).
getCellSize()
function getCellSize(): numberReturns the cell size.
getRows()
function getRows(): numberReturns the number of rows.
getColumns()
function getColumns(): numberReturns the number of columns.
removeEntityFromCells()
function removeEntityFromCells(uid: string, indexes: number[]): voidRemoves an entity UID from the specified cell indexes and the entityMap.
addEntityToCells()
function addEntityToCells(uid: string, indexes: number[]): voidAdds an entity UID to the specified cell indexes and updates the entityMap.