Files
2026-02-16 17:41:09 +00:00

94 lines
3.0 KiB
C#

using UnityEngine;
using System.Collections;
public class Door : MonoBehaviour, IInteract
{
[SerializeField] bool isLocked;
[SerializeField] string requiredKeyTag;
[SerializeField] GameObject door;
[SerializeField] float rotationAngle = 90f;
[SerializeField] bool requiresReview = true;
bool isOpen = false;
public void OnInteract()
{
if (isLocked)
{
StartCoroutine(LockedDoorShake()); // Shake the door to indicate it's locked
}
else
{
if (isOpen)
{
StartCoroutine(ClosingDoor()); // Close the door by rotating it back
isOpen = false;
}
else
{
StartCoroutine(OpeningDoor()); // Simple door opening by rotating it 90 degrees on the Y-axis
isOpen = true;
}
}
}
IEnumerator OpeningDoor()
{
float elapsedTime = 0f;
float duration = 1f; // Duration of the door opening animation
Quaternion initialRotation = door.transform.rotation;
Quaternion targetRotation = initialRotation * Quaternion.Euler(0, -rotationAngle, 0);
while (elapsedTime < duration)
{
door.transform.rotation = Quaternion.Slerp(initialRotation, targetRotation, elapsedTime / duration);
elapsedTime += Time.deltaTime;
yield return null;
}
door.transform.rotation = targetRotation; // Ensure it ends at the exact target rotation
ReviewObject();
}
private void ReviewObject()
{
// Notify LevelManager for review after animation completes
if (requiresReview && LevelManager.Instance != null)
{
LevelManager.Instance.RequestReview(gameObject);
}
}
IEnumerator ClosingDoor()
{
float elapsedTime = 0f;
float duration = 1f; // Duration of the door closing animation
Quaternion initialRotation = door.transform.rotation;
Quaternion targetRotation = initialRotation * Quaternion.Euler(0, rotationAngle, 0);
while (elapsedTime < duration)
{
door.transform.rotation = Quaternion.Slerp(initialRotation, targetRotation, elapsedTime / duration);
elapsedTime += Time.deltaTime;
yield return null;
}
door.transform.rotation = targetRotation; // Ensure it ends at the exact target rotation
ReviewObject();
}
IEnumerator LockedDoorShake()
{
float elapsedTime = 0f;
float duration = 0.5f; // Duration of the shake
Quaternion originalRotation = door.transform.rotation;
while (elapsedTime < duration)
{
float yOffset = Random.Range(-1f, 1f);
door.transform.rotation = originalRotation * Quaternion.Euler(0, yOffset, 0);
elapsedTime += Time.deltaTime;
yield return null;
}
door.transform.rotation = originalRotation; // Reset to original rotation after shaking
ReviewObject();
}
}