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

public class LightFogManager : MonoBehaviour
{
    const int cMaxTargetGameObjects = 6;

    private Material m_lightFogMaterial;
    private List<GameObject> m_targetGameObjects;


    void Awake()
    {
        m_targetGameObjects = new List<GameObject>();
        m_lightFogMaterial = GetComponent<SpriteRenderer>().material;
    }

    void Update()
    {
        UpdateMaterialInfo();
    }
	
	public void AddLightTarget(GameObject aGameObject)
    {
        if(m_targetGameObjects.Count < cMaxTargetGameObjects)
        {
            m_targetGameObjects.Add(aGameObject);
        }
        else
        {
            Debug.LogError("[LightFogManager] Tried to add too many light targets");
        }
    }

    public void RemoveLightTarget(GameObject aGameObject)
    {
        for(int i = 0; i < cMaxTargetGameObjects; i++)
        {
            if(m_targetGameObjects[i] == aGameObject)
            {
                RemoveLightTarget(i);
                return;
            }
        }
    }

    public void RemoveLightTarget(int aIndex)
    {
        GameObject removedObject = m_targetGameObjects[aIndex];
        m_targetGameObjects.RemoveAt(aIndex);
    }

    private void UpdateMaterialInfo()
    {
        Vector4[] targetGameObjectsArray = new Vector4[cMaxTargetGameObjects];
        for (int i = 0; i < m_targetGameObjects.Count; i++)
        {
            targetGameObjectsArray[i] = m_targetGameObjects[i].transform.position;
        }
        m_lightFogMaterial.SetVectorArray("_LightPositions", targetGameObjectsArray);
        m_lightFogMaterial.SetInt("_NumLightPositions", m_targetGameObjects.Count);
    }

}
