Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections.Generic;
- 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 closeButton;
- public Transform inventorySlotParent;
- private InventorySystem inventorySystem;
- private Item currentStorageItem;
- private StorageSystem storageSystem;
- private void Awake()
- {
- if (Instance == null)
- {
- Instance = this;
- }
- else if (Instance != this)
- {
- gameObject.SetActive(false);
- return;
- }
- }
- private void Start()
- {
- inventorySystem = InventorySystem.Instance;
- storageSystem = StorageSystem.Instance;
- closeButton.onClick.AddListener(CloseInventory);
- }
- public void ShowInventoryPrompt(Item storageItem)
- {
- if (storageItem == null)
- {
- Debug.LogError("<color=red>Attempted to show inventory prompt for null storage item.</color>");
- return;
- }
- inventoryPrompt.SetActive(true);
- promptText.text = $"Open {storageItem.itemName}?";
- yesButton.onClick.RemoveAllListeners();
- yesButton.onClick.AddListener(() => OpenInventory(storageItem));
- noButton.onClick.RemoveAllListeners();
- noButton.onClick.AddListener(ClosePrompt);
- }
- private void OpenInventory(Item storageItem)
- {
- inventoryPrompt.SetActive(false);
- inventoryPanel.SetActive(true);
- currentStorageItem = storageItem;
- PopulateInventoryUI(storageItem);
- Debug.Log($"<color=green>Opened inventory for {storageItem.itemName}</color>");
- }
- private void PopulateInventoryUI(Item storageItem)
- {
- ClearInventoryUI();
- List<Item> storageInventory = storageSystem.GetStorageInventory(storageItem);
- for (int i = 0; i < inventorySlotParent.childCount; i++)
- {
- if (i < storageInventory.Count)
- {
- UpdateInventorySlot(inventorySlotParent.GetChild(i).gameObject, storageInventory[i]);
- }
- else
- {
- ClearInventorySlot(inventorySlotParent.GetChild(i).gameObject);
- }
- }
- }
- private void UpdateInventorySlot(GameObject slotObject, Item item)
- {
- Image iconImage = slotObject.GetComponentInChildren<Image>();
- iconImage.sprite = item.icon;
- iconImage.enabled = true;
- Button slotButton = slotObject.GetComponent<Button>();
- slotButton.onClick.RemoveAllListeners();
- slotButton.onClick.AddListener(() => EquipItemFromInventory(item));
- }
- private void ClearInventorySlot(GameObject slotObject)
- {
- Image iconImage = slotObject.GetComponentInChildren<Image>();
- iconImage.sprite = null;
- iconImage.enabled = false;
- Button slotButton = slotObject.GetComponent<Button>();
- slotButton.onClick.RemoveAllListeners();
- }
- private void EquipItemFromInventory(Item item)
- {
- if (inventorySystem.leftHandItem == null)
- {
- inventorySystem.EquipItem(item, true, item.isTwoHanded);
- }
- else if (inventorySystem.rightHandItem == null && !item.isTwoHanded)
- {
- inventorySystem.EquipItem(item, false, false);
- }
- else
- {
- Debug.Log("<color=orange>Cannot equip item. Hands are full.</color>");
- return;
- }
- inventorySystem.RemoveItemFromStorage(currentStorageItem, item);
- PopulateInventoryUI(currentStorageItem);
- Debug.Log($"<color=green>Equipped {item.itemName} from inventory</color>");
- }
- private void ClearInventoryUI()
- {
- foreach (Transform child in inventorySlotParent)
- {
- ClearInventorySlot(child.gameObject);
- }
- }
- private void ClosePrompt()
- {
- inventoryPrompt.SetActive(false);
- }
- private void CloseInventory()
- {
- inventoryPanel.SetActive(false);
- currentStorageItem = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement