Advertisement
evelynshilosky

UIManager - Part 5

Feb 25th, 2025
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.28 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.EventSystems;
  4. using System.Collections.Generic;
  5. using TMPro;
  6.  
  7. public class UIManager : MonoBehaviour
  8. {
  9.     public static UIManager Instance { get; private set; }
  10.  
  11.     public GameObject inventoryPanel;
  12.     public GameObject inventoryPrompt;
  13.     public Text promptText;
  14.     public Button yesButton;
  15.     public Button noButton;
  16.     public Button okButton;
  17.     public Button closeButton;
  18.     public Button inventoryDoneButton;
  19.     public Transform inventorySlotParent;
  20.     public Transform storageHolder;
  21.     public GameObject moneyDisplay;
  22.     public GameObject doneButton;
  23.     public GameObject itemDetailsPanel;
  24.     public TextMeshProUGUI itemTitleText;
  25.     public TextMeshProUGUI itemDescriptionText;
  26.     public TextMeshProUGUI itemUsesText;
  27.  
  28.     [SerializeField] private GameObject itemIconPrefab;
  29.  
  30.     private InventorySystem inventorySystem;
  31.     private StorageSystem storageSystem;
  32.     private InteractionSystem interactionSystem;
  33.     private PlayerMovement playerMovement;
  34.     private Item currentStorageItem;
  35.     private Item draggedItem;
  36.     private Transform originalSlot;
  37.  
  38.     public bool isInventoryOpen = false;
  39.  
  40.     private void Awake()
  41.     {
  42.         if (Instance == null)
  43.         {
  44.             Instance = this;
  45.         }
  46.         else
  47.         {
  48.             Destroy(gameObject);
  49.         }
  50.     }
  51.  
  52.     private void Start()
  53.     {
  54.         inventorySystem = InventorySystem.Instance;
  55.         storageSystem = StorageSystem.Instance;
  56.         interactionSystem = FindObjectOfType<InteractionSystem>();
  57.         playerMovement = FindObjectOfType<PlayerMovement>();
  58.  
  59.         if (closeButton != null) closeButton.onClick.AddListener(CloseInventory);
  60.         if (okButton != null)
  61.         {
  62.             okButton.onClick.AddListener(ResetPrompt);
  63.             okButton.gameObject.SetActive(false);
  64.         }
  65.         if (yesButton != null) yesButton.onClick.AddListener(() => OpenInventory(currentStorageItem));
  66.         if (noButton != null) noButton.onClick.AddListener(ClosePrompt);
  67.         if (inventoryDoneButton != null) inventoryDoneButton.onClick.AddListener(CloseInventory);
  68.     }
  69.  
  70.     public void ShowInventoryPrompt(Item storageItem)
  71.     {
  72.         currentStorageItem = storageItem;
  73.         inventoryPrompt.SetActive(true);
  74.         promptText.text = storageItem != null ? $"Open {storageItem.itemName}?" : "";
  75.         TogglePlayerControls(false);
  76.     }
  77.  
  78.     public void UpdatePromptText(string message)
  79.     {
  80.         if (promptText != null)
  81.         {
  82.             promptText.text = message;
  83.         }
  84.     }
  85.  
  86.     public void ShowOkayButton()
  87.     {
  88.         if (okButton != null)
  89.         {
  90.             okButton.gameObject.SetActive(true);
  91.             yesButton.gameObject.SetActive(false);
  92.             noButton.gameObject.SetActive(false);
  93.         }
  94.     }
  95.  
  96.     public void OpenInventory(Item storageItem)
  97.     {
  98.         inventoryPrompt.SetActive(false);
  99.         inventoryPanel.SetActive(true);
  100.         currentStorageItem = storageItem;
  101.         isInventoryOpen = true;
  102.  
  103.         TransferHeldItemsToStorage();
  104.         PopulateInventoryUI(storageItem);
  105.         TogglePlayerControls(false);
  106.     }
  107.  
  108.     private void TransferHeldItemsToStorage()
  109.     {
  110.         if (inventorySystem.leftHandItem != null)
  111.         {
  112.             bool added = storageSystem.AddToStorage(currentStorageItem, inventorySystem.leftHandItem);
  113.             if (added)
  114.             {
  115.                 inventorySystem.UnequipItem(true);
  116.                 playerMovement.UpdateCarryingAnimations();
  117.             }
  118.         }
  119.         if (inventorySystem.rightHandItem != null)
  120.         {
  121.             bool added = storageSystem.AddToStorage(currentStorageItem, inventorySystem.rightHandItem);
  122.             if (added)
  123.             {
  124.                 inventorySystem.UnequipItem(false);
  125.                 playerMovement.UpdateCarryingAnimations();
  126.             }
  127.         }
  128.     }
  129.  
  130.     private void PopulateInventoryUI(Item storageItem)
  131.     {
  132.         ClearInventoryUI();
  133.         List<Item> storageInventory = storageSystem.GetStorageInventory(storageItem);
  134.  
  135.         for (int i = 0; i < inventorySlotParent.childCount - 3; i++)
  136.         {
  137.             Transform slot = inventorySlotParent.GetChild(i);
  138.             ClearInventorySlot(slot.gameObject);
  139.         }
  140.  
  141.         foreach (Item item in storageInventory)
  142.         {
  143.             int slotIndex = storageSystem.GetItemSlotIndex(storageItem, item);
  144.             if (slotIndex == -1 || slotIndex >= inventorySlotParent.childCount - 3)
  145.             {
  146.                 slotIndex = FindFirstEmptySlot();
  147.             }
  148.  
  149.             if (slotIndex != -1)
  150.             {
  151.                 Transform slot = inventorySlotParent.GetChild(slotIndex);
  152.                 UpdateInventorySlot(slot.gameObject, item);
  153.                 storageSystem.UpdateItemPosition(storageItem, item, slotIndex);
  154.             }
  155.         }
  156.     }
  157.  
  158.     private void ClearInventoryUI()
  159.     {
  160.         for (int i = 0; i < inventorySlotParent.childCount - 3; i++)
  161.         {
  162.             Transform slot = inventorySlotParent.GetChild(i);
  163.             ClearInventorySlot(slot.gameObject);
  164.         }
  165.     }
  166.  
  167.     private void UpdateInventorySlot(GameObject slotObject, Item item)
  168.     {
  169.         ClearInventorySlot(slotObject);
  170.  
  171.         GameObject iconObject = Instantiate(itemIconPrefab, slotObject.transform);
  172.         Image iconImage = iconObject.GetComponent<Image>();
  173.         if (iconImage != null)
  174.         {
  175.             iconImage.sprite = item.icon;
  176.             iconImage.enabled = true;
  177.         }
  178.  
  179.         iconObject.AddComponent<ItemReference>().item = item;
  180.  
  181.         EventTrigger trigger = iconObject.GetComponent<EventTrigger>();
  182.         if (trigger == null) trigger = iconObject.AddComponent<EventTrigger>();
  183.  
  184.         trigger.triggers.Clear();
  185.  
  186.         EventTrigger.Entry rightClickEntry = new EventTrigger.Entry();
  187.         rightClickEntry.eventID = EventTriggerType.PointerClick;
  188.         rightClickEntry.callback.AddListener((data) => {
  189.             if (((PointerEventData)data).button == PointerEventData.InputButton.Right)
  190.                 TryEquipItem(item);
  191.         });
  192.  
  193.         EventTrigger.Entry dragEntry = new EventTrigger.Entry();
  194.         dragEntry.eventID = EventTriggerType.BeginDrag;
  195.         dragEntry.callback.AddListener((data) => BeginDrag(iconObject.transform));
  196.  
  197.         EventTrigger.Entry dragEndEntry = new EventTrigger.Entry();
  198.         dragEndEntry.eventID = EventTriggerType.EndDrag;
  199.         dragEndEntry.callback.AddListener((data) => EndDrag(iconObject.transform));
  200.  
  201.         EventTrigger.Entry pointerEnterEntry = new EventTrigger.Entry();
  202.         pointerEnterEntry.eventID = EventTriggerType.PointerEnter;
  203.         pointerEnterEntry.callback.AddListener((data) => ShowItemDetails(item));
  204.  
  205.         EventTrigger.Entry pointerExitEntry = new EventTrigger.Entry();
  206.         pointerExitEntry.eventID = EventTriggerType.PointerExit;
  207.         pointerExitEntry.callback.AddListener((data) => HideItemDetails());
  208.  
  209.         trigger.triggers.Add(rightClickEntry);
  210.         trigger.triggers.Add(dragEntry);
  211.         trigger.triggers.Add(dragEndEntry);
  212.         trigger.triggers.Add(pointerEnterEntry);
  213.         trigger.triggers.Add(pointerExitEntry);
  214.     }
  215.  
  216.     private void ClearInventorySlot(GameObject slotObject)
  217.     {
  218.         foreach (Transform child in slotObject.transform)
  219.         {
  220.             Destroy(child.gameObject);
  221.         }
  222.     }
  223.  
  224.     private void ShowItemDetails(Item item)
  225.     {
  226.         if (item != null && itemDetailsPanel != null)
  227.         {
  228.             itemDetailsPanel.SetActive(true);
  229.             if (itemTitleText != null) itemTitleText.text = item.itemName;
  230.             if (itemDescriptionText != null) itemDescriptionText.text = item.description;
  231.             if (itemUsesText != null) itemUsesText.text = item.itemUses;
  232.         }
  233.     }
  234.  
  235.     private void HideItemDetails()
  236.     {
  237.         if (itemDetailsPanel != null)
  238.         {
  239.             itemDetailsPanel.SetActive(false);
  240.         }
  241.     }
  242.  
  243.     private void TryEquipItem(Item item)
  244.     {
  245.         if (inventorySystem.leftHandItem != null && inventorySystem.rightHandItem != null)
  246.         {
  247.             ShowError("Hands are full!");
  248.             return;
  249.         }
  250.  
  251.         storageSystem.RemoveItemFromStorage(currentStorageItem, item);
  252.         bool isLeftHand = inventorySystem.leftHandItem == null;
  253.         inventorySystem.EquipItem(item, isLeftHand, item.isTwoHanded);
  254.         interactionSystem.UpdateItemPosition(item.gameObject, isLeftHand);
  255.         playerMovement.UpdateCarryingAnimations();
  256.         PopulateInventoryUI(currentStorageItem);
  257.         HideItemDetails();
  258.     }
  259.  
  260.     private void BeginDrag(Transform iconTransform)
  261.     {
  262.         ItemReference itemRef = iconTransform.GetComponent<ItemReference>();
  263.         if (itemRef != null && itemRef.item != null)
  264.         {
  265.             draggedItem = itemRef.item;
  266.             originalSlot = iconTransform.parent;
  267.             iconTransform.SetParent(inventoryPanel.transform);
  268.         }
  269.     }
  270.  
  271.     private void EndDrag(Transform iconTransform)
  272.     {
  273.         if (draggedItem == null) return;
  274.  
  275.         Transform closestSlot = FindClosestSlot();
  276.  
  277.         if (closestSlot != null)
  278.         {
  279.             SwapItems(closestSlot, iconTransform);
  280.         }
  281.         else
  282.         {
  283.             ReturnItemToOriginalSlot(iconTransform);
  284.         }
  285.  
  286.         draggedItem = null;
  287.     }
  288.  
  289.     private Transform FindClosestSlot()
  290.     {
  291.         Transform closestSlot = null;
  292.         float minDistance = Mathf.Infinity;
  293.  
  294.         foreach (Transform slot in inventorySlotParent)
  295.         {
  296.             float dist = Vector3.Distance(slot.position, Input.mousePosition);
  297.             if (dist < minDistance)
  298.             {
  299.                 minDistance = dist;
  300.                 closestSlot = slot;
  301.             }
  302.         }
  303.  
  304.         return closestSlot;
  305.     }
  306.  
  307.     private void SwapItems(Transform newSlot, Transform draggedIcon)
  308.     {
  309.         if (newSlot.childCount > 0)
  310.         {
  311.             Transform existingIcon = newSlot.GetChild(0);
  312.             existingIcon.SetParent(originalSlot);
  313.             existingIcon.localPosition = Vector3.zero;
  314.  
  315.             Item existingItem = existingIcon.GetComponent<ItemReference>().item;
  316.             storageSystem.UpdateItemPosition(currentStorageItem, existingItem, originalSlot.GetSiblingIndex());
  317.         }
  318.  
  319.         draggedIcon.SetParent(newSlot);
  320.         draggedIcon.localPosition = Vector3.zero;
  321.  
  322.         storageSystem.UpdateItemPosition(currentStorageItem, draggedItem, newSlot.GetSiblingIndex());
  323.     }
  324.  
  325.     private void ReturnItemToOriginalSlot(Transform draggedIcon)
  326.     {
  327.         draggedIcon.SetParent(originalSlot);
  328.         draggedIcon.localPosition = Vector3.zero;
  329.     }
  330.  
  331.     public void ShowError(string message)
  332.     {
  333.         promptText.text = message;
  334.         yesButton.gameObject.SetActive(false);
  335.         noButton.gameObject.SetActive(false);
  336.         okButton.gameObject.SetActive(true);
  337.     }
  338.  
  339.     private void ResetPrompt()
  340.     {
  341.         promptText.text = currentStorageItem != null ? $"Open {currentStorageItem.itemName}?" : "";
  342.         yesButton.gameObject.SetActive(true);
  343.         noButton.gameObject.SetActive(true);
  344.         okButton.gameObject.SetActive(false);
  345.         inventoryPrompt.SetActive(false);
  346.         TogglePlayerControls(true);
  347.     }
  348.  
  349.     private void ClosePrompt()
  350.     {
  351.         inventoryPrompt.SetActive(false);
  352.         TogglePlayerControls(true);
  353.     }
  354.  
  355.     public void CloseInventory()
  356.     {
  357.         inventoryPanel.SetActive(false);
  358.         isInventoryOpen = false;
  359.         TogglePlayerControls(true);
  360.     }
  361.  
  362.     private void TogglePlayerControls(bool enable)
  363.     {
  364.         if (playerMovement != null)
  365.         {
  366.             playerMovement.enabled = enable;
  367.             playerMovement.GetComponent<MouseMovement>().enabled = enable;
  368.         }
  369.  
  370.         Cursor.lockState = enable ? CursorLockMode.Locked : CursorLockMode.None;
  371.         Cursor.visible = !enable;
  372.     }
  373.  
  374.     private int FindFirstEmptySlot()
  375.     {
  376.         for (int i = 0; i < inventorySlotParent.childCount - 3; i++)
  377.         {
  378.             if (inventorySlotParent.GetChild(i).childCount == 0)
  379.             {
  380.                 return i;
  381.             }
  382.         }
  383.         return -1;
  384.     }
  385. }
  386.  
  387. public class ItemReference : MonoBehaviour
  388. {
  389.     public Item item;
  390. }
  391.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement