using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using StarterAssets;
public class EnemySpawner : MonoBehaviour
{
    [SerializeField] public GameObject enemyPrefab;
    [SerializeField] private Transform player;
    [SerializeField] private float enemyInterval = 3.5f;
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(spawnEnemy(enemyInterval, enemyPrefab));
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private IEnumerator spawnEnemy(float interval, GameObject enemy)
    {

        yield return new WaitForSeconds(interval);
        if (GameObject.FindGameObjectsWithTag("Enemy").Length < 10)
        {
            
             GameObject newEnemy = Instantiate(enemy, new Vector3(Random.Range(-5f, 5), Random.Range(-6f, 6f), 1), Quaternion.identity);
            
            newEnemy.GetComponent<EnemyAi>().target = player;
        }
        StartCoroutine(spawnEnemy(interval, enemy));

    }
}