Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.EventSystems;
- using System.Collections.Generic;
- using TMPro;
- public class UIManager : MonoBehaviour
- {
- public static UIManager Instance { get; private set; }
- public GameObject inventoryPanel;
- public GameObject inventoryPrompt;
- public Text promptText;
- public Button yesButton;
- public Button noButton;
- public Button okButton;
- public Button closeButton;
- public Button inventoryDoneButton;
- public Transform inventorySlotParent;
- public Transform storageHolder;
- public GameObject moneyDisplay;
- public GameObject doneButton;
- public GameObject itemDetailsPanel;
- public TextMeshProUGUI itemTitleText;
- public TextMeshProUGUI itemDescriptionText;
- public TextMeshProUGUI itemUsesText;
- [SerializeField] private GameObject itemIconPrefab;
- private InventorySystem inventorySystem;
- private StorageSystem storageSystem;
- private InteractionSystem interactionSystem;
- private PlayerMovement playerMovement;
- private Item currentStorageItem;
- private Item draggedItem;
- private Transform originalSlot;
- public bool isInventoryOpen = false;
- private void Awake()
- {
- if (Instance == null)
- {
- Instance = this;
- }
- else
- {
- Destroy(gameObject);
- }
- }
- private void Start()
- {
- inventorySystem = InventorySystem.Instance;
- storageSystem = StorageSystem.Instance;
- interactionSystem = FindObjectOfType<InteractionSystem>();
- playerMovement = FindObjectOfType<PlayerMovement>();
- if (closeButton != null) closeButton.onClick.AddListener(CloseInventory);
- if (okButton != null)
- {
- okButton.onClick.AddListener(ResetPrompt);
- okButton.gameObject.SetActive(false);
- }
- if (yesButton != null) yesButton.onClick.AddListener(() => OpenInventory(currentStorageItem));
- if (noButton != null) noButton.onClick.AddListener(ClosePrompt);
- if (inventoryDoneButton != null) inventoryDoneButton.onClick.AddListener(CloseInventory);
- }
- public void ShowInventoryPrompt(Item storageItem)
- {
- if (storageItem == null) return;
- currentStorageItem = storageItem;
- inventoryPrompt.SetActive(true);
- promptText.text = $"Open {storageItem.itemName}?";
- TogglePlayerControls(false);
- }
- public void OpenInventory(Item storageItem)
- {
- inventoryPrompt.SetActive(false);
- inventoryPanel.SetActive(true);
- currentStorageItem = storageItem;
- isInventoryOpen = true;
- TransferHeldItemsToStorage();
- PopulateInventoryUI(storageItem);
- TogglePlayerControls(false);
- }
- private void TransferHeldItemsToStorage()
- {
- if (inventorySystem.leftHandItem != null)
- {
- bool added = storageSystem.AddToStorage(currentStorageItem, inventorySystem.leftHandItem);
- if (added)
- {
- inventorySystem.UnequipItem(true);
- playerMovement.UpdateCarryingAnimations();
- }
- }
- if (inventorySystem.rightHandItem != null)
- {
- bool added = storageSystem.AddToStorage(currentStorageItem, inventorySystem.rightHandItem);
- if (added)
- {
- inventorySystem.UnequipItem(false);
- playerMovement.UpdateCarryingAnimations();
- }
- }
- }
- private void PopulateInventoryUI(Item storageItem)
- {
- ClearInventoryUI();
- List<Item> storageInventory = storageSystem.GetStorageInventory(storageItem);
- for (int i = 0; i < inventorySlotParent.childCount - 3; i++)
- {
- Transform slot = inventorySlotParent.GetChild(i);
- if (i < storageInventory.Count)
- {
- UpdateInventorySlot(slot.gameObject, storageInventory[i]);
- }
- else
- {
- ClearInventorySlot(slot.gameObject);
- }
- }
- }
- private void ClearInventoryUI()
- {
- for (int i = 0; i < inventorySlotParent.childCount - 3; i++)
- {
- Transform slot = inventorySlotParent.GetChild(i);
- ClearInventorySlot(slot.gameObject);
- }
- }
- private void UpdateInventorySlot(GameObject slotObject, Item item)
- {
- ClearInventorySlot(slotObject);
- GameObject iconObject = Instantiate(itemIconPrefab, slotObject.transform);
- Image iconImage = iconObject.GetComponent<Image>();
- if (iconImage != null)
- {
- iconImage.sprite = item.icon;
- iconImage.enabled = true;
- }
- iconObject.AddComponent<ItemReference>().item = item;
- EventTrigger trigger = iconObject.GetComponent<EventTrigger>();
- if (trigger == null) trigger = iconObject.AddComponent<EventTrigger>();
- trigger.triggers.Clear();
- EventTrigger.Entry rightClickEntry = new EventTrigger.Entry();
- rightClickEntry.eventID = EventTriggerType.PointerClick;
- rightClickEntry.callback.AddListener((data) => {
- if (((PointerEventData)data).button == PointerEventData.InputButton.Right)
- TryEquipItem(item);
- });
- EventTrigger.Entry dragEntry = new EventTrigger.Entry();
- dragEntry.eventID = EventTriggerType.BeginDrag;
- dragEntry.callback.AddListener((data) => BeginDrag(iconObject.transform));
- EventTrigger.Entry dragEndEntry = new EventTrigger.Entry();
- dragEndEntry.eventID = EventTriggerType.EndDrag;
- dragEndEntry.callback.AddListener((data) => EndDrag(iconObject.transform));
- EventTrigger.Entry pointerEnterEntry = new EventTrigger.Entry();
- pointerEnterEntry.eventID = EventTriggerType.PointerEnter;
- pointerEnterEntry.callback.AddListener((data) => ShowItemDetails(item));
- EventTrigger.Entry pointerExitEntry = new EventTrigger.Entry();
- pointerExitEntry.eventID = EventTriggerType.PointerExit;
- pointerExitEntry.callback.AddListener((data) => HideItemDetails());
- trigger.triggers.Add(rightClickEntry);
- trigger.triggers.Add(dragEntry);
- trigger.triggers.Add(dragEndEntry);
- trigger.triggers.Add(pointerEnterEntry);
- trigger.triggers.Add(pointerExitEntry);
- }
- private void ClearInventorySlot(GameObject slotObject)
- {
- foreach (Transform child in slotObject.transform)
- {
- Destroy(child.gameObject);
- }
- }
- private void ShowItemDetails(Item item)
- {
- if (item != null && itemDetailsPanel != null)
- {
- itemDetailsPanel.SetActive(true);
- if (itemTitleText != null) itemTitleText.text = item.itemName;
- if (itemDescriptionText != null) itemDescriptionText.text = item.description;
- if (itemUsesText != null) itemUsesText.text = item.itemUses;
- }
- }
- private void HideItemDetails()
- {
- if (itemDetailsPanel != null)
- {
- itemDetailsPanel.SetActive(false);
- }
- }
- private void TryEquipItem(Item item)
- {
- if (inventorySystem.leftHandItem != null && inventorySystem.rightHandItem != null)
- {
- ShowError("Hands are full!");
- return;
- }
- storageSystem.RemoveItemFromStorage(currentStorageItem, item);
- bool isLeftHand = inventorySystem.leftHandItem == null;
- inventorySystem.EquipItem(item, isLeftHand, item.isTwoHanded);
- interactionSystem.UpdateItemPosition(item.gameObject, isLeftHand);
- playerMovement.UpdateCarryingAnimations();
- PopulateInventoryUI(currentStorageItem);
- HideItemDetails();
- }
- private void BeginDrag(Transform iconTransform)
- {
- ItemReference itemRef = iconTransform.GetComponent<ItemReference>();
- if (itemRef != null && itemRef.item != null)
- {
- draggedItem = itemRef.item;
- originalSlot = iconTransform.parent;
- iconTransform.SetParent(inventoryPanel.transform);
- }
- }
- private void EndDrag(Transform iconTransform)
- {
- if (draggedItem == null) return;
- Transform closestSlot = FindClosestSlot();
- if (closestSlot != null)
- {
- SwapItems(closestSlot, iconTransform);
- }
- else
- {
- ReturnItemToOriginalSlot(iconTransform);
- }
- draggedItem = null;
- }
- private Transform FindClosestSlot()
- {
- Transform closestSlot = null;
- float minDistance = Mathf.Infinity;
- foreach (Transform slot in inventorySlotParent)
- {
- float dist = Vector3.Distance(slot.position, Input.mousePosition);
- if (dist < minDistance)
- {
- minDistance = dist;
- closestSlot = slot;
- }
- }
- return closestSlot;
- }
- private void SwapItems(Transform newSlot, Transform draggedIcon)
- {
- if (newSlot.childCount > 0)
- {
- Transform existingIcon = newSlot.GetChild(0);
- existingIcon.SetParent(originalSlot);
- existingIcon.localPosition = Vector3.zero;
- }
- draggedIcon.SetParent(newSlot);
- draggedIcon.localPosition = Vector3.zero;
- }
- private void ReturnItemToOriginalSlot(Transform draggedIcon)
- {
- draggedIcon.SetParent(originalSlot);
- draggedIcon.localPosition = Vector3.zero;
- }
- public void ShowError(string message)
- {
- promptText.text = message;
- yesButton.gameObject.SetActive(false);
- noButton.gameObject.SetActive(false);
- okButton.gameObject.SetActive(true);
- }
- private void ResetPrompt()
- {
- promptText.text = $"Open {currentStorageItem.itemName}?";
- yesButton.gameObject.SetActive(true);
- noButton.gameObject.SetActive(true);
- okButton.gameObject.SetActive(false);
- inventoryPrompt.SetActive(false);
- TogglePlayerControls(true);
- }
- private void ClosePrompt()
- {
- inventoryPrompt.SetActive(false);
- TogglePlayerControls(true);
- }
- public void CloseInventory()
- {
- inventoryPanel.SetActive(false);
- isInventoryOpen = false;
- TogglePlayerControls(true);
- }
- private void TogglePlayerControls(bool enable)
- {
- if (playerMovement != null)
- {
- playerMovement.enabled = enable;
- playerMovement.GetComponent<MouseMovement>().enabled = enable;
- }
- Cursor.lockState = enable ? CursorLockMode.Locked : CursorLockMode.None;
- Cursor.visible = !enable;
- }
- }
- public class ItemReference : MonoBehaviour
- {
- public Item item;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement