Пример #1
0
    private IEnumerator RecalculatePath()
    {
        if (astar == null)
        {
            goto end;
        }

        pathCalcId++;
        //pathFinder.StopCalculation();

        lastRecalculatePathTime = Time.time;

        bool  forceRecalcPath = false;
        float newTargetDiff   = Vector2.Distance(lastPathTarget, moveTarget);

        //check if player got off the path (eg. pushed out)
        bool isPlayerOffPath = false;

        if (currentPathNode?.Previous != null)
        {
            Vector2 previousToCurrentDirection = currentPathNode.point - currentPathNode.Previous.point;
            float   playerDistFromLine         = Utils.GetDistanceFromLine(
                playerPosition, currentPathNode.Previous.point, previousToCurrentDirection);
            isPlayerOffPath = playerDistFromLine > 2 * DIST_CHECK_TOLERANCE;
        }

        if (
            //path.IsValid() &&
            !forceRecalcPath && newTargetDiff < PATH_STEP / 2 && !isPlayerOffPath)
        {
            if (path != null)
            {
                Utils.DebugDrawPath(path.GetNodePositions(), Color.blue);
            }
            goto end;
        }
        else
        {
            //Debug.Log($"RecalculatePath - {pathCalcId} | {player.InitInfo.Number} | {Time.frameCount}");

            if (isLogEnabled)
            {
                Debug.Log($"RecalculatePath - {newTargetDiff}");
            }

            astar.StopCalculation();
            brain.StartCoroutine(pathFinder.GetPathAsync(playerPosition, moveTarget, astar));
            while (pathFinder.IsSearching)
            {
                yield return(new WaitForEndOfFrame());
            }

            path = pathFinder.Path;

            if (path == null || !path.IsValid())
            {
                goto end;
            }

            OnPathRecalculated();
            CheckPathProgress();
            //moveTarget = path.First(); //NO!!
            lastPathTarget = moveTarget;
            //player.Movement.Stop(); //lagging
        }

        end : UpdateMove();
    }