using UnityEngine;

namespace AsteroidGame
{
    public class EnemySpawner : MonoBehaviour
    {
        [Header("Parameters")]
        [SerializeField] private GameObject _enemyPrefab;
        [SerializeField] [Range(0.1f, 60f)] private float _spawnRate = 60f;
        [SerializeField] private bool _spawnEnemies;

        [Header("State")]
        [SerializeField] private float _timeToNextSpawn;

        private void Start()
        {
            _timeToNextSpawn = 0;
        }

        private void Update()
        {
            if (!_spawnEnemies) return;

            _timeToNextSpawn -= Time.deltaTime;

            if (!(_timeToNextSpawn <= 0)) return;

            var newEnemy = Instantiate(_enemyPrefab, transform);
            newEnemy.transform.parent = transform;

            _timeToNextSpawn = _spawnRate;
        }
    }
}