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

public class GaneratorStateOneD : FiniteState
{
    Generator self;
    Coroutine spawnCor;
    GameObject stuffRes;
    float lastSpawnTime;
    List<GameObject> stuffs = new List<GameObject>();
    int index;
    public override void Init()
    {
        self = subject as Generator;
        spawnCor = self.StartCoroutine(Spawn());
        stuffRes = Resources.Load<GameObject>("Stuff_1");
    }
    void Generate()
    {
        GameObject stuff = GameObject.Instantiate<GameObject>(stuffRes);
        stuff.transform.position = new Vector3(self.player.position.x + 18, 0, 0);
        stuffs.Add(stuff);
    }
    IEnumerator Spawn()
    {
        while (true)
        {
            if (index == self.stuffSpawnTime.Length)
                break;
            yield return new WaitForSeconds(self.stuffSpawnTime[index] - lastSpawnTime);
            lastSpawnTime = self.stuffSpawnTime[index];
            index++;
            Generate();
        }
    }
    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;
        }
    }
}
