using UnityEngine;
using System.Collections;

public class EnemySpawner : MonoBehaviour
{
    public GameObject enemyPrefab;
    public Vector2 spawnAreaMin;
    public Vector2 spawnAreaMax;
    public float spawnInterval = 2.0f;

    void Start()
    {
        // Start the coroutine to spawn enemies at regular intervals
        // KISS: Simple call to start the coroutine, making the code easy to understand
        StartCoroutine(SpawnEnemies());
    }

    IEnumerator SpawnEnemies()
    {
        while (true)
        {
            // Wait for the specified interval
            // KISS: Using WaitForSeconds keeps the waiting logic simple
            yield return new WaitForSeconds(spawnInterval);

            // Get a random spawn position
            // DRY: Reuse the method to get a random spawn position
            Vector2 spawnPosition = GetRandomSpawnPosition();

            // Spawn the enemy
            // KISS: Simple instantiation call
            Instantiate(enemyPrefab, spawnPosition, Quaternion.identity);

            // Log the spawn position for debugging
            Debug.Log("Spawned enemy at position: " + spawnPosition);
        }
    }

    Vector2 GetRandomSpawnPosition()
    {
        // Calculate and return a random position within the defined area
        // KISS: Simple calculation for random position
        float x = Random.Range(spawnAreaMin.x, spawnAreaMax.x);
        float y = Random.Range(spawnAreaMin.y, spawnAreaMax.y);
        
        // DRY: This method encapsulates the random position calculation, avoiding repetition
        return new Vector2(x, y);
    }
}