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


public class ObstacleManager : UnitySingleton<ObstacleManager>
{   
    [Header("各种障碍物的预制体")]
    public GameObject[] obstacles;
    [Header("出现障碍物的时间间隔")]
    public float creatObstaclesTimeInterval; 
    [Header("生成障碍物的位置，仅控制xz，y由预制体决定")]
    public Transform obstacleCreatPostion;

    private Horse horse;

    void Start()
    {
        horse = FindObjectOfType<Horse>();
    }

    public void StartCreatObstacle()
    {
        StartCoroutine(CreatAObstacle());
    }

    private IEnumerator CreatAObstacle()
    {
        float randomT = Random.Range(0f,1f);
        GameObject obstacle;
        if(randomT < 0.25f)
            obstacle = obstacles[0];
        else if(randomT < 0.5f)
            obstacle = obstacles[1];
        else if(randomT < 0.75f)
            obstacle = obstacles[2];
        else
            obstacle = obstacles[3];
        Vector3 xz = obstacleCreatPostion.position;
        Vector3 pos = new Vector3(xz.x,obstacle.transform.position.y,xz.z);
        GameObject.Instantiate(obstacle, pos, Quaternion.identity);
        yield return new WaitForSeconds(creatObstaclesTimeInterval);
        if(horse.IsHorseStillAlive()) StartCoroutine(CreatAObstacle());
    } 
}
