Ejemplo n.º 1
0
    public int GetNextNodeNearestToTarget()
    {
        var myDistances        = new List <float>();
        var targetDistances    = new List <float>();
        var nodeDistancesToEnd = new List <float>();

        var myClosestIndex = 0;
        var myClosestDist  = 99999f;
        var myDistToEnd    = 0.0f;

        var targetClosestIndex = 0;
        var targetClosestDist  = 99999f;

        for (int i = 0; i < _currentPath.Count; i++)
        {
            myDistances.Add(((Vector2)_currentPath[i].position - (Vector2)this.transform.position).magnitude);
            //nodeDistancesToEnd.Add(GetDistFromNodeToEndOfPath(i));
            //targetDistances.Add(((Vector2)_currentPath[i].position - (Vector2)_currentPath[_currentPath.Count-1].position).magnitude);
        }

        for (int i = 0; i < myDistances.Count; i++)
        {
            if (myDistances[i] < myClosestDist)
            {
                myClosestDist  = myDistances[i];
                myClosestIndex = i;
            }
        }

        //Could find a nice way to find the next node via distance, but just
        //	get the closest node and start going to the next one
        if (myClosestIndex + 1 < _currentPath.Count)
        {
            myClosestIndex++;
        }

        if (myClosestIndex < 0 || !_pathfinder.IsPointWithinPlayableArea(_currentPath[myClosestIndex].position))
        {
            myClosestIndex = 0;
        }

        return(Math.Max(0, myClosestIndex));
        //myDistToEnd = GetDistFromNodeToEndOfPath(myClosestIndex);
    }