using UnityEngine;

public class ExitButton : MonoBehaviour 
{
    public Sprite ActivatedSprite;

    private SpriteRenderer rend;
    private Exit exit;
    void Start()
    {
        rend = GetComponent<SpriteRenderer>();
        exit = transform.parent.GetComponent<Exit>();
    }

    void OnCollisionEnter2D(Collision2D c)
    {
        if (c.transform.GetComponent<Human>() == null)
            return;

        Vector3 exitPos = transform.parent.position;
        bool isInExit = (c.transform.position - exitPos).magnitude < (transform.position - exitPos).magnitude;
        if (!isInExit)
            return;

        rend.sprite = ActivatedSprite;
        exit.Activate();
    }
}
