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

public class GeneratorStateThreeD : FiniteState
{
    Generator self;
    Coroutine spawnCor;
    GameObject stuffRes;
    List<GameObject> stuffs = new List<GameObject>();
    public override void Init()
    {
        self = subject as Generator;
        spawnCor = self.StartCoroutine(Spawn());
        stuffRes = Resources.Load<GameObject>("Stuff_3");
    }
    void Generate()
    {
        GameObject stuff = GameObject.Instantiate<GameObject>(stuffRes);
        stuff.transform.position = new Vector3(self.player.position.x + 80, Random.Range(-4, 4), Random.Range(-4, 4));
    }
    IEnumerator Spawn()
    {
        while (true)
        {
            yield return new WaitForSeconds(Random.Range(.3f, 2));
            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;
        }
    }
}
