Cell
geometry/cell.ts:64 Extends Point
A single addressable position in a Grid, carrying both gameplay data and the metadata the grid's algorithms read.
Cell is-a Point
Cell extends Point, so its x/y are its column/row in the grid
and every read-only point query works between cells out of the box —
Point.distanceTo, Point.angleTo, Point.equals,
Point.dot, and so on. For grid-native step counts, prefer
Cell.manhattanDistanceTo / Cell.chebyshevDistanceTo over the
inherited Euclidean Point.distanceTo.
Coordinates are immutable
A cell's coordinate is its identity within the grid index, so it cannot
change. Writing cell.x/cell.y — or calling any of Point's in-place
vector mutators (add, scale, forward, lerp, …), which assign through
those setters — throws an EngineError with
ErrorCode.GRID_CELL_IMMUTABLE. To do vector math, take a detached
copy first: Point.clone returns a plain, mutable Point.
Data and metadata
- Cell.data is the arbitrary gameplay payload (a wall, an actor, an
item, …) — set it, read it, clear it to
null. - Cell.passable and Cell.cost are the defaults
Grid.findPath reads. Mark walls
passable = false; raisecostto make the search route around difficult terrain.
Cells are created by their owning Grid; you never construct one directly. Resolve them with Grid.cellAt and navigate with Cell.neighbors / Cell.left and friends.
Example
const grid = new Grid<'wall' | 'floor'>(10, 10, () => 'floor');
const cell = grid.cellAt(2, 3)!;
cell.data = 'wall';
cell.passable = false;
const right = cell.right(); // Cell | null
const reach = cell.manhattanDistanceTo({ x: 5, y: 3 }); // 3
const detached = cell.clone().add(1, 0); // plain Point; cell is untouchedProperties
cost: number The cost of stepping into this cell, read by Grid.findPath as the default movement cost (overridable per call via PathfindingOptions.cost). Must be positive. Defaults to DEFAULT_CELL_COST.
data: null | TData The arbitrary gameplay payload occupying this cell, or null when the
cell is empty. Mutable: assign to populate, set to null to clear.
grid: Grid<TData> The Grid this cell belongs to. Used to resolve neighbours and wrap coordinates; a cell can only ever be navigated within its own grid.
passable: boolean Whether the cell may be stepped into. Consumed by Grid.findPath as the default walkability test (overridable per call via PathfindingOptions.isPassable). Defaults to DEFAULT_CELL_PASSABLE.
Accessors
angle: number Calculates the angle of this point in radians, measured from 0,0 as
atan2(y, x). The result is in the range (-π, π].
length: number Calculates the length (magnitude) of this point measured as a vector from
0,0.
lengthSquared: number Calculates the squared length of this point measured from 0,0. Cheaper
than Point.length (no square root) and sufficient when only
comparing magnitudes.
x: number The column (x coordinate) of this cell. Read-only — see the class docs on coordinate immutability.
y: number The row (y coordinate) of this cell. Read-only — see the class docs on coordinate immutability.
Methods
add(x: number, y: number): this Adds the input values to this point's coordinates.
Parameters
-
xnumber -
ynumber
Returns
this This point, for chaining.
add(point: PointPrimitive): this Adds another point's coordinates to this point's coordinates.
Parameters
-
pointPointPrimitive
Returns
this This point, for chaining.
angleTo(target: PointPrimitive): number Calculate the angle from this point to a target point.
Parameters
-
targetPointPrimitive
Returns
number chebyshevDistanceTo(target: PointPrimitive): number The Chebyshev (chessboard) distance to a target — max(|dx|, |dy|). This
is the step count between cells under 8-connected movement, where a
diagonal step costs the same as an orthogonal one.
Parameters
-
targetPointPrimitive
Returns
number cloneToImmutablePrimitive(): Readonly Returns an immutable primitive clone of this point, where the x and y
values are frozen at runtime and marked readonly by the compiler.
Returns
Readonly cloneToTuple(): [number, number] Returns a tuple clone of this point expressed as [x, y].
Returns
[number, number] copyFrom(source: PointPrimitive): this Copies the coordinates from another point into this one.
Parameters
-
sourcePointPrimitive
Returns
this This point, for chaining.
cross(target: PointPrimitive): number Calculate the 2D cross product (the z-component of the 3D cross product)
between this point and a target point, treating both as vectors from the
origin 0,0. The sign indicates winding direction.
Parameters
-
targetPointPrimitive
Returns
number distanceSquaredTo(target: PointPrimitive): number Calculate the squared distance from this point to a target point. Cheaper than Point.distanceTo (no square root) and sufficient when only comparing distances.
Parameters
-
targetPointPrimitive
Returns
number distanceTo(target: PointPrimitive): number Calculate the distance from this point to a target point.
Parameters
-
targetPointPrimitive
Returns
number dot(target: PointPrimitive): number Calculate the dot product between this point and a target point, treating
both as vectors from the origin 0,0.
Parameters
-
targetPointPrimitive
Returns
number equals(target: PointPrimitive, precision: number): boolean Determines whether this cell sits at the same grid coordinate as a target.
Overrides Point.equals to default precision to 0 (exact)
rather than Point's deliberately-loose 1. Cell coordinates are
discrete integers, so the inherited tolerance of 1 would report two
adjacent cells as equal — almost never what grid code wants. Within a
single grid, cells are stable singletons, so === identity is the
cheapest exact test; this method is for comparing against a bare
coordinate (e.g. a { x, y } target).
Parameters
-
targetPointPrimitive -
precisionnumber
Returns
boolean forward(distance: number): this Move this point forward by a given distance along Point.angle —
i.e. along the ray from the origin 0,0 through this point, treating the
point as a vector.
Parameters
-
distancenumber
Returns
this This point, for chaining.
lerp(target: PointPrimitive, t: number): this Linearly interpolates this point toward a target point by a fraction t,
where 0 leaves this point unchanged and 1 moves it onto the target.
Values outside 0..1 extrapolate.
Parameters
-
targetPointPrimitive -
tnumber
Returns
this This point, for chaining.
manhattanDistanceTo(target: PointPrimitive): number The Manhattan (taxicab) distance to a target — |dx| + |dy|. This is the
step count between cells under 4-connected movement.
Parameters
-
targetPointPrimitive
Returns
number moveInDirection(angle: number, distance: number): this Move this point in a given direction by a given distance.
Parameters
-
anglenumber -
distancenumber
Returns
this This point, for chaining.
moveTowards(target: PointPrimitive, distance: number): this Move this point toward a target point by a given distance. The point will
not overshoot: if the target is nearer than distance, this point lands
exactly on the target.
Parameters
-
targetPointPrimitive -
distancenumber
Returns
this This point, for chaining.
negate(): this Negates this point's coordinates, producing the opposing vector.
Returns
this This point, for chaining.
neighbors(options: NeighborOptions): Cell<TData>[] The cells adjacent to this one. Orthogonal (4-connected) by default; pass
{ diagonal: true } for the 8-connected ring. Out-of-bounds neighbours are
dropped unless { wrap: true } is given, in which case they wrap across
edges (this cell itself is never returned).
Parameters
-
optionsNeighborOptions
Example
const orthogonal = cell.neighbors(); // up to 4
const all = cell.neighbors({ diagonal: true }); // up to 8
const open = cell.neighbors().filter((c) => c.passable);normalize(): this Normalizes this point in place so it becomes a unit vector pointing in the
same direction. Normalizing a zero-length point leaves it at 0,0.
Returns
this This point, for chaining.
offset(dx: number, dy: number, wrap: boolean): null | Cell<TData> Resolves the cell at a relative [dx, dy] offset from this one. The
primitive behind all the directional accessors.
Parameters
-
dxnumber -
dynumber -
wrapboolean
rotate(radians: number, origin?: PointPrimitive): this Rotates this point around an origin by the given angle.
Parameters
-
radiansnumber -
origin?PointPrimitive
Returns
this This point, for chaining.
scale(x: number, y: number): this Scales this point's coordinates by the given factors. A single argument scales both axes uniformly.
Parameters
-
xnumber -
ynumber
Returns
this This point, for chaining.
set(x: number, y: number): this Sets the coordinates of this point.
Parameters
-
xnumber -
ynumber
Returns
this This point, for chaining.
snap(x: number, y: number): this Updates the coordinates of this point so the position is "snapped" to the
nearest cell corner in an abstract grid as defined by the input x and
y values. A spacing of 0 on an axis collapses that axis to 0.
Parameters
-
xnumber -
ynumber
Returns
this This point, for chaining.
subtract(x: number, y: number): this Subtracts the input values from this point's coordinates.
Parameters
-
xnumber -
ynumber
Returns
this This point, for chaining.
subtract(point: PointPrimitive): this Subtracts another point's coordinates from this point's coordinates.
Parameters
-
pointPointPrimitive
Returns
this This point, for chaining.
toString(): string Returns a string representation of this cell (its grid coordinate).
Returns
string from(value: PointPrimitive | readonly [number, number]): Point Creates a new point from an existing point primitive or an [x, y] tuple.
Parameters
-
valuePointPrimitive | readonly [number, number]
Returns
Point