Components
Last updated
Last updated
The PathGrid system is the main component of PathGrid. It simply handles the public methods for adding and removing path cells. This is also where you add the available PathPresets and PathNetworks in the Unity inspector.
PathGrid supports multiple path networks. A path network is a "closed" path system where you can add or remove path cells. So for example if you have a SimCity like game, you would probably have a PathNetwork for the roads, then a seperate one for the sewage pipes and another one for the electricity.
Every PathNetwork has it's own grid where additional data is being stored for each cell such as the cell type. A cell type can either be:
None
Water
Terrain
Blocked
Using the API you can easily define those cell types. So if you place a building for example you can then set the cell type to "Blocked" on the buildings position.
Here's an example code to set a cell type:
Using cell types is optional. If you already have your own proprietary solution for blocking cells and different cell types you can of course use your own.
To create a new preset simply right click in the project view and select: Create / PathGrid / Path Preset
A new preset asset is being created. A path consists of six tiles. Make sure to assign them all in the preset. You can also set a Y rotation offset to some tiles, depending if they're rotated the wrong way on creation.
Additionally you can also set the road type which allows you to check if the current cell at position (SetCellTypeAtPosition) equals to the current selected path type. This is being done in the PathGridInputSystem script for example. (Line: 392)
This script handles all the user input as well as checking for the cell type at current mouse grid position.
The PathGridInputSystem can be seen as an example on how to collect path cells positions from the users mouse position and then passing them on to the PathGridSystem (AddPathCells).
What this script do:
When system is enabled, we wait for the users first click (clickCount)
When user has clicked once, we start previewing the mouse grid position by instantiating preview blocks.(PreviewPaths)
The directionLockDistance defines how many cells we should "lock" a direction for the path preview (+x,-x,+y,-y). Every further movement of the mouse is then being previewed in perpendicular direction. like an "L"
If checkForCellTypesOnPathNetwork is enabled, we further check if the currently selected path prefabs cell type matches the path networks cell type. A path network cell type can be defined by using: SetCellTypeAtPosition. Following types can be set: * None * Terrain
* Water
* Blocked
When user clicks the second time we clear the preview blocks and send the collected positions to the PathGridSystem which takes care of building the path.
Right mouse click aborts the preview and resets the click count.