# Class: SpatialHash\<T\>

Defined in: ../debuff/packages/core/src/spatial.ts:32

A uniform grid for "what's near this" queries: enemies near the player, shots against a crowd, a swarm
keeping its members apart. Put items in with `rebuild(items)` once per step (or `clear` and `insert`), then
ask `near` or `nearest`, which only look at the cells around the point instead of every item.

Pick a cell size about the radius you query most. Results come in a fixed order (cells row by row, items
in insertion order), so replays and tests stay deterministic. After the first few steps it allocates
nothing: cell arrays are reused, and cells that empty out are released. Items are points: for big objects,
query with the radius plus their size. Don't insert or rebuild while a query is running; nested queries are fine.

```ts
const grid = new SpatialHash<Enemy>(24);
// in update, after moving:
grid.rebuild(enemies);
grid.near(shot.x, shot.y, 12, (e) => {
  hit(e);
  return true; // stop at the first
});
const target = grid.nearest(player.x, player.y, 200, (e) => !e.dead);
```

## Type Parameters

### T

`T` *extends* [`Point`](../interfaces/Point.md)

## Constructors

### Constructor

> **new SpatialHash**\<`T`\>(`cellSize?`): `SpatialHash`\<`T`\>

Defined in: ../debuff/packages/core/src/spatial.ts:38

#### Parameters

##### cellSize?

`number` = `32`

#### Returns

`SpatialHash`\<`T`\>

## Properties

### cellSize

> `readonly` **cellSize**: `number`

Defined in: ../debuff/packages/core/src/spatial.ts:33

## Accessors

### size

#### Get Signature

> **get** **size**(): `number`

Defined in: ../debuff/packages/core/src/spatial.ts:45

How many items are in the grid.

##### Returns

`number`

## Methods

### clear()

> **clear**(): `void`

Defined in: ../debuff/packages/core/src/spatial.ts:54

Empty the grid, keeping its arrays for the next fill. Cells keep their place in the map from one fill to
the next (most are refilled, and re-adding them was a third slower); a cell that stayed empty for a whole
fill is released, so a world explored over time doesn't keep growing the grid.

#### Returns

`void`

***

### insert()

> **insert**(`item`): `void`

Defined in: ../debuff/packages/core/src/spatial.ts:65

Add one item at its current position. Moving it later doesn't move it in the grid: rebuild each step.

#### Parameters

##### item

`T`

#### Returns

`void`

***

### near()

> **near**(`x`, `y`, `radius`, `visit`): `boolean`

Defined in: ../debuff/packages/core/src/spatial.ts:86

Call `visit` for every item within `radius` of (x, y), measured centre to centre. Return `true` from `visit`
to stop. Returns true if it was stopped.

#### Parameters

##### x

`number`

##### y

`number`

##### radius

`number`

##### visit

(`item`, `distanceSquared`) => `boolean` \| `undefined`

#### Returns

`boolean`

***

### nearest()

> **nearest**(`x`, `y`, `radius`, `accept?`): `T` \| `undefined`

Defined in: ../debuff/packages/core/src/spatial.ts:107

The item closest to (x, y) within `radius` that `accept` allows (default: any). Ties go to the first found.

#### Parameters

##### x

`number`

##### y

`number`

##### radius

`number`

##### accept?

(`item`) => `boolean`

#### Returns

`T` \| `undefined`

***

### rebuild()

> **rebuild**(`items`): `void`

Defined in: ../debuff/packages/core/src/spatial.ts:77

`clear()`, then insert every item. Call it once per step after things have moved.

#### Parameters

##### items

`Iterable`\<`T`\>

#### Returns

`void`
