private void updatePathing(NativeList <PathNode> closedList)
 {
     setPathBuffer(closedList, pathRoute);
     routeFollow[entity] = new PathfindingRouteFollow {
         routeIndex = pathRoute.Length - 1
     };
 }
        public void Execute()
        {
            if (pathfindingType == PathfindingType.ToSpot &&
                !pathNodes[getNodeIndex(positionEnd.x, positionEnd.y)].walkable)
            {
                routeFollow[entity] = new PathfindingRouteFollow {
                    routeIndex = -1
                };
                return;
            }

            if (SuccessfullyReachedTarget(positionStart))
            {
                pathRoute.Clear();
                routeFollow[entity] = new PathfindingRouteFollow {
                    routeIndex = -1
                };
                return;
            }

            bool foundPath = false;

            NativeList <PathNode> openList   = new NativeList <PathNode>(Allocator.Temp);
            NativeList <PathNode> closedList = new NativeList <PathNode>(Allocator.Temp);

            PathNode startNode = pathNodes[getNodeIndex(positionStart.x, positionStart.y)];

            startNode.gCost = 0;
            startNode.hCost = CalculateHeuristicCost(startNode.toInt2());
            startNode.UpdateFCost();
            openList.Add(startNode);

            while (openList.Length > 0)
            {
                PathNode currentNode = popNodeWithLowestFCost(ref openList);
                closedList.Add(currentNode);

                if (SuccessfullyReachedTarget(currentNode.toInt2()))
                {
                    foundPath = true;
                    break;
                }

                NativeList <PathNode> eligibleNeighbours = getEligibleNeighbours(currentNode, gridSize);

                for (int i = 0; i < eligibleNeighbours.Length; i++)
                {
                    PathNode neighbourNode;

                    if (containsNode(openList, eligibleNeighbours[i]))
                    {
                        neighbourNode = getNodeInList(openList, eligibleNeighbours[i].toInt2());
                    }
                    else
                    {
                        neighbourNode = eligibleNeighbours[i];
                    }

                    if (containsNode(closedList, neighbourNode))
                    {
                        continue;
                    }

                    int2 currentPosition   = new int2(currentNode.x, currentNode.y);
                    int2 neighbourPosition = new int2(neighbourNode.x, neighbourNode.y);

                    int tentativeGCost = currentNode.gCost + CalculateDistanceCost(currentPosition, neighbourPosition);
                    if (tentativeGCost < neighbourNode.gCost)
                    {
                        neighbourNode.cameFromNodeIndex = getNodeIndex(currentNode.x, currentNode.y);
                        neighbourNode.gCost             = tentativeGCost;
                        neighbourNode.hCost             = CalculateHeuristicCost(neighbourNode.toInt2());
                        neighbourNode.UpdateFCost();

                        if (!containsNode(openList, neighbourNode))
                        {
                            openList.Add(neighbourNode);
                        }
                        else
                        {
                            updateNodeInList(ref openList, neighbourNode);
                        }
                    }
                }

                eligibleNeighbours.Dispose();
            }

            pathRoute.Clear();

            if (foundPath)
            {
                updatePathing(closedList);
            }
            else
            {
                routeFollow[entity] = new PathfindingRouteFollow {
                    routeIndex = -1
                };
            }
            openList.Dispose();
            closedList.Dispose();
        }