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

public class DoorsScript : MonoBehaviour
{

    public Sprite sprite1; 
    public Sprite sprite2; 

    private SpriteRenderer spriteRenderer;
    [SerializeField] private Collider2D m_doorCollider;
   
    void Start()
    {
        spriteRenderer = GetComponent<SpriteRenderer>(); 
        if (spriteRenderer.sprite == null) 
            spriteRenderer.sprite = sprite1;
    }

  
    void FixedUpdate()
    {

        bool IsCloseEnough = CheckCloseToTag("Player", 7);

        if (IsCloseEnough)
        {
            m_doorCollider.enabled = false;
            spriteRenderer.sprite = sprite2;
        }
        else
        {
            m_doorCollider.enabled = true;
            spriteRenderer.sprite = sprite1;
        }

    }
    bool CheckCloseToTag(string tag, float minimumDistance)
    {
        GameObject[] goWithTag = GameObject.FindGameObjectsWithTag(tag);

        for (int i = 0; i < goWithTag.Length; ++i)
        {
            if (Vector3.Distance(transform.position, goWithTag[i].transform.position) <= minimumDistance)
                return true;
        }

        return false;

    }
}
