Exemplo n.º 1
0
    public void FindPath(Transform _startPos, Transform _endPos)
    {
        if (!readyToCountPath)
        {
            return;
        }
        startPos = _startPos;
        endPos   = _endPos;
        if (_startPos == null || _endPos == null)
        {
            print("Missing start position or endposition");
            return;
        }

        //Basic raycast if can move directly to end target
        bool cantSeeTarget = Physics2D.Linecast(_startPos.transform.position, _endPos.position, Grid.instance.unwalkableMask);

        if (cantSeeTarget == false)
        {
            Vector3[] newPath = new Vector3[1];
            newPath[0] = _endPos.position;
            OnPathFound(newPath);
            StartCoroutine(PathCountDelay());
            return;
        }

        if (_endPos.position != endPosition)
        {
            endPosition      = _endPos.position;
            readyToCountPath = false;

            if (Grid.instance.useThreading)
            {
                ThreadController.SearchPathRequest(this, _startPos.position, endPosition);
            }
            else
            {
                Node      start   = Grid.instance.ClosestNodeFromWorldPoint(_startPos.position);
                Node      end     = Grid.instance.ClosestNodeFromWorldPoint(endPosition);
                Vector3[] newPath = AStar.FindPath(start, end);
                OnPathFound(newPath);
                StartCoroutine(PathCountDelay());
            }
        }
    }
Exemplo n.º 2
0
    public void FindPath(Transform _seeker, Vector2 _endPos)
    {
        if (!readyToCountPath)
        {
            return;
        }

        else if (_seeker == null)
        {
            UnityEngine.Debug.LogError("Missing seeker!", this);
            return;
        }

        startPos = _seeker;
        endPos   = _endPos;
        Grid grid = Grid.instance;

        //Basic raycast if can move directly to end target

        //bool cantSeeTarget = Physics2D.Linecast(_seeker.transform.position, _endPos, grid.unwalkableMask);
        //if (cantSeeTarget == false)
        //{
        //    Vector2[] newPath = new Vector2[1];
        //    newPath[0] = _endPos;
        //    OnPathFound(newPath);
        //    sw.Stop();
        //    print("Time took to find path: " + sw.ElapsedMilliseconds);
        //    StartCoroutine(PathCountDelay());
        //    return;
        //}

        if (_endPos != endPosition)
        {
            endPosition = _endPos;
            ThreadController.SearchPathRequest(this, _seeker.position, endPosition, grid);
        }
    }