using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class Goal : MonoBehaviour
{
    public Timer timer;

    private GameObject[] characters;

    public string linkedScene;
    public bool playerAtGoal    = false;
    public bool companionAtGoal = false;

    void Start()
    {
        characters = GameObject.FindGameObjectsWithTag("Characters");
    }

    void Update()
    {
        if(playerAtGoal && companionAtGoal && !timer.GetWin())
        {
            for (int i = 0; i < characters.Length; i++)
                characters[i].GetComponent<BaseCharacterController>().Win();
            timer.Win();
        }
    }

    void OnTriggerStay2D(Collider2D col)
    {
        if (col.gameObject.GetComponent<CharacterUserInput>())
        {
            playerAtGoal = true;
        }
        if (col.gameObject.GetComponent<AICharacterInput>())
        {
            companionAtGoal = true;
        }
    }

    void OnTriggerExit2D(Collider2D col)
    {
        if (col.gameObject.GetComponent<CharacterUserInput>())
        {
            playerAtGoal = false;
        }
        if (col.gameObject.GetComponent<AICharacterInput>())
        {
            companionAtGoal = false;
        }
    }
}
    
