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

public class EndLevelScript : MonoBehaviour {

    [SerializeField] private int sceneToLoad;
    private bool doOnce = true;
    private Animator anim;
    public FadeToBlack fadeToBlack;

    private void Start()
    {
        anim = GetComponent<Animator>();
    }



    private void OnTriggerStay2D(Collider2D other)
    {
        if (other.isTrigger)
        {
            return;
        }
        if (!other.gameObject.CompareTag("Possessed"))
        {
            return;
        }
        if (doOnce)
        {
            doOnce = false;
            GetComponent<AudioSource>().Play();
            anim.SetBool("Open", true);
        }

    }

    public void EndLevel()
    {
        PlayerPrefs.SetInt("levelReached", SceneManager.GetActiveScene().buildIndex + 1);
        StartCoroutine(fadeToBlack.FadeOut(sceneToLoad));
    }
}
