using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemySpawner : MonoBehaviour
{
    public float EnemyLine = 20.0f;
    public float EnemyLineWidth = 10.0f;
    public int MinEnemiesInWaveCount = 3;
    public int MaxEnemiesInWaveCount = 10;
    public int MaxEnemies = 30;
    public int WavePeriod = 5;
    public GameObject[] EnemyPrefabs;
    public GameObject Player;

    private Queue<GameObject> enemies = new Queue<GameObject>();

    public void FixedUpdate()
    {
        if (Time.fixedTime % WavePeriod > 0.001f || EndCondition.IsGameOver)
        {
            return;
        }

        var enemyCount = Random.Range(MinEnemiesInWaveCount, MaxEnemiesInWaveCount);
        for (int i = 0; i < enemyCount; ++i)
        {
            var enemyPrefab = EnemyPrefabs[Random.Range(0, EnemyPrefabs.Length - 1)];
            var x = Random.Range(-EnemyLineWidth, EnemyLineWidth);
            var z = EnemyLine + i;
            var enemy = Instantiate(enemyPrefab, new Vector3(x, 0, z), Quaternion.AngleAxis(180, Vector3.up));
            enemy.GetComponent<DumbEnemy>().Player = Player;
            enemies.Enqueue(enemy);
        }

        for (int i = MaxEnemies; i < enemies.Count; ++i)
        {
            var enemy = enemies.Dequeue();
            Destroy(enemy);
        }
    }
}
