Momo-Space-Diner-Code-Repo / DoorOpenerOtherDoors.cs
DoorOpenerOtherDoors.cs
Raw
using ML.SDK;
using UnityEngine;

public class DoorOpenerOtherDoors : MonoBehaviour
{
    // The axis of rotation (X or Y)
    public enum RotationAxis { X_Axis, Y_Axis }
    public RotationAxis rotationAxis = RotationAxis.Y_Axis;

    // Rotation angles for open and closed positions
    private Quaternion closedRotation;
    private Quaternion openRotation;
    public float rotationSpeed = 2f;

    // Control door state
    private bool isOpening = false;
    private bool isClosing = false;

    // Flag to track door state
    private bool isDoorOpen = false;
    public MLClickable clickableComponent;

    public void OnPlayerClickDoor(MLPlayer player)
    {
        Debug.Log("Local Button Pressed");
        this.InvokeNetwork(EVENT_ID, EventTarget.All, null, true);
    }

    public void OnNetworkOpenDoor(object[] args)
    {
        ToggleDoor();
    }

    const string EVENT_ID = "OnLeftDoorClick";
    EventToken DoorLeftToken;

    void Start()
    {
        clickableComponent.OnPlayerClick.AddListener(OnPlayerClickDoor);
        DoorLeftToken = this.AddEventHandler(EVENT_ID, OnNetworkOpenDoor);

        // Set initial rotation angles based on the selected axis
        //   if (rotationAxis == RotationAxis.X_Axis)
        //  {
        //    closedRotation = transform.localRotation;
        //    openRotation = Quaternion.Euler(closedRotation.eulerAngles + new Vector3(90f, 0f, 0f));  // Open 90 degrees along X axis
        //  }
        //  else if (rotationAxis == RotationAxis.Y_Axis)
        // {
        closedRotation = transform.localRotation;
           openRotation = Quaternion.Euler(closedRotation.eulerAngles + new Vector3(0f, 90f, 0f));  // Open 90 degrees along Y axis
       // }
    }

    void Update()
    {
        if (isOpening)
        {
            RotateDoor(openRotation);
        }
        else if (isClosing)
        {
            RotateDoor(closedRotation);
        }
    }

    // Call this function to open or close the door
    public void ToggleDoor()
    {
        if (isDoorOpen)
        {
            isClosing = true;
            isOpening = false;
        }
        else
        {
            isOpening = true;
            isClosing = false;
        }
        isDoorOpen = !isDoorOpen;  // Toggle door state
    }

    private void RotateDoor(Quaternion targetRotation)
    {
        transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, Time.deltaTime * rotationSpeed);

        // Stop rotating when close enough to the target rotation
        if (Quaternion.Angle(transform.localRotation, targetRotation) < 0.1f)
        {
            isOpening = false;
            isClosing = false;
        }
    }
}