private unsafe static void TestNeighbour(NativeArray2D <MapNode> gridCopy, ref MapNode kNeighbour, MapNode *currentNodePtr, NativeList <MapNode> neighbours) { //If node is open, (eg. already tested previously), we compare the g distances to see if traversing the neighbouring node will be more efficient from this node than its parents node. if (kNeighbour.state == NodeState.Open) { float gDistance = math.distancesq(kNeighbour.position, kNeighbour.parent->position); float tempGDistance = currentNodePtr->g + gDistance; if (tempGDistance < kNeighbour.g) { MapNode *neighbourPtr = gridCopy.GetPointerToElement(kNeighbour.gridPosition); neighbourPtr->SetParent(currentNodePtr); neighbours.Add(*neighbourPtr); } } //If we're untested, we don't have a parent already, so set parent and add to list. else { MapNode *neighbourPtr = gridCopy.GetPointerToElement(kNeighbour.gridPosition); neighbourPtr->SetParent(currentNodePtr); neighbourPtr->state = NodeState.Open; neighbours.Add(*neighbourPtr); } }
private static unsafe bool SearchForPath(int2 currentNode, int2 targetNode, NativeArray2D <MapNode> gridCopy) { MapNode *currentNodePtr = gridCopy.GetPointerToElement(currentNode); NativeList <MapNode> passableNeighbours = GetPassableNeighbours(currentNode, gridCopy, currentNodePtr); NodeCompare nodeCompare = new NodeCompare(); passableNeighbours.Sort(nodeCompare); currentNodePtr->state = NodeState.Closed; for (int i = 0; i < passableNeighbours.Length; ++i) { MapNode neighbour = passableNeighbours[i]; currentNodePtr->child = gridCopy.GetPointerToElement(neighbour.gridPosition); //Target has been reached. if (neighbour.gridPosition.Equals(targetNode)) { passableNeighbours.Dispose(); return(true); } //Recursively search deeper. if (SearchForPath(neighbour.gridPosition, targetNode, gridCopy)) { passableNeighbours.Dispose(); return(true); } } passableNeighbours.Dispose(); return(false); }
public float f; //Estimated total distance traveled if taking this node in the path. public void SetParent(MapNode *newParent) { if (newParent == null) { return; } parent = newParent; g = newParent->g + math.distancesq(position, newParent->position); f = g + h; }
private static unsafe NativeList <MapNode> GetPassableNeighbours(int2 currentNode, NativeArray2D <MapNode> gridCopy, MapNode *currentNodePtr) { NativeList <MapNode> neighbours = new NativeList <MapNode>(8, Allocator.Temp); //Find all neighbours on the grid by checking if all surrounding nodes are within grid bounds and are valid nodes to traverse to. for (int x = -1; x <= 1; ++x) { int xIndex = currentNode.x + x; if (!gridCopy.CheckXBounds(xIndex)) { continue; } for (int y = -1; y <= 1; ++y) { if (x == 0 && y == 0) { continue; } int yIndex = currentNode.y + y; if (!gridCopy.CheckYBounds(yIndex)) { continue; } MapNode neighbour = gridCopy[xIndex, yIndex]; if (neighbour.occupiedBy != OccupiedBy.Nothing || neighbour.state == NodeState.Closed) { continue; } TestNeighbour(gridCopy, ref neighbour, currentNodePtr, neighbours); } } return(neighbours); }