Ejemplo n.º 1
0
    private void ProcessTaskAttackTarget()
    {
        _moverRef.Direction = Vector2.zero;

        if (_taskTarget == null)
        {
            _taskTarget         = FindSuitableTargetToAttack();
            _moverRef.Direction = Vector2.zero;

            if (_taskTarget == null)
            {
                _targetFindAttempts++;

                if (_targetFindAttempts >= 4)
                {
                    CurrentTask         = AITask.Wander;
                    _targetFindAttempts = 0;
                }

                return;
            }

            _targetFindAttempts = 0;
        }

        //Retarget if our target has begun being infected, and not by me
        if (!_waitingOnPathResult && _statsRef.BeingInfectedBy != null && !_statsRef.Infected)
        {
            if (_statsRef.BeingInfectedBy != this.gameObject)
            {
                ClearTarget();
                return;
            }
        }

        if (_taskTarget.GetComponent <KillActor>())
        {
            ClearTarget();
            return;
        }

        //TODO Maybe retarget if we see something else is right next to us

        //Retarget if the target is now infected
        if (!_waitingOnPathResult && _statsRef.Infected && _taskTarget.GetComponent <ActorStats>().Infected)
        {
            ClearTarget();
            return;
        }

        _timeSinceLastAttackAction += Time.deltaTime;
        if (_timeSinceLastAttackAction >= 7.5f)
        {
            if (((Vector2)_taskTarget.transform.position - (Vector2)this.transform.position).magnitude > _statsRef.AttackRange * 2.0f)
            {
                ClearTarget();
                return;
            }
        }

        Vector2 customMovetoPoint = Vector2.zero;

        if (_statsRef.UsesAoeAttack)
        {
            if (_actionRef.CanAttack)
            {
                _actionRef.DoAction(ActionManager.ActionType.Attack);
                _actionRef.TargetLocationForAction = this.transform.position;
                _timeSinceLastAttackAction         = 0.0f;
            }
        }
        //If we're close enough, attack the target
        else if (((Vector2)_taskTarget.transform.position - (Vector2)this.transform.position).magnitude < _statsRef.AttackRange)
        {
            if (_actionRef.CanAttack)
            {
                _actionRef.DoAction(ActionManager.ActionType.Attack);
                _actionRef.TargetLocationForAction = _taskTarget.transform.position;
                _timeSinceLastAttackAction         = 0.0f;
            }
            //TODO maybe reintroduce this - atm enemies can just sit inside their target
            //customMovetoPoint = _taskTarget.transform.position;
        }

        if (_currentPath == null || _currentPath.Count == 0)
        {
            _moverRef.Direction = Vector2.zero;
            if (!_waitingOnPathResult)
            {
                if (_pathfinder.RequestPathfind(this.transform.position, _taskTarget.transform.position, this, cbPathResult))
                {
                    _originalTargetPlace = _taskTarget.transform.position;
                    _waitingOnPathResult = true;
                }
            }
        }
        //We've done our current one
        else if (_currentNode >= _currentPath.Count)
        {
            _currentPath.Clear();
            _currentNode = 0;
        }
        //Recalculate if the target has moved
        else if (((Vector2)_taskTarget.transform.position - _originalTargetPlace).magnitude >= REEVALUATE_DIST_FOR_TARGET_HAVING_MOVED)
        {
            if (!_waitingOnPathResult)
            {
                if (_pathfinder.RequestPathfind(this.transform.position, _taskTarget.transform.position, this, cbPathResult))
                {
                    _originalTargetPlace = _taskTarget.transform.position;
                    _waitingOnPathResult = true;
                }
            }
        }
        else
        {
            //Otherwise travel in the direction of the next node
            var direction = (Vector2)_currentPath[_currentNode].position - (Vector2)transform.position;
            var orthDir   = new Vector3(direction.x, direction.y, 0);

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

            _moverRef.Direction = orthDir;

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

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