Struct SquareGrid
A 2D-grid where each cell is defined as an int2 Travelling one cell along the map has a cost of 1 + destination cell cost. Travelling diagonally has a cost of sqrt(2) + destination cell cost. Unset cells have a default cost of zero. To make a cell unwalkable, assign it a cost of infinity. This means that every location is walkable by default.
Namespace: AnyPath.Graphs.SquareGrid
Assembly: AnyPath.dll
Syntax
public struct SquareGrid : IGraph<SquareGridCell>, IDisposable
Constructors
SquareGrid(int2, int2, SquareGridType, IReadOnlyList<SquareGridCell>, Allocator)
Constructs a new grid
Declaration
public SquareGrid(int2 min, int2 max, SquareGridType neighbourMode, IReadOnlyList<SquareGridCell> cells, Allocator allocator)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | min | Boundary min |
| int2 | max | Boundary max |
| SquareGridType | neighbourMode | Should cells have four or eight neighbours? |
| IReadOnlyList<SquareGridCell> | cells | Array containing the cells to set initially |
| Allocator | allocator | Allocator to use |
SquareGrid(int2, int2, SquareGridType, int, Allocator)
Constructs a new grid
Declaration
public SquareGrid(int2 min, int2 max, SquareGridType neighbourMode, int capacity, Allocator allocator)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | min | Boundary min |
| int2 | max | Boundary max |
| SquareGridType | neighbourMode | Should cells have four or eight neighbours? |
| int | capacity | Initial capacity of the internal hashmap |
| Allocator | allocator | Allocator to use |
Fields
max
The boundary -max- position
Declaration
public readonly int2 max
Field Value
| Type | Description |
|---|---|
| int2 |
min
The boundary -min- position
Declaration
public readonly int2 min
Field Value
| Type | Description |
|---|---|
| int2 |
neighbourMode
Type of grid, 4 our 8 neighbours
Declaration
public readonly SquareGridType neighbourMode
Field Value
| Type | Description |
|---|---|
| SquareGridType |
Methods
Collect(SquareGridCell, ref NativeList<Edge<SquareGridCell>>)
Collects all neighbouring cells from a given location
Declaration
public void Collect(SquareGridCell node, ref NativeList<Edge<SquareGridCell>> edgeBuffer)
Parameters
| Type | Name | Description |
|---|---|---|
| SquareGridCell | node | The location to find the neighbours for |
| NativeList<Edge<SquareGridCell>> | edgeBuffer |
GetCell(int2)
Returns the cell at a given position.
Declaration
public SquareGridCell GetCell(int2 position)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | position |
Returns
| Type | Description |
|---|---|
| SquareGridCell |
GetCost(int2)
Returns the enter cost of a given position. Note that unset cells are considered open and have an entering cost of zero.
Declaration
public float GetCost(int2 position)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | position |
Returns
| Type | Description |
|---|---|
| float |
GetEnumerator()
Enumerates all of the cells in the grid, including unset ones. This can be used for constructing ALT heuristics.
Declaration
public SquareGrid.Enumerator GetEnumerator()
Returns
| Type | Description |
|---|---|
| SquareGrid.Enumerator |
GetSetCells(Allocator)
Allocates an array containing all of the cells that are set on this grid.
Declaration
public NativeArray<SquareGridCell> GetSetCells(Allocator allocator)
Parameters
| Type | Name | Description |
|---|---|---|
| Allocator | allocator |
Returns
| Type | Description |
|---|---|
| NativeArray<SquareGridCell> |
InBounds(int2)
Returns wether a certain position is within the bounds of the grid
Declaration
public bool InBounds(int2 position)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | position |
Returns
| Type | Description |
|---|---|
| bool |
IsOpen(int2)
Returns wether a cell at a position is open/walkable.
Declaration
public bool IsOpen(int2 position)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | position |
Returns
| Type | Description |
|---|---|
| bool |
SetCell(int2, float, int)
Sets the cost for a cell.
Declaration
public void SetCell(int2 position, float enterCost, int flags = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| int2 | position | Position to set |
| float | enterCost | Additional cost for walking this cell. Use float.PositiveInfinity to make this cell unwalkable |
| int | flags | Flags for this cell, this can be used in conjunction with FlagBitmask<TNode> to exclude certain areas. |
Remarks
No bounds checking is done on the position