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

public class CoinSpawner : MonoBehaviour
{
    public GameObject m_Coin;                // The coin prefab to be spawned.
    public float m_SpawnTime = 3f;            // How long between each spawn.
    public Transform[] m_SpawnPoints;         // An array of the spawn points this coin can spawn from.
    public float DeathCoinSpread;

    void Start()
    {
        // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
        InvokeRepeating("Spawn", m_SpawnTime, m_SpawnTime);
    }

    public void SpillCoins(Vector3 sourcePosition, int coinCount)
    {
        for(int i = 0; i < coinCount; i++)
        {
            float offsetValue = Random.Range(0, DeathCoinSpread);
            Vector3 offsetPos = new Vector3(sourcePosition.x + offsetValue, sourcePosition.y + offsetValue, sourcePosition.z);
            Instantiate(m_Coin, offsetPos, Quaternion.identity);
        }
    }

    void Spawn()
    {
        //is there a conditionn where you don't spawn coins? put that here..

        // Find a random index between zero and one less than the number of spawn points.
        int spawnPointIndex = Random.Range(0, m_SpawnPoints.Length);

        // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
        Instantiate(m_Coin, m_SpawnPoints[spawnPointIndex].position, m_SpawnPoints[spawnPointIndex].rotation);
    }
}
