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

public class GeneratorStateTwoD : FiniteState
{
    Generator self;
    Coroutine spawnCor;
    GameObject stuffRes;
    List<GameObject> stuffs = new List<GameObject>();
    //int index;
    float lastSpawnTime;
    public override void Init()
    {
        self = subject as Generator;
        spawnCor = self.StartCoroutine(Spawn());
        stuffRes = Resources.Load<GameObject>("Stuff_2");
    }
    void Generate(float yAxis)
    {
        GameObject stuff = GameObject.Instantiate<GameObject>(stuffRes);
        stuff.transform.position = new Vector3(self.player.position.x + 15, yAxis, 0);
    }
    IEnumerator Spawn()
    {
        while (true)
        {
            //if (index == self.stuffSpawn2D.Length)
            //    yield break;
            //yield return new WaitForSeconds(self.stuffSpawn2D[index].time - lastSpawnTime);
            //lastSpawnTime = self.stuffSpawn2D[index].time;
            //Generate(self.stuffSpawn2D[index].yAxis);
            //index++;
            yield return new WaitForSeconds(Random.Range(0.1f, 1.8f));
            Generate(Random.Range(-4, 4));
            for (int i = 0; i < stuffs.Count; i++)
            {
                if (stuffs[i])
                {
                    break;
                }
                else
                {
                    stuffs.RemoveAt(i);
                    i--;
                }
            }
        }
    }
    public override void Exit()
    {
        self.StopCoroutine(spawnCor);
        for (int i = 0; i < stuffs.Count; i++)
        {
            GameObject stuff = stuffs[i];
            if (!stuff)
                continue;
            stuff.GetComponent<Collider>().enabled = false;
        }
    }
}
