using ML.SDK;
using UnityEngine;
public class DoorOpenerOvens : 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 OnClickDoor(MLPlayer player)
{
Debug.Log("Local Button Pressed");
this.InvokeNetwork(EVENT_ID, EventTarget.All, null, true);
}
public void OnNetworkOpenOvenDoor(object[] args)
{
ToggleDoor();
}
const string EVENT_ID = "OvenClickEvent";
EventToken Oventoken;
void Start()
{
clickableComponent.OnPlayerClick.AddListener(OnClickDoor);
// 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
Oventoken = this.AddEventHandler(EVENT_ID, OnNetworkOpenOvenDoor);
// }
// 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;
}
}
}