Exemplo n.º 1
0
    IEnumerator AICoroutine()
    {
        yield return(new WaitForSeconds(1));

        while (true)
        {
            if (VisibilityHelper.CheckVisibility(transform, _playerGameObject.transform, MaxVisibleDistance, _layerMask))
            {
                // player visible
                if (_toTargetObjectRotation != null && !_toTargetObjectRotation.enabled)
                {
                    // point to player
                    _toTargetObjectRotation.Target  = _playerGameObject.transform;
                    _toTargetObjectRotation.enabled = true;
                }
                if (_toMoveDirectionRotation != null && _toMoveDirectionRotation.enabled)
                {
                    _toMoveDirectionRotation.enabled = false;
                }

                // shoot if able
                if (!_isStaggered && Stats.HasEnough(StatsEnum.Bullets, 1) && !_gunCooldown)
                {
                    var q = Quaternion.LookRotation(Vector3.forward,
                                                    _playerGameObject.transform.position - transform.position);
                    FireBullet(q.eulerAngles.z);
                    Stats.AddAmount(StatsEnum.Bullets, -1);
                    StartCoroutine(WeaponCooldown(2));

                    yield return(null);
                }
                else
                {
                    // go to proper distance
                    var distance = (transform.position - _playerGameObject.transform.position).magnitude;
                    if (distance < TooCloseDistance && !_isAttacking)
                    {
                        // move back when close
                        var vector = -_playerGameObject.transform.position + transform.position;
                        MoveBackWards(vector);
                    }
                    else if (distance <= AttackDistance && !_isAttacking)
                    {
                        // attack when in attack range
                        Attack();
                    }
                    else
                    {
                        // move to target
                        var vector = _playerGameObject.transform.position - transform.position;
                        Move(vector);
                    }
                    yield return(null);
                }
            }
            else
            {
                if (_toTargetObjectRotation != null && _toTargetObjectRotation.enabled)
                {
                    _toTargetObjectRotation.enabled = false;
                }
                if (_toMoveDirectionRotation != null && !_toMoveDirectionRotation.enabled)
                {
                    _toMoveDirectionRotation.enabled = true;
                }

                // wander randomly
                float angle  = Random.Range(0, 16) * (float)Math.PI * 2f / 16;
                var   vector = Math2.AngleRadToVector(angle);
                Move(vector);

                yield return(new WaitForSeconds(Random.Range(1, 3)));
            }
        }
    }