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;
- public TextMeshProUGUI inventoryTitleText;
- [SerializeField] private GameObject itemIconPrefab;
- private InventorySystem inventorySystem;
- private StorageSystem storageSystem;
- private InteractionSystem interactionSystem;
- private PlayerMovement playerMovement;
- private MouseMovement mouseMovement;
- 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>();
- mouseMovement = FindObjectOfType<MouseMovement>();
- if (closeButton != null) closeButton.onClick.AddListener(CloseInventory);
- if (okButton != null)
- {
- okButton.onClick.AddListener(ClosePrompt);
- okButton.gameObject.SetActive(false);
- }
- if (yesButton != null) { yesButton.onClick.AddListener(() => HandleStoragePrompt(true)); }
- if (noButton != null) noButton.onClick.AddListener(() => HandleStoragePrompt(false));
- if (inventoryDoneButton != null) inventoryDoneButton.onClick.AddListener(CloseInventory);
- }
- public void ShowInventoryPrompt(Item storageItem)
- {
- currentStorageItem = storageItem;
- inventoryPrompt.SetActive(true);
- promptText.text = storageItem != null ? $"Open {storageItem.itemName}?" : "";
- yesButton.gameObject.SetActive(true);
- noButton.gameObject.SetActive(true);
- okButton.gameObject.SetActive(false);
- TogglePlayerControls(false);
- }
- public void HandleStoragePrompt(bool accepted)
- {
- if (accepted && currentStorageItem != null)
- {
- OpenInventory(currentStorageItem); // Open inventory for the selected storage item
- }
- else
- {
- ClosePrompt(); // Close the prompt if "No" is selected
- }
- }
- public void UpdatePromptText(string message)
- {
- if (promptText != null)
- {
- promptText.text = message;
- }
- }
- public void ShowOkayButton()
- {
- if (okButton != null)
- {
- okButton.gameObject.SetActive(true);
- yesButton.gameObject.SetActive(false);
- noButton.gameObject.SetActive(false);
- }
- }
- public void OpenInventory(Item storageItem)
- {
- inventoryPrompt.SetActive(false);
- inventoryPanel.SetActive(true);
- currentStorageItem = storageItem;
- isInventoryOpen = true;
- TransferHeldItemsToStorage();
- PopulateInventoryUI(storageItem);
- TogglePlayerControls(false);
- if (inventoryTitleText != null)
- {
- inventoryTitleText.text = storageItem.itemName;
- }
- }
- 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();
- }
- }
- }
- public void PopulateInventoryUI(Item storageItem)
- {
- UIHelperFunctions.Instance.PopulateInventoryUI(storageItem, inventorySlotParent, storageSystem, this);
- }
- public void UpdateInventorySlot(GameObject slotObject, Item item)
- {
- UIHelperFunctions.Instance.UpdateInventorySlot(slotObject, item, itemIconPrefab, this);
- }
- public void ShowItemDetails(Item item)
- {
- UIHelperFunctions.Instance.ShowItemDetails(item, itemDetailsPanel, itemTitleText, itemDescriptionText, itemUsesText);
- }
- public void HideItemDetails()
- {
- UIHelperFunctions.Instance.HideItemDetails(itemDetailsPanel);
- }
- public void TryEquipItem(Item item)
- {
- UIHelperFunctions.Instance.TryEquipItem(item, inventorySystem, storageSystem, currentStorageItem, interactionSystem, playerMovement, this);
- }
- public void BeginDrag(Transform iconTransform)
- {
- UIHelperFunctions.Instance.BeginDrag(iconTransform, ref draggedItem, ref originalSlot, inventoryPanel);
- }
- public void EndDrag(Transform iconTransform)
- {
- UIHelperFunctions.Instance.EndDrag(iconTransform, draggedItem, originalSlot, inventorySlotParent, currentStorageItem, storageSystem, this);
- draggedItem = null;
- }
- public void ShowError(string message)
- {
- promptText.text = message;
- yesButton.gameObject.SetActive(false);
- noButton.gameObject.SetActive(false);
- okButton.gameObject.SetActive(true);
- }
- public void CloseInventory()
- {
- inventoryPanel.SetActive(false);
- isInventoryOpen = false;
- TogglePlayerControls(true);
- }
- private void TogglePlayerControls(bool enable)
- {
- if (playerMovement != null) playerMovement.enabled = enable;
- if (mouseMovement != null) mouseMovement.enabled = enable;
- SetCursorState(!enable);
- }
- public void ShowBackpackPrompt(Item backpackItem)
- {
- currentStorageItem = backpackItem;
- inventoryPrompt.SetActive(true);
- promptText.text = "Would you like to wear the backpack?";
- yesButton.gameObject.SetActive(true);
- noButton.gameObject.SetActive(true);
- okButton.gameObject.SetActive(false);
- SetCursorState(true);
- FreezePlayerMovement(true);
- }
- public void HandleBackpackPrompt(bool accepted)
- {
- if (accepted)
- {
- interactionSystem.WearBackpack();
- }
- else
- {
- UpdateBackpackPrompt();
- }
- }
- public void UpdateBackpackPrompt()
- {
- promptText.text = "Okay, if you change your mind press E to equip the backpack";
- yesButton.gameObject.SetActive(false);
- noButton.gameObject.SetActive(false);
- okButton.gameObject.SetActive(true);
- SetCursorState(true);
- FreezePlayerMovement(true);
- }
- public void ClosePrompt()
- {
- inventoryPrompt.SetActive(false);
- SetCursorState(false);
- FreezePlayerMovement(false);
- currentStorageItem = null;
- }
- private void SetCursorState(bool visible)
- {
- Cursor.visible = visible;
- Cursor.lockState = visible ? CursorLockMode.None : CursorLockMode.Locked;
- }
- private void FreezePlayerMovement(bool freeze)
- {
- if (playerMovement != null) playerMovement.enabled = !freeze;
- if (mouseMovement != null) mouseMovement.enabled = !freeze;
- }
- }
- public class ItemReference : MonoBehaviour
- {
- public Item item;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement