using System.Collections; using System.Collections.Generic; using UnityEngine; using InfallibleCode; using System.Reflection; using System; using TRInventoryUpdatable; public class KeyPickup : MonoBehaviour { public ExaminableItem examinableItem; [SerializeField] private string KeyNameID; //public int KeyID; private bool _canPickupKeyItem; //public bool _canPressButtonDown = true; public bool hasKey; [HideInInspector] public bool ItemCollected; public MeshRenderer _KeyGameObject; public GameObject keyModelForKeyHole; public string keyModelName; public string keyNameInInventory; public bool IsReusable; public bool shouldDestroyKey = true; private BodyPartJointHole bodyPartJointHole; Transform bodyPartPivotPosition; PickupEvent pickupEvent; public enum KeyType { DoorKeyType, BodyPartKeyType } public KeyType keyType = KeyType.DoorKeyType; public EventDelegateTrigger eventDelegateTriggerOnKeyObtained; //private void OnEnable() //{ // if (hasKey) // { // gameObject.SetActive(false); // } //} void Start() { //foreach (MeshRenderer keyGameObject in _KeyGameObject) //{ // keyGameObject = GetComponentsInChildren(); //} if (_KeyGameObject == null) { _KeyGameObject = GetComponentInChildren(); } if (TryGetComponent(out ExaminableItem _examinableItem)) { examinableItem = _examinableItem; } if (TryGetComponent(out PickupEvent _pickupEvent)) { pickupEvent = _pickupEvent; } } // Update is called once per frame void Update() { #region Old Implementation //if (_canPickupKeyItem && _canPressButtonDown) //{ // if (Input.GetKeyDown(KeyCode.E)) // { // hasKey = true; // _canPickupKeyItem = false; // _canPressButtonDown = false; // Destroy(gameObject); // #region Debugging // print("You picked up the key!"); // #endregion // } //} #endregion PickUpKey(); } void PickUpKey() { //Change later to update from a coroutine if (examinableItem != null) { if (/*_canPickupKeyItem && *//*_canPressButtonDown*/examinableItem.examineItem.CanPressButtonToGetItem && examinableItem.examineItem.CurrenltyExaminingObj) { if (Input.GetKeyDown(KeyCode.E) || Input.GetKeyDown(KeyCode.JoystickButton0)) { hasKey = true; ItemCollected = true; _canPickupKeyItem = false; examinableItem.examineItem.CanPressButtonToGetItem = false; UIManager.GetInstance().userInterfaceIsOnScreen = false; //_canPressButtonDown = false; if (pickupEvent != null) { pickupEvent.eventAfterPickup.Invoke(); } #region Body part keypickup related //TryGetBodyPartJointHoleComponentInParent(); if (TryGetComponent(out BodyPartKeyAdditional bodyPartKeyAdditional)) { if (bodyPartKeyAdditional.bodyPartJointHoleOfThisBodyPart != null) { bodyPartKeyAdditional.bodyPartJointHoleOfThisBodyPart.hasBodyPartInserted = false; bodyPartKeyAdditional.bodyPartJointHoleOfThisBodyPart.hasCorrectBodyPartInserted = false; bodyPartKeyAdditional.bodyPartJointHoleOfThisBodyPart.afterAttachOrDetachEvent.Invoke(); } else { Debug.Log("bodyPartJointHoleOfThisBodyPart in BodyPartKeyAdditional Component is NULL."); } } else { Debug.Log("BodyPartKeyAdditional Component not found."); } //if (TryGetComponent(out BodyPartJointHole _bodyPartJointHole)) //{ // bodyPartJointHole = _bodyPartJointHole; // bodyPartJointHole.hasBodyPartInserted = false; // bodyPartJointHole.hasCorrectBodyPartInserted = false; //} #endregion StartCoroutine(PickingUpObject()); #region Debugging print("You picked up the key!"); #endregion } } } } void TryGetBodyPartJointHoleComponentInParent() where T : MonoBehaviour, IInteractable { if (examinableItem.examineItem._item3DParent != null) { // Try to get the component in the parent GameObject T component = GetComponentInParent(); // If the component is found, store it if (component != null) { bodyPartJointHole = component as BodyPartJointHole; bodyPartJointHole.hasBodyPartInserted = false; bodyPartJointHole.hasCorrectBodyPartInserted = false; bodyPartJointHole.afterAttachOrDetachEvent.Invoke(); Debug.Log("Found " + typeof(T).ToString() + " in parent"); } else { Debug.Log("No " + typeof(T).ToString() + " found"); } } else { print("No parent found"); } } /// /// Maybe you can call this from a unity event in the scene. /// public void AddKeyToInventory() { hasKey = true; ItemCollected = true; _canPickupKeyItem = false; StartCoroutine(PickingUpObject()); print("Is Adding key to Inventory"); } IEnumerator PickingUpObject() { KeysManager.GetInstance().AddKeyNameID(KeyNameID); //First instantiate the key model you want: GameObject key = Instantiate(AccessFieldInInventoryManager(keyModelName).gameObject, InventoryManager.GetInstance().TransformWithPosRotForItemsToAdd.position, InventoryManager.GetInstance().TransformWithPosRotForItemsToAdd.rotation, InventoryManager.GetInstance().itemsToAddTransform); //Set the key name that's going to show up when the player is in the selection of the key's slot. KeyInventoryItem keyInventoryItem = key.GetComponent(); keyInventoryItem.keyNameInInventory = keyNameInInventory; keyInventoryItem.keyNameID = KeyNameID; keyInventoryItem.IsReusable = IsReusable; if (keyModelForKeyHole != null) { keyInventoryItem.keyModelForKeyHole = keyModelForKeyHole; } //keyInventoryItem.keyTypeString = keyType.ToString(); //Add the new instantiated item into the keysAndItemsInventory RingInventory.GetInstance().AddItemToKeysAndItemsInventory(key.transform); //key.GetComponentInChildren().gameObject.layer = 5; //Added today 11/08/24 Yiannis ChangeLayerRecursively(key.transform, 5); #region SAVE TEMPORARILY THE KEY ITEM InventoryManager.GetInstance().SaveTemporarilyKeyInventoryItemData(KeyNameID/*, keyModelForKeyHole.name*/, keyModelName, keyNameInInventory, gameObject.name, IsReusable); #endregion #region SAVE TEMPORARILY THE PICKUP //InventoryManager.GetInstance().SaveTemporarilyObtainedItemsData(gameObject.name); #endregion if (shouldDestroyKey) { Destroy(_KeyGameObject.gameObject); } yield return new WaitForSecondsRealtime(0.1f); if (examinableItem != null) { examinableItem.examineItem.GotItemExitDisplayingItem3d(); } if (eventDelegateTriggerOnKeyObtained != null) { eventDelegateTriggerOnKeyObtained?.OnEventInvoke(); } yield return new WaitForSecondsRealtime(0.1f); if (shouldDestroyKey) { Destroy(gameObject); } } void ChangeLayerRecursively(Transform obj, int newLayer) { // Skip changing the layer if the object has the VisibilityIcon script if (obj.GetComponent() != null) { return; } // Change the layer of the current child object obj.gameObject.layer = newLayer; // Recursively change the layer of all children foreach (Transform child in obj) { ChangeLayerRecursively(child, newLayer); } } public GameObject AccessFieldInInventoryManager(string fieldName) { if (InventoryManager.GetInstance() == null) { Debug.LogWarning("InventoryManager instance not found."); return null; } // Get the InventoryManager instance InventoryManager inventoryManager = InventoryManager.GetInstance(); // Start a recursive search for the field in InventoryManager return FindFieldRecursively(inventoryManager, fieldName); } private GameObject FindFieldRecursively(object currentObject, string fieldName) { if (currentObject == null) return null; // Get the type of the current object Type type = currentObject.GetType(); // First, check if the field exists at the current level FieldInfo field = type.GetField(fieldName, BindingFlags.Public | BindingFlags.Instance); if (field != null) { // If the field exists and is a GameObject, return it return field.GetValue(currentObject) as GameObject; } // If the field is not found, search all public instance fields of the current object FieldInfo[] allFields = type.GetFields(BindingFlags.Public | BindingFlags.Instance); foreach (FieldInfo subField in allFields) { // Get the value of the subfield (e.g., a nested class or struct) object subFieldValue = subField.GetValue(currentObject); if (subFieldValue == null) continue; // Recursively search within this subfield GameObject result = FindFieldRecursively(subFieldValue, fieldName); if (result != null) { return result; // If found in the nested field, return it } } // If the field is not found in this object or its nested fields, return null return null; } #region Old Implementation //private void OnTriggerStay(Collider col) //{ // if (col.gameObject.tag == "Player") // { // _canPickupKeyItem = true; // } //} //private void OnTriggerExit(Collider col) //{ // if (col.gameObject.tag == "Player") // { // _canPickupKeyItem = false; // } //} #endregion }