Пример #1
0
    /// <summary>
    /// Path is calculated asynchronously. If it takes too long it is possible that
    /// player as already located somewhere else than the start of the path.
    /// Check first X nodes if player is between some of them and mark them as visited.
    /// </summary>
    private void OnPathRecalculated()
    {
        if (path == null)
        {
            currentPathNode = null;
            return;
        }

        int maxCheckCount = Mathf.Min(3, path.nodes.Count);

        for (int i = 0; i < maxCheckCount; i++)
        {
            PathNode node                     = path.nodes[i];
            bool     isCloseToNode            = IsCloseTo(node.point);
            bool     isBetweenThisAndNextNode = node.Next != null &&
                                                Utils.GetDistanceFromLine(playerPosition, node.point, node.Next.point) < DIST_CHECK_TOLERANCE;

            if (isCloseToNode || isBetweenThisAndNextNode)
            {
                for (int j = i; j >= 0; j--)
                {
                    path.nodes[j].visited = true;
                    Utils.DebugDrawCross(path.nodes[j].point, Color.red, 1);
                }
                break;
            }
        }

        currentPathNode = path.GetFirstUnvisitedNode();
    }