using UnityEngine;

public class DoorAnimation : MonoBehaviour
{
    [SerializeField] private GameObject door;
    [SerializeField] private float openSpeed = 2.0f;
    [SerializeField] private float closeSpeed = 2.0f;
    private bool isOpen = false;
    private Vector3 closedPosition;
    private Vector3 openPosition;

    void Start()
    {
        closedPosition = door.transform.position;
        openPosition = new Vector3(door.transform.position.x, door.transform.position.y - 4.6f, door.transform.position.z);
    }

    public void ToggleDoor()
    {
        if (isOpen)
        {
            StartCoroutine(CloseDoor());
        }
        else
        {
            StartCoroutine(OpenDoor());
        }
    }
    private System.Collections.IEnumerator OpenDoor()
    {
        while (Vector3.Distance(door.transform.position, openPosition) > 0.01f)
        {
            door.transform.position = Vector3.MoveTowards(door.transform.position, openPosition, openSpeed * Time.deltaTime);
            yield return null;
        }
        isOpen = true;
    }

    private System.Collections.IEnumerator CloseDoor()
    {
        while (Vector3.Distance(door.transform.position, closedPosition) > 0.01f)
        {
            door.transform.position = Vector3.MoveTowards(door.transform.position, closedPosition, closeSpeed * Time.deltaTime);
            yield return null;
        }
        isOpen = false;
    }
}
