Files
2025-05-29 22:31:40 +03:00

192 lines
6.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using InfallibleCode;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
namespace InfallibleCode
{
[RequireComponent(typeof(AudioSource))]
public class KeyHole : MonoBehaviour, IInteractable
{
[SerializeField] private string doorNameID;
//public DoorDebugging door;
public DoorBehaviour door;
public KeyPickup KeyPickup;
public bool keyAndDoorNameMatch;
public LocalizedString keyUsedMessage;
public LocalizedString keyMismatchMessage;
public EventDelegateTrigger eventDelegateTriggerOnKeyUsed;
public EventTrigger OnKeyUsed;
//public bool canInsertKey;
private AudioSource _audioSource;
private void Start()
{
if (door == null)
{
door = GetComponent<DoorBehaviour>();
}
_audioSource = GetComponent<AudioSource>();
_audioSource.loop = false;
}
//// Update is called once per frame
//void Update()
//{
// //if (canInsertKey && door.DoorRequiresKey)
// //{
// // if (Input.GetKeyDown(KeyCode.Mouse0) /*&& KeyPickup.hasKey*/)
// // {
// // if (KeyPickup.hasKey)
// // {
// // KeyPickup.hasKey = false;
// // door.UnlockDoor();
// // print("You used your key!");
// // }
// // else
// // {
// // print("You don't have a key or you have already used it");
// // }
// // }
// //}
//}
public void UseKey()
{
if (door.DoorRequiresKey)
{
#region Old keypPickup Logic
//if (KeyPickup != null)
//{
// if (KeyPickup.hasKey)
// {
// KeyPickup.hasKey = false;
// door.UnlockDoor();
// UIManager.GetInstance().StartCoroutine(UIManager.GetInstance().DisplayMessage("You used your key."));
// AudioManager.GetInstance().UnlockDoorClip(_audioSource);
// if (eventDelegateTriggerOnKeyUsed != null)
// {
// eventDelegateTriggerOnKeyUsed?.OnEventInvoke();
// }
// print("You used your key!");
// }
// else
// {
// //UIManager.GetInstance().StartCoroutine(UIManager.GetInstance().DisplayMessage("Locked"));
// print("You don't have a key or you have already used it");
// }
//}
//else
//{
#endregion
if (keyAndDoorNameMatch)
{
door.UnlockDoor();
string localizedText = LocalizationSettings.StringDatabase.GetLocalizedString("Messages", "keyUsed");
UIManager.GetInstance().StartCoroutine(UIManager.GetInstance().DisplayMessage(keyUsedMessage));
AudioManager.GetInstance().UnlockDoorClip(_audioSource);
if (eventDelegateTriggerOnKeyUsed != null)
{
eventDelegateTriggerOnKeyUsed?.OnEventInvoke();
print("(eventDelegateTriggerOnKeyUsed) invoked after inserted key in key hole!");
}
if(OnKeyUsed!= null)
{
OnKeyUsed.Invoke();
print("On key used event invoked");
}
KeysManager.GetInstance().RemoveKeyNameIDFromList(doorNameID);
keyAndDoorNameMatch = false;
print("You used your key!");
}
else
{
//UIManager.GetInstance().StartCoroutine(UIManager.GetInstance().DisplayMessage("Locked"));
print("You don't have a key or you have already used it");
}
//}
}
}
public void OpenInventoryToUseKey()
{
if (GameplayController.GetInstance().IsOnState(GameplayController.State.InventoryMenu) == false &&
KeysManager.GetInstance().keyNameID.Contains(doorNameID))
{
InventoryManager.GetInstance().OpenInventory();
//StartCoroutine(UpdateInteraction());
}
}
public void Interact()
{
OpenInventoryToUseKey();
}
public void TryUnlockDoor()
{
if (KeysManager.GetInstance().keyNameID.Contains(doorNameID) &&
GameplayController.GetInstance().IsOnState(GameplayController.State.InventoryMenu))
{
LookForMatchingKeyAndDoorName();
UseKey();
}
}
private void LookForMatchingKeyAndDoorName()
{
//if (!GameplayController.GetInstance().IsOnState(GameplayController.State.InventoryMenu))
//{
// foreach (var name in KeysManager.GetInstance().keyNameID)
// {
// if (name == doorNameID)
// {
// keyAndDoorNameMatch = true;
// }
// }
//}
//else
//{
if (KeysManager.GetInstance().currentlySelectedInventoryKeyNameID == doorNameID)
{
keyAndDoorNameMatch = true;
KeysManager.GetInstance().currentlySelectedKeyAndDoorNameMatch = true;
KeysManager.GetInstance().currentlySelectedInventoryKeyNameID = "";
print("Found matching key and door with the same ID");
}
else
{
string localizedText = LocalizationSettings.StringDatabase.GetLocalizedString("Messages", "keyMismatch");
UIManager.GetInstance().DisplayMessageOnScreen(keyMismatchMessage);
}
//}
}
//private void OnTriggerStay(Collider col)
//{
// if (col.gameObject.tag == "Player")
// {
// canInsertKey = true;
// }
//}
//private void OnTriggerExit(Collider col)
//{
// if (col.gameObject.tag == "Player")
// {
// canInsertKey = false;
// }
//}
}
}