﻿
/*
 * Author:Kagami
*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[AddComponentMenu("3What/InGame/GenerateItem")]
/// <summary>
/// 道具產生控制器
/// </summary>
public class Sct_ItemControl : Sct_EdibleGenerator
{
    private int RandomItem;

    private int randomSeed = 1;

    void Awake()
    {
        //Initialize variables
        InitNumber = 1;
        _lists = new List<GameObject>();

        //載入Prefab
        Prefab = Resources.Load(Sct_DEFCONST.FILE_PATH.GAME_PREFAB_FILE_PATH + Sct_DEFCONST.GAME_PREFAB_NAME.TRAPITEM, typeof(GameObject)) as GameObject;
        Prefab2 = Resources.Load(Sct_DEFCONST.FILE_PATH.GAME_PREFAB_FILE_PATH + Sct_DEFCONST.GAME_PREFAB_NAME.PESTICIDE, typeof(GameObject)) as GameObject;
    }

	void Start ()
    {
        //取得螢幕長寬
        ScrenPoint = Sct_BorderControl.WorldPointOfScreen;

        StartCoroutine(Generate());
	}

    public override IEnumerator Generate()
    {
        //RandomItem = Random.Range(1, 2);
        if (randomSeed == 1)
        {
            RandomItem = 2;
            randomSeed = 2;
        }
        else if (randomSeed == 2)
        {
            RandomItem = 1;
            randomSeed = 1;
        }

        if (RandomItem == 1)
            yield return new WaitForSeconds(6f);
        else
            yield return new WaitForSeconds(3f);

        //不斷產生道具直到符合設定好的初始道具數量
        while (_lists.Count < InitNumber)
        {
            if (RandomItem == 1)
                _lists.Add(Sct_HelpTools.AddObjectInGamePanel(GameObject.FindWithTag("GamePanel"), Prefab, new Vector3(Random.Range(-ScrenPoint.x * 0.5f, ScrenPoint.x * 0.5f), Random.Range(-ScrenPoint.y * 0.5f, ScrenPoint.y * 0.5f), 0f)));
            else if (RandomItem == 2)
                _lists.Add(Sct_HelpTools.AddObjectInGamePanel(GameObject.FindWithTag("GamePanel"), Prefab2, new Vector3(Random.Range(-ScrenPoint.x * 0.5f, ScrenPoint.x * 0.5f), Random.Range(-ScrenPoint.y * 0.5f, ScrenPoint.y * 0.5f), 0f)));
        }
    }

    public override void RemoveItem()
    {
        Destroy(_lists[_lists.Count - 1].gameObject);
        _lists.Remove(_lists[_lists.Count - 1]);
    }

    public override void DestoryAllItem()
    {
        while (_lists.Count > 0)
        {
            Destroy(_lists[_lists.Count - 1].gameObject);
            _lists.Remove(_lists[_lists.Count - 1]);
        }
    }
}

