|
Unity Grid Toolkit
Utilitary API to proceed operations on abstract grids such as tile extraction, raycasting, and pathfinding.
|
Utilitary API to proceed operations on abstract grids such as tile extraction, raycasting, and pathfinding.
All you need to use this API is a bi-dimensional array of tiles ordered in row major order (see below).
What is a tile ? Any object (custom class, struct, component, ...) that implements the very light ITile (IWeightedTile for DijkstraGrid) interface of this library. This interface requires three properties getters:
This API is using a namespace so you have to add a using instruction to the scripts that will use this library:
When working with two-dimensional arrays there is two ways of storing tiles, first rows then lines or the opposite.
This is called the Major Order. The most common major order used in C languages (and the one used in this library) is the row major order, meaning that the first index of the array represents the row index and the second index represents the column index.
Be aware that the row index actually indicates the vertical position of the tile in the grid, and the column index indicates the horizontal position of the tile in the grid, as it can be counter intuitive. For more information you can refer to this Wikipedia article.
Allows you to extract tiles on a grid.
Provides shape extraction (rectangles, circles, cones and lines) and neighbors extraction with a lot of parameters.
You can extract tiles from shapes.
You can extract neighbors of a tile (if existing).
Each extraction method has a variant to check if a specific tile would be extracted
Allows you to cast lines of sight and cones of vision on a grid
You can get the line of sight from a tile (a line that "stops" at the first encountered unwalkable tile).
Many signatures are available to specify the length and direction of the line.
Allows you to calculate paths between tiles.
This API offers several ways to do pathfinding, depending on your needs.
You can generate objects that can be seen as layers of data on top of your grid. Once generated, these objects allows you to get paths with almost no performance cost.
A DirectionPath object holds direction data for all tiles on the path between two tiles.
A DijkstraPath object holds both direction and distance data for all tiles on the path between two tiles.
A DirectionField object holds direction data between a target tile and all the tiles that are accessible to this target into a specified maximum distance range.
A DijkstraField object holds both direction and distance data between a target tile and all the tiles that are accessible to this target into a specified maximum distance range.
A DirectionGrid object holds direction data between a target tile and all the tiles that are accessible to this target, on the entire grid.
A DijkstraGrid object holds both direction and distance data between a target tile and all the tiles that are accessible to this target, on the entire grid.
A DirectionAtlas object holds DirectionGrid objects for each tile.
A DijkstraAtlas object holds DijkstraGrid objects for each tile.
Note that, obviously, any path calculation is valid as long as the user grid, walkable states (and weights for dijkstra objects) of the tiles, remains unchanged
By default, paths are calculated with orthogonal movements only (up, down, left, right). This is the most efficient way to calculate paths. You can allow diagonal movements but you have to decide the tolerance regarding the walls common neibours. You can use the DiagonalsPolicy optional parameter, in any pathfinding calculation method, to allow and tune diagonal movements.
Take a look at this schematic to understand how it works:
When moving diagonally from one tile to another, there is actually more distance covered than when moving with orthogonal movement. Mathematically, when the orthogonal distance between two adjacent tiles is 1, then the diagonal distance between two diagonally adjacent tiles is roughly 1.414. The detailed calculation is Sqrt(x_distance²+y_distance²). Although it is the most commonly used diagonal movement cost value, you can decide to use any value superior or equal to 1.
Every pathfinding calculation method has an asynchronous variant, that returns a Task, with additional optional parameters to handle cancellation and be notified of progress.
A DirectionPath object holds direction data for all tiles on the path between two tiles.
To generate a DirectionPath, use the GenerateDirectionPath method that needs the grid, the target tile and the *start** tile from which to calculate the path, as parameters.
A DijkstraPath object holds both direction and distance data for all tiles on the path between two tiles.
To generate a DijkstraPath, use the GenerateDisjktraPath method that needs the grid, the target tile and the *start** tile from which to calculate the path, as parameters.
A DirectionField object holds direction data between a target tile and all the tiles that are accessible to this target into a specified maximum distance range.
To generate a DirectionField object, use the GenerateDirectionField method that needs the grid and the target tile from which to calculate the paths, as parameters.
You can access and iterate over all the accessible tiles.
A DijkstraField object holds both direction and distance data between a target tile and all the tiles that are accessible to this target into a specified maximum distance range.
To generate a DijkstraField object, use the GenerateDijkstraField method that needs the grid and the target tile from which to calculate the paths, as parameters.
You can access and iterate over all the accessible tiles and also get their distance to the target.
A DirectionGrid object holds direction data between a target tile and all the tiles that are accessible to this target, on the entire grid.
To generate a DirectionGrid object, use the GenerateDirectionGrid method that needs the grid and the target tile from which to calculate the paths, as parameters.
You can retrieve the tile that has been used as the target to generate this DirectionGrid.
You can get all the tiles on the path from a tile to the target.
Or you can get all the tiles on the path from the target to a tile.
You can know if a tile is accessible from the target tile. This is useful before calling the following DirectionGrid methods that only takes an accessible tile as parameter.
You can get the next tile on the path between the target and a tile.
You can get the next tile direction on the path between the target and a tile (in 2D grid coordinates). NextTileDirection is an enum representing the eight possible directions.
You can serialize the generated DirectionGrid to a byte array. Usefull for path baking in edit time.
You can deserialize a byte array to a DirectionGrid. Usefull for loading baked paths at runtime.
A DijkstraGrid object holds both direction and distance data between a target tile and all the tiles that are accessible to this target, on the entire grid.
To generate a DijkstraGrid object, use the GenerateDijkstraGrid method that needs the grid and the target tile from which to calculate the paths, as parameters.
You can get the distance between the target and a tile.