Esempio n. 1
0
    /** Called when a requested path has finished calculation.
     * A path is first requested by #SearchPath, it is then calculated, probably in the same or the next frame.
     * Finally it is returned to the seeker which forwards it to this function.\n
     */
    public virtual void OnPathComplete(Path _p)
    {
        ABPath p = _p as ABPath;

        if (p == null)
        {
            throw new System.Exception("This function only handles ABPaths, do not use special path types");
        }

        canSearchAgain = true;

        //Claim the new path
        p.Claim(this);

        // Path couldn't be calculated of some reason.
        // More info in p.errorLog (debug string)
        if (p.error)
        {
            p.Release(this);
            return;
        }

        //Release the previous path
        if (path != null)
        {
            path.Release(this);
        }

        //Replace the old path
        path = p;

        //Reset some variables
        currentWaypointIndex = 0;
        targetReached        = false;
        lastTargetPos        = lastPoses[0];
        lastChoosenTarget    = GameManager.Instance.GetChooseTheBuildingIdxByPos(lastPoses [0]);
        //The next row can be used to find out if the path could be found or not
        //If it couldn't (error == true), then a message has probably been logged to the console
        //however it can also be got using p.errorLog
        //if (p.error)

        if (closestOnPathCheck)
        {
            Vector3 p1   = Time.time - lastFoundWaypointTime < 0.3f ? lastFoundWaypointPosition : p.originalStartPoint;
            Vector3 p2   = GetFeetPosition();
            Vector3 dir  = p2 - p1;
            float   magn = dir.magnitude;
            dir /= magn;
            int steps = (int)(magn / pickNextWaypointDist);

#if ASTARDEBUG
            Debug.DrawLine(p1, p2, Color.red, 1);
#endif

            for (int i = 0; i <= steps; i++)
            {
                CalculateVelocity(p1);
                p1 += dir;
            }
        }
        if (parent != null)
        {
            parent.OnPathComplete();
        }
    }