Ejemplo n.º 1
0
    private void ProcessTaskWander()
    {
        //Random wait at path end
        if (_endOfPathWaitTime > 0.0f)
        {
            _endOfPathWaitTime -= Time.deltaTime;
            _moverRef.Direction = Vector2.zero;
            return;
        }

        //Find a new path
        if (_currentPath == null || _currentPath.Count == 0)
        {
            if (!_waitingOnPathResult)
            {
                _waitingOnPathResult = _pathfinder.RequestPathfind(this.transform.position, _pathfinder.GetRandomPointInBounds(CurrentTask != AITask.Wander), this, cbPathResult);
                _moverRef.Direction  = Vector2.zero;
            }
        }
        //We've done our current one
        else if (_currentNode >= _currentPath.Count)
        {
            _currentPath.Clear();
            _currentNode        = 0;
            _moverRef.Direction = Vector2.zero;

            if (CurrentTask == AITask.Wander)
            {
                _endOfPathWaitTime = Random.Range(0.5f, 3.0f);
            }
        }
        else
        {
            //Otherwise travel in the direction of the next node
            var direction = _currentPath[_currentNode].position - transform.position;

            var normal = transform.forward;
            Vector3.OrthoNormalize(ref normal, ref direction);

            _moverRef.Direction = direction;

            if ((transform.position - _currentPath[_currentNode].position).magnitude <= REACHED_NODE_DISTANCE)
            {
                _currentNode++;

                if (_currentNode >= _currentPath.Count)
                {
                    _moverRef.Direction = Vector2.zero;
                }
            }
        }
    }