using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using UnityEngine.UI; public class PlayerInventoryUI : MonoBehaviour { [SerializeField] private GameObject uiSlotPrefab; [SerializeField] private GameObject[] uiSlots; //Slots for the quick inventory [SerializeField] private GameObject inventoryUIObject; [SerializeField] private GameObject slotsOptions; [SerializeField] private GameObject insertAmountPanelprefab; [SerializeField] private Inventory inventory, quickInventory; //Inventory settings [SerializeField] private float gridSpacingX, gridSpacingY; //the space between each tile of the matrix [SerializeField] private int columns = 5; // Number of columns in the inventory grid private int rows; public GameObject currentButtonOptions; //UI Inventory Matrix Size GameObject[,] inventorySlots = null; // Start is called before the first frame update void Start() { inventory = GameManager.Instance.PlayerController.InventoryManager.Inventory; quickInventory = GameManager.Instance.PlayerController.InventoryManager.QuickInventory; CreateInventoryGrid(); // Populate UI slots with initial data for (int i = 0; i < inventory.Capacity; i++) { UpdateGridUI(i); } for (int i = 0; i < quickInventory.Capacity; i++) { UpdateQuickInventoryUI(i); } inventory.OnSlotChanged += UpdateGridUI; inventory.OnSlotsChanged += UpdateGridUI; quickInventory.OnSlotChanged += UpdateQuickInventoryUI; quickInventory.OnSlotsChanged += UpdateQuickInventoryUI; } // Update is called once per frame void Update() { } public void DisplayGrid(bool enable) { for (int row = 0; row < rows; row++) { for (int col = 0; col < columns; col++) { inventorySlots[row, col].SetActive(enable); } } } //Creates an inventory grid private void CreateInventoryGrid() { // Get the inventory size from the player int inventorySize = inventory.Capacity; // Calculate number of rows needed based on the inventory size and number of columns rows = Mathf.CeilToInt((float)inventorySize / columns); // Initialize the 2D array based on rows and columns inventorySlots = new GameObject[rows, columns]; // Calculate total width and height of the grid float gridWidth = columns * gridSpacingX; float gridHeight = rows * gridSpacingY; // Calculate the starting position for centering the grid Vector3 startPosition = new Vector3(-gridWidth / 2 + gridSpacingX / 2, gridHeight / 2 - gridSpacingY / 2, 0); // Start drawing the inventory grid for (int row = 0; row < rows; row++) { for (int col = 0; col < columns; col++) { // Calculate the slot index (linear inventory index) int index = row * columns + col; // Ensure we don't create extra slots beyond the inventory size if (index >= inventorySize) break; // Calculate the position for each slot based on row, column, and spacing Vector3 position = startPosition + new Vector3(col * gridSpacingX, -row * gridSpacingY, 0); // Instantiate the slot at the calculated position GameObject slot = Instantiate(uiSlotPrefab, inventoryUIObject.transform); slot.GetComponent<RectTransform>().anchoredPosition = position; slot.SetActive(false); // Store the slot reference in the array inventorySlots[row, col] = slot; } } } private void UpdateQuickInventoryUI(int index) { InventorySlot slot = quickInventory.GetSlot(index); GameObject inventorySlot = uiSlots[index]; InventoryButton slotButton = inventorySlot.GetComponent<InventoryButton>(); slotButton.ChangeData(slot); } private void UpdateQuickInventoryUI(int indexA, int indexB) { InventorySlot slotA = quickInventory.GetSlot(indexA); InventorySlot slotB = quickInventory.GetSlot(indexB); GameObject inventorySlotA = uiSlots[indexA]; GameObject inventorySlotB = uiSlots[indexB]; InventoryButton slotButtonA = inventorySlotA.GetComponent<InventoryButton>(); InventoryButton slotButtonB = inventorySlotB.GetComponent<InventoryButton>(); //Updates the data of the slots slotButtonA.ChangeData(slotA); slotButtonB.ChangeData(slotB); } //Updates a single index private void UpdateGridUI(int index) { Tuple<int, int> gridIndex = CalculateIndexPosition(index); InventorySlot slot = inventory.GetSlot(index); GameObject inventorySlot = inventorySlots[gridIndex.Item1, gridIndex.Item2]; InventoryButton slotButton = inventorySlot.GetComponent<InventoryButton>(); slotButton.ChangeData(slot); } //Update 2 indexes //Used 2 items swap positions on the inventory private void UpdateGridUI(int indexA, int indexB) { //Calculates the index on the grid based on the index of the inventory Tuple<int, int> gridIndexA = CalculateIndexPosition(indexA); Tuple<int, int> gridIndexB = CalculateIndexPosition(indexB); //Get Inventory Data InventorySlot slotA = inventory.GetSlot(indexA); InventorySlot slotB = inventory.GetSlot(indexB); //Get the ui inventorySlots GameObject inventorySlotA = inventorySlots[gridIndexA.Item1, gridIndexA.Item2]; GameObject inventorySlotB = inventorySlots[gridIndexB.Item1, gridIndexB.Item2]; //Get the inventory button components InventoryButton slotButtonA = inventorySlotA.GetComponent<InventoryButton>(); InventoryButton slotButtonB = inventorySlotB.GetComponent<InventoryButton>(); //Change the data slotButtonA.ChangeData(slotA); slotButtonB.ChangeData(slotB); } //Calculates the index position private Tuple<int,int> CalculateIndexPosition(int index) { //Calculate the column int row = index / columns; int col = index % columns; return Tuple.Create(row, col); } }