using UnityEngine;

public class DoorController : MonoBehaviour
{
    public ButtonController button;

    private SpriteRenderer top;
    private SpriteRenderer bottom;

    private Vector3 iTopPos;
    private Vector3 iBottomPos;

    private Collider2D iTopCollider;
    private Collider2D iBottomCollider;

    void Start()
    {
        this.top = GetObject(0);
        this.bottom = GetObject(1);

        this.iTopPos = this.top.transform.position;
        this.iBottomPos = this.bottom.transform.position;

        this.iTopCollider = this.top.GetComponent<Collider2D>();
        this.iBottomCollider = this.bottom.GetComponent<Collider2D>();
    }

    void Update()
    {
        bool open = this.button.isActive();

        if (open)
        {
            this.top.transform.position = this.iTopPos - new Vector3(0, -1.1f);
            this.bottom.transform.position = this.iBottomPos - new Vector3(0, 1.1f);

            this.iTopCollider.enabled = false;
            this.iBottomCollider.enabled = false;
        }
        else
        {
            this.top.transform.position = this.iTopPos;
            this.bottom.transform.position = this.iBottomPos;

            this.iTopCollider.enabled = true;
            this.iBottomCollider.enabled = true;
        }
    }

    private SpriteRenderer GetObject(int index)
    {
        return GetComponentsInChildren<SpriteRenderer>()[index];
    }
}
