327 lines
11 KiB
C#
327 lines
11 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Audio;
|
|
|
|
namespace InfallibleCode
|
|
{
|
|
public class Drawer : MonoBehaviour, /*IDraggable*/ IInteractable
|
|
{
|
|
public float DrawerMin;
|
|
private float DrawerDefaultMin;
|
|
public float DrawerMax = 0.70f;
|
|
|
|
[Header("Drawer Draw Out")]
|
|
public bool CanOpenDrawer = true;
|
|
public bool DrawerRequiresKey;
|
|
public Transform _drawerDrawOutIn;
|
|
|
|
[SerializeField] private float DrawSpeed = 1f;
|
|
[SerializeField] private bool DrawerIsOpen;
|
|
|
|
Vector3 _OpenPos;
|
|
Vector3 _ClosedPos;
|
|
float posZ;
|
|
public float LerpDrawer;
|
|
|
|
public AudioSource drawerAudioSource;
|
|
public AudioMixer audioMixer;
|
|
//public AudioClip drawerOpenSoundEffect;
|
|
//public AudioClip drawerCloseSoundEffect;
|
|
|
|
[Header("DEBUGGING")]
|
|
public bool IsOpeningDrawer;
|
|
public bool IsClosingDrawer;
|
|
|
|
public bool IsPullingOut;
|
|
public bool IsPushingIn;
|
|
public bool IsDraggingDrawer;
|
|
public bool drawerIsOpeningOrClosing;
|
|
|
|
public bool IsDrawerABottom; // the bottom drawer is unstable
|
|
|
|
//public bool IsDraggingDrawer;
|
|
|
|
private void Awake()
|
|
{
|
|
if(_drawerDrawOutIn == null)
|
|
{
|
|
_drawerDrawOutIn = GetComponent<Transform>();
|
|
}
|
|
if (gameObject.GetComponent<AudioSource>() == null)
|
|
{
|
|
drawerAudioSource = gameObject.AddComponent(typeof(AudioSource)) as AudioSource;
|
|
// Assign the Audio Mixer to the AudioSource
|
|
if (audioMixer != null)
|
|
{
|
|
drawerAudioSource.outputAudioMixerGroup = audioMixer.FindMatchingGroups("SFX")[0];
|
|
}
|
|
}
|
|
|
|
//drawerOpenSoundEffect = Resources.Load<AudioClip>("DrawerSounds/360949__marcusgar__drawer");
|
|
//drawerCloseSoundEffect = Resources.Load("DrawerSounds/486082__nox_sound__object_drawer_wood_close") as AudioClip;
|
|
// RandomSoundsManager.GetInstance().PlayOpenDrawerSound(drawerAudioSource);
|
|
//RandomSoundsManager.GetInstance().PlayCloseDrawerSound(drawerAudioSource);
|
|
|
|
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
DrawerDefaultMin = _drawerDrawOutIn.localPosition.z;
|
|
DrawerMin = DrawerDefaultMin;
|
|
|
|
//DrawerMax = 0.7
|
|
if (DrawerRequiresKey)
|
|
{
|
|
CanOpenDrawer = false;
|
|
}
|
|
|
|
//If you dont put local position it flies horizontally its funny
|
|
_OpenPos = new Vector3(_drawerDrawOutIn.localPosition.x, _drawerDrawOutIn.localPosition.y, DrawerMax);
|
|
_ClosedPos = new Vector3(_drawerDrawOutIn.localPosition.x, _drawerDrawOutIn.localPosition.y, DrawerMin);
|
|
|
|
if (drawerAudioSource == null)
|
|
{
|
|
drawerAudioSource = GetComponent<AudioSource>();
|
|
}
|
|
drawerAudioSource.loop = false;
|
|
drawerAudioSource.spatialBlend = 1f;
|
|
drawerAudioSource.maxDistance = 100f;
|
|
}
|
|
|
|
//private void Update()
|
|
//{
|
|
|
|
// if (/*DraggablesManager.GetInstance().*/IsDraggingDrawer)
|
|
// {
|
|
// #region - Input (Negative) Open drawer
|
|
// if (Input.GetAxis("Mouse Y") < 0 && DraggablesManager.GetInstance().IsDragging && !IsClosingDrawer)
|
|
// {
|
|
// LerpDrawer = Mathf.Abs(posZ);
|
|
// IsOpeningDrawer = true;
|
|
// IsClosingDrawer = false;
|
|
// LerpDrawer += posZ * Time.deltaTime;
|
|
// }
|
|
// else if (!IsClosingDrawer)
|
|
// {
|
|
// IsOpeningDrawer = false;
|
|
// LerpDrawer = 0f;
|
|
// StopAllCoroutines(); //STOP OPENING DRAWER.
|
|
// /*DraggablesManager.GetInstance().*/IsDraggingDrawer = false;
|
|
// print("Stop Opening drawer");
|
|
// }
|
|
// #endregion
|
|
|
|
// #region + Input (Positive) Close drawer
|
|
// if (Input.GetAxis("Mouse Y") > 0 && DraggablesManager.GetInstance().IsDragging)
|
|
// {
|
|
// IsClosingDrawer = true;
|
|
// IsOpeningDrawer = false;
|
|
// LerpDrawer += posZ * Time.deltaTime;
|
|
// }
|
|
// else if (!IsOpeningDrawer)
|
|
// {
|
|
// //IsClosingDrawer = false;
|
|
// //LerpDrawer = 0f;
|
|
|
|
// IsClosingDrawer = false;
|
|
// LerpDrawer = 0f;
|
|
// StopAllCoroutines(); //STOP OPENING DRAWER.
|
|
// /*DraggablesManager.GetInstance().*/IsDraggingDrawer = false;
|
|
// print("Stop Closing drawer");
|
|
// }
|
|
// #endregion
|
|
// }
|
|
|
|
//}
|
|
|
|
|
|
//public void DragDrawer()
|
|
//{
|
|
// if (CanOpenDrawer)
|
|
// {
|
|
// DraggablesManager.GetInstance().IsDragging = true;
|
|
// /*DraggablesManager.GetInstance().*/IsDraggingDrawer = true;
|
|
|
|
// posZ = Input.GetAxis("Mouse Y") * DrawSpeed * Mathf.Deg2Rad;
|
|
|
|
// #region - Input (Negative) Open drawer
|
|
// //------------------ OPENING DRAWER -----------------------
|
|
// if (IsOpeningDrawer)
|
|
// {
|
|
// StopCoroutine(CloseDrawer()); //STOP CLOSING DRAWER.
|
|
// StartCoroutine(OpenDrawer());
|
|
// }
|
|
// #endregion
|
|
|
|
// #region + Input (Positive) Close drawer
|
|
// //------------------ CLOSING DRAWER -----------------------
|
|
// if (IsClosingDrawer)
|
|
// {
|
|
// StopCoroutine(OpenDrawer()); //STOP OPENING DRAWER.
|
|
// StartCoroutine(CloseDrawer());
|
|
// }
|
|
// #endregion
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
public void Interact()
|
|
{
|
|
UpdateDrawerState();
|
|
}
|
|
|
|
private void UpdateDrawerState()
|
|
{
|
|
if (CanOpenDrawer && !drawerIsOpeningOrClosing)
|
|
{
|
|
if (DrawerIsOpen)
|
|
{
|
|
CloseDrawer(1.5f);
|
|
}
|
|
else
|
|
{
|
|
OpenDrawer(1.2f);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public void CloseDrawer(float drawerLerpSpeed)
|
|
{
|
|
|
|
DrawerIsOpen = true;
|
|
LerpDrawer = 0f;
|
|
//drawerAudioSource.PlayOneShot(drawerCloseSoundEffect);
|
|
RandomSoundsManager.GetInstance().PlayCloseDrawerSound(drawerAudioSource);
|
|
StartCoroutine(CloseDrawerCoroutine(drawerLerpSpeed));
|
|
//DrawerIsOpen = false;
|
|
//
|
|
|
|
}
|
|
|
|
IEnumerator CloseDrawerCoroutine(float drawerLerpSpeed)
|
|
{
|
|
Vector3 initialPos = _drawerDrawOutIn.localPosition;
|
|
Vector3 targetPos = _ClosedPos;
|
|
// Calculate the transitionTime based on the windowLerpSpeed
|
|
float transitionTime = Vector3.Distance(initialPos, targetPos) / drawerLerpSpeed;
|
|
|
|
drawerIsOpeningOrClosing = true;
|
|
|
|
while (LerpDrawer <= transitionTime)//Maya: I am not sure about the calculations here
|
|
{
|
|
LerpDrawer += Time.deltaTime;
|
|
float normalizedTime = Mathf.Clamp01(LerpDrawer / transitionTime);
|
|
_drawerDrawOutIn.localPosition = Vector3.Lerp(initialPos, targetPos, normalizedTime);
|
|
|
|
float drawerPos = _drawerDrawOutIn.transform.localPosition.y;
|
|
if (!IsDrawerABottom)// wtffff
|
|
{
|
|
if (drawerIsOpeningOrClosing && drawerPos <= DrawerMin) //Maya: I am not sure about the calculations here
|
|
{
|
|
//if (drawerIsOpeningOrClosing)
|
|
//{
|
|
drawerIsOpeningOrClosing = false;
|
|
DrawerIsOpen = false;//
|
|
//} The problem is here
|
|
|
|
print("stop Closing");
|
|
StopAllCoroutines();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (drawerIsOpeningOrClosing && drawerPos >= DrawerMin) //Maya: I am not sure about the calculations here
|
|
{
|
|
//if (drawerIsOpeningOrClosing)
|
|
//{
|
|
drawerIsOpeningOrClosing = false;
|
|
DrawerIsOpen = false;//
|
|
//} The problem is here
|
|
|
|
print("stop Closing");
|
|
StopAllCoroutines();
|
|
}
|
|
}
|
|
|
|
yield return null;
|
|
}
|
|
DrawerIsOpen = false;
|
|
drawerIsOpeningOrClosing = false;
|
|
}
|
|
|
|
|
|
public void OpenDrawer(float drawerLerpSpeed)
|
|
{
|
|
//drawerAudioSource.PlayOneShot(drawerOpenSoundEffect);
|
|
RandomSoundsManager.GetInstance().PlayOpenDrawerSound(drawerAudioSource);
|
|
DrawerIsOpen = false;
|
|
LerpDrawer = 0f;
|
|
StartCoroutine(OpenDrawerCoroutine(drawerLerpSpeed));
|
|
}
|
|
|
|
IEnumerator OpenDrawerCoroutine(float drawerLerpSpeed)
|
|
{
|
|
Vector3 initialPos = _drawerDrawOutIn.localPosition;
|
|
Vector3 targetPos = _OpenPos;
|
|
// Calculate the transitionTime based on the windowLerpSpeed
|
|
float transitionTime = Vector3.Distance(initialPos, targetPos) / drawerLerpSpeed;
|
|
|
|
drawerIsOpeningOrClosing = true;
|
|
while (LerpDrawer <= transitionTime)
|
|
{
|
|
LerpDrawer += Time.deltaTime;
|
|
float normalizedTime = Mathf.Clamp01(LerpDrawer / transitionTime);
|
|
_drawerDrawOutIn.localPosition = Vector3.Lerp(initialPos, targetPos, normalizedTime);
|
|
|
|
float drawerPos = _drawerDrawOutIn.transform.localPosition.y;
|
|
if (drawerIsOpeningOrClosing && drawerPos >= DrawerMax)
|
|
{
|
|
// if (drawerIsOpeningOrClosing)
|
|
//{
|
|
drawerIsOpeningOrClosing = false;
|
|
DrawerIsOpen = true;//
|
|
//}
|
|
|
|
print("stop Opening");
|
|
StopAllCoroutines();
|
|
}
|
|
|
|
yield return null;
|
|
}
|
|
DrawerIsOpen = true;
|
|
drawerIsOpeningOrClosing = false;
|
|
}
|
|
|
|
|
|
//public IEnumerator OpenDrawer()
|
|
//{
|
|
// while (LerpDrawer <= 1f)
|
|
// {
|
|
// _drawerDrawOutIn.localPosition = Vector3.Lerp(_drawerDrawOutIn.localPosition, _OpenPos, LerpDrawer);
|
|
// DrawerIsOpen = true;
|
|
// print("Is opening drawer");
|
|
// yield return null;
|
|
// }
|
|
//}
|
|
|
|
//public IEnumerator CloseDrawer()
|
|
//{
|
|
// while (LerpDrawer <= 1f)
|
|
// {
|
|
// _drawerDrawOutIn.localPosition = Vector3.Lerp(_drawerDrawOutIn.localPosition, _ClosedPos, LerpDrawer);
|
|
// DrawerIsOpen = false;
|
|
// print("Is closing drawer");
|
|
// yield return null;
|
|
// }
|
|
//}
|
|
|
|
//public void Drag()
|
|
//{
|
|
// DragDrawer();
|
|
//}
|
|
}
|
|
} |