Exemple #1
0
        protected virtual void Update()
        {
            if (_state == State.Undefined)
            {
                return;
            }

            var availableTargets = _gameManager.GetEnemies().Where(IsInRange).ToList();

            if (_target != null && (!_target.Alive || !IsInRange(_target)))
            {
                _target = null;
            }

            if (_target != null)
            {
                var direction = Quaternion.LookRotation(_target.transform.position - transform.position).eulerAngles;
                TowerTransform.rotation = Quaternion.Euler(90f, direction.y, 0f);

                var time = _gameManager.GetTime();
                if (time - _lastShot > 1f / FireRate)
                {
                    Fire(_target);
                    _lastShot = time;
                }
            }
            else
            {
                var minDistance = float.MaxValue;
                foreach (var availableTarget in availableTargets)
                {
                    var distance = (availableTarget.transform.position - transform.position).sqrMagnitude;
                    if (distance < minDistance)
                    {
                        minDistance = distance;
                        _target     = availableTarget;
                    }
                }
            }
        }