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

/* Summary:
 * 
 * Maintains a list of objects that are in the trigger volume.
 * 
 * */
 
public class MobVolumeTargets : MonoBehaviour {

    [SerializeField]
    private List<GameObject> m_attackList = new List<GameObject>();

    public List<GameObject> TargetList
    {
        get
        {
            return m_attackList;
        }
    }

    private void FixedUpdate()
    {
        // Purge null refs from list
        for (int i = 0; i < m_attackList.Count; i++)
        {
            if (m_attackList[i] == null)
            {
                m_attackList.RemoveAt(i);
                i--;
            }
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        // Put all enemies into a list
        if (other.gameObject.GetComponent<PlayerHealth>() != null)
        {
            if(m_attackList.Contains(other.gameObject) == false)
            {
                m_attackList.Add(other.gameObject);

                // Debug.Log(m_attackList.Count + " enemies");
            }
        }
    }

    private void OnTriggerExit(Collider other)
    {
        // Remove from list...
        if (other.gameObject.GetComponent<PlayerHealth>() != null)
        {
            m_attackList.Remove(other.gameObject);
        }
    }

    public void ResetList()
    {
        m_attackList.Clear();
    }

    void OnDisable()
    {
        m_attackList.Clear();
    }
}
