private void FixedUpdate()
 {
     _src.Accelerate(_bufferedMovementDirection);
 }
Ejemplo n.º 2
0
    public void Update()
    {
        if (_targetLizard != null)
        {
            _facing   = (_targetLizard.transform.position - _src.transform.position).normalized;
            _facing.y = _src.transform.position.y;
            _src.Orientate(_facing);
        }
        else
        {
            _src.Orientate(Vector3.forward);
        }

        //If the target it outside the chase radius then the timer will increase
        if (_targetDistance > chaseRadius)
        {
            _searchTimer += Time.deltaTime;
        }

        if (_targetLizard == null)
        {
            _currentState = State.CHOOSETARGET;
        }

        //State machine for the AI
        switch (_currentState)
        {
        case State.IDLE:
            _src.Stop();

            _targetLizard = FindRandomLizard();

            if (_targetLizard != null)
            {
                _currentState = State.MOVINGTOTARGET;
            }
            break;

        //Moving towards the AI target
        case State.MOVINGTOTARGET:
            Vector3 direction = (_targetLizard.transform.position - this.transform.position).normalized;
            _src.Accelerate(direction);
            //If the timer exceeds 4 seconds the AI will change target
            if (_searchTimer > 4.0f)
            {
                _currentState = State.CHOOSETARGET;
            }
            //When a lizard passes by close enough 60% of the time it will become the new target
            foreach (Lizard currentLizard in _gameManager.completeList)
            {
                if (_src != currentLizard && currentLizard != _targetLizard)
                {
                    if (Vector3.Distance(this.transform.position, currentLizard.transform.position) < chaseRadius * 0.5f)
                    {
                        int randomNumber = Random.Range(1, 10);
                        if (randomNumber <= 4)
                        {
                            _targetLizard = currentLizard;
                            _searchTimer  = 0.0f;
                        }
                    }
                }
            }
            _targetDistance = Vector3.Distance(this.transform.position, _targetLizard.transform.position);
            if (_targetDistance < attackRange)
            {
                if (Time.time > _attackTime + attackPause)
                {
                    _currentState = State.ATTACKING;
                }
            }
            break;

        //Choosing a random target for the AI
        case State.CHOOSETARGET:
            _targetLizard = FindRandomLizard();
            _searchTimer  = 0.0f;

            if (_targetLizard == null)
            {
                _currentState = State.IDLE;
            }
            else
            {
                _currentState = State.MOVINGTOTARGET;
            }
            break;

        case State.ATTACKING:
            _attackTime = Time.time;
            _targetLizard.Knockback(_facing, _targetedKnockbackData);
            _currentState = State.WANDER;
            wanderPoint   = Random.insideUnitSphere + _gameManager.circleCentre.position;
            wanderPoint.y = 0;
            _wanderTime   = Time.time;
            break;

        case State.WANDER:
            Vector3 wanderDirection = (wanderPoint - this.transform.position).normalized;
            _src.Accelerate(wanderDirection);
            _facing = wanderDirection;
            _src.Orientate(_facing);
            if (Time.time > _wanderTime + wanderDuration || Vector3.Distance(this.transform.position, wanderPoint) < 0.25)
            {
                _currentState = State.CHOOSETARGET;
            }
            break;

        default:
            break;
        }
    }