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

public class GeysirManager : MonoBehaviour
{
    public float timer;
    public AudioClip fireSound;
    public AudioSource source;

    private float timerCounter;
    private List<GameObject> geysirParts = new List<GameObject>();
    private IEnumerator coroutine;

    // Start is called before the first frame update
    void Start()
    {
        foreach (Transform child in transform)
        {
            geysirParts.Add(child.gameObject);
            timerCounter = timer;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (timerCounter < 0)
        {
            timerCounter = timer;
            coroutine = ShootGeysirParts();
            StartCoroutine(coroutine);
        }
        timerCounter -= Time.deltaTime;
    }

    IEnumerator ShootGeysirParts()
    {
        GeysirSound();
        foreach (GameObject part in geysirParts)
        {
            part.SetActive(true);
            yield return new WaitForSeconds(0.01f);
        }

        yield return new WaitForSeconds(1);

        foreach (GameObject part in geysirParts)
        {
            part.SetActive(false);
        }
    }

    private void GeysirSound()
    {
        source.PlayOneShot(fireSound);
    }
}
