Files
HauntedBloodlines/Assets/Scripts/Draggable/DraggableDrawerDoor.cs
2025-05-29 22:31:40 +03:00

300 lines
11 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
namespace InfallibleCode
{
public class DraggableDrawerDoor : MonoBehaviour, /*IDraggable*/ IInteractable
{
public Transform door;
// public Rigidbody doorRb;
public bool DrawerDoor_Right;
public bool DoorRequiresKey;
public bool DoorIsOpen;
public float RotationSpeed = 2;
[Range(90, -90)]
public float DoorStartAngle = 0f;
private Vector3 _OpenRot;
private Vector3 _CloseRot;
[HideInInspector] public bool CanOpenDoor = true;
private bool drawerDoorIsOpeningOrClosing;
float drawerLerp;
//[SerializeField] public Transform _player;
public AudioSource drawerDoorAudioSource;
public AudioMixer audioMixer;
//public AudioClip drawerDoorOpenSoundEffect;
//public AudioClip drawerDoorCloseSoundEffect;
private bool hasPlayedDrawerCloseSound;
private void Awake()
{
if (gameObject.GetComponent<AudioSource>() == null)
{
drawerDoorAudioSource = gameObject.AddComponent(typeof(AudioSource)) as AudioSource;
// Assign the Audio Mixer to the AudioSource
if (audioMixer != null)
{
drawerDoorAudioSource.outputAudioMixerGroup = audioMixer.FindMatchingGroups("SFX")[0];
}
}
//drawerDoorOpenSoundEffect = Resources.Load<AudioClip>("DrawerSounds/DrawerDoorOpening");
//drawerDoorCloseSoundEffect = Resources.Load("DrawerSounds/DrawerDoorClose") as AudioClip;
}
void Start()
{
if (DoorRequiresKey)
{
CanOpenDoor = false;
}
door.transform.localRotation = Quaternion.Euler(door.localEulerAngles.x, DoorStartAngle, door.localEulerAngles.z);
if (DrawerDoor_Right)
{
_OpenRot = new Vector3(door.localEulerAngles.x, -90f, door.localEulerAngles.z);
}
else
{
_OpenRot = new Vector3(door.localEulerAngles.x, 90f, door.localEulerAngles.z);
}
_CloseRot = new Vector3(door.localEulerAngles.x, 0f, door.localEulerAngles.z);
if (DoorStartAngle >= 0f)
{
DoorIsOpen = false;
}
if (DoorStartAngle <= -90f)
{
DoorIsOpen = true;
}
if (drawerDoorAudioSource == null)
{
drawerDoorAudioSource = GetComponent<AudioSource>();
}
drawerDoorAudioSource.loop = false;
drawerDoorAudioSource.spatialBlend = 1f;
drawerDoorAudioSource.maxDistance = 100f;
}
public void UnlockDrawer()
{
CanOpenDoor = true;
}
public void LockDrawer()
{
CanOpenDoor = false;
}
public void DragDoor()
{
#region Door Drag
if (CanOpenDoor)
{
DraggablesManager.GetInstance().IsDragging = true;
float rotX = Input.GetAxis("Mouse X") * RotationSpeed * Mathf.Deg2Rad;
#region fml
//if (Mathf.Approximately(door.rotation.z, _player.rotation.z))
//{
// door.RotateAroundLocal(Vector3.up, -rotX);
// print("I work normmaly");
//}
//else
//{
// door.RotateAroundLocal(Vector3.down, -rotXReverse);
// print("I work gay");
//}
#endregion
door.RotateAroundLocal(Vector3.up, -rotX);
#region Left Drawer
if (!DrawerDoor_Right)
{
float DoorAngle = door.transform.localEulerAngles.y;
DoorAngle = (DoorAngle > 180) ? DoorAngle - 360 : DoorAngle;
if (DoorAngle >= 90f)
{
print("Door Rotation.Y is 90f");
door.localEulerAngles = new Vector3(0f, 90f, 0f);
DoorIsOpen = true;
}
if (DoorAngle > 0.01f)
{
print("Door Rotation.Y is -90f");
DoorIsOpen = true;
}
if (door.localRotation.y <= 0f)
{
print("Door Rotation.Y is 0f");
door.localEulerAngles = new Vector3(0f, 0f, 0f);
DoorIsOpen = false;
}
}
#endregion
#region Right Drawer
else if (DrawerDoor_Right)
{
float DoorAngle = door.transform.localEulerAngles.y;
DoorAngle = (DoorAngle > 180) ? DoorAngle - 360 : DoorAngle;
if (DoorAngle <= -90f)
{
print("Door Rotation.Y is 90f");
door.localEulerAngles = new Vector3(0f, -90f, 0f);
DoorIsOpen = true;
}
if (DoorAngle < -0.01f)
{
print("Door Rotation.Y is" + DoorAngle);
DoorIsOpen = true;
}
if (door.localRotation.y >= 0f)
{
print("Door Rotation.Y is 0f");
door.localEulerAngles = new Vector3(0f, 0f, 0f);
DoorIsOpen = false;
}
}
#endregion
//}
}
else if (DoorRequiresKey)
{
Debug.Log("Door is locked");
}
else
{
Debug.Log("Door is jammed");
}
}
#endregion
//public void Drag()
//{
// DragDoor();
//}
public void Interact()
{
UpdateDrawerState();
}
private void UpdateDrawerState()
{
if (CanOpenDoor && !drawerDoorIsOpeningOrClosing)
{
if (DoorIsOpen)
{
CloseDrawer(55f);
}
else
{
OpenDrawer(65f);
}
}
else if (DoorRequiresKey)
{
Debug.Log("Drawer Door is locked");
}
else
{
Debug.Log("Drawer Door is jammed");
}
}
public void OpenDrawer(float drawerLerpSpeed)
{
//drawerDoorAudioSource.PlayOneShot(drawerDoorOpenSoundEffect);
RandomSoundsManager.GetInstance().PlayOpenCupboardSound(drawerDoorAudioSource);
drawerLerp = 0f;
DoorIsOpen = true;
print("Is opening drawer");
StartCoroutine(openDrawerCoroutine(drawerLerpSpeed));
}
IEnumerator openDrawerCoroutine(float drawerLerpSpeed)
{
Quaternion initialRotation = door.localRotation;
Quaternion targetRotation = Quaternion.Euler(_OpenRot); // Assuming _ClosedRotation is a Vector3 containing the target Euler angles for rotation
// Calculate the angle to rotate
float angleToRotate = Quaternion.Angle(initialRotation, targetRotation);
// Calculate the transitionTime based on the rotationLerpSpeed and the angle to rotate
float transitionTime = angleToRotate / drawerLerpSpeed;
drawerDoorIsOpeningOrClosing = true;
// Continuously interpolate the door rotation until it reaches the target rotation
while (Quaternion.Angle(door.localRotation, targetRotation) > 0.1f) // You can adjust the threshold (0.1f) based on your needs
{
if (Time.timeScale > 0f) // Check if time is not paused
{
drawerLerp += Time.deltaTime;
float normalizedTime = Mathf.Clamp01(drawerLerp / transitionTime);
door.localRotation = Quaternion.Slerp(door.localRotation, targetRotation, normalizedTime);
// Add additional logic here if you need to perform actions based on specific angles during the rotation
}
yield return null;
}
// Ensure that the window reaches the exact target rotation
door.localRotation = targetRotation;
DoorIsOpen = true;
drawerDoorIsOpeningOrClosing = false;
}
public void CloseDrawer(float drawerLerpSpeed)
{
RandomSoundsManager.GetInstance().PlayCloseCupboardSound(drawerDoorAudioSource);
drawerLerp = 0f;
DoorIsOpen = false;
StartCoroutine(CloseDrawerCoroutine(drawerLerpSpeed));
}
IEnumerator CloseDrawerCoroutine(float drawerLerpSpeed)
{
Quaternion initialRotation = door.localRotation;
Quaternion targetRotation = Quaternion.Euler(_CloseRot); // Assuming _ClosedRotation is a Vector3 containing the target Euler angles for rotation
// Calculate the transitionTime based on the rotationLerpSpeed
float transitionTime = Quaternion.Angle(initialRotation, targetRotation) / drawerLerpSpeed;
drawerDoorIsOpeningOrClosing = true;
// Continuously interpolate the door rotation until it reaches the target rotation
while (Quaternion.Angle(door.localRotation, targetRotation) > 0.1f) // You can adjust the threshold (0.1f) based on your needs
{
if (Time.timeScale > 0f) // Check if time is not paused
{
drawerLerp += Time.deltaTime;
float normalizedTime = Mathf.Clamp01(drawerLerp / transitionTime);
door.localRotation = Quaternion.Slerp(door.localRotation, targetRotation, normalizedTime);
// Add additional logic here if you need to perform actions based on specific angles during the rotation
if (Quaternion.Angle(door.localRotation, targetRotation) <= 15f && !hasPlayedDrawerCloseSound)
{
RandomSoundsManager.GetInstance().PlayCloseCupboardSound(drawerDoorAudioSource);
hasPlayedDrawerCloseSound = true;
}
}
yield return null;
}
// Ensure that the window reaches the exact target rotation
door.localRotation = targetRotation;
hasPlayedDrawerCloseSound = false;
DoorIsOpen = false;
drawerDoorIsOpeningOrClosing = false;
}
}
}