using Managers; using Unity.VisualScripting; using UnityEngine; using UnityEngine.UI; using ColorUtility = UnityEngine.ColorUtility; public class SlotComponent : MonoBehaviour { public int SlotId; public Slot.SlotType SlotType; private ItemComponent itemComponent; protected GameObject itemPrefab; protected SlotHandler slotHandler; private void Awake() { // PrintHierarchy(gameObject); slotHandler = gameObject.AddComponent(); slotHandler.SetSlotComponent(this); // ListAllComponents(); // ListAllComponentsInChildren(); if (slotHandler == null) Debug.LogError("SlotHandler not found"); } public void SetSlotId(int id) { SlotId = id; } public void PrintHierarchy(GameObject obj, string indent = "") { // Print the name of the current GameObject Debug.Log(indent + obj.name); // Print the names of all components attached to the current GameObject foreach (var component in obj.GetComponents()) Debug.Log(indent + " - " + component.GetType().Name); // Recursively print the hierarchy for each child of the current GameObject foreach (Transform child in obj.transform) PrintHierarchy(child.gameObject, indent + " "); } public void ListAllComponents() { var components = GetComponents(); foreach (var component in components) Debug.Log(component.GetType().Name); } public void ListAllComponentsInChildren() { var components = GetComponentsInChildren(); foreach (var component in components) Debug.Log(component.GetType().Name); } // use slot type to choose manager to get slot from protected void SwapItems(GameItem draggedItemHandler) { // Get parent slot of the dragged item var draggedSlot = draggedItemHandler.GetComponentInParent(); var destinationSlot = GetComponent(); // we need to check the slot type of the dragged item to see if it can be placed in this slot if (draggedSlot.SlotType != SlotType) { Debug.Log("Cannot place item in this slot"); return; } PlayerManager.Instance.SwapItems(draggedSlot.SlotType, draggedSlot.SlotId, SlotType, SlotId); var tempItem = destinationSlot; // assigns to the destination slot the dragged item AssignItem(draggedItemHandler); // If this slot had an item, move it to the dragged item's original slot if (destinationSlot != null) draggedSlot.AssignItem(destinationSlot.GetItem()); // todo: UIManager.Instance.InventoryUIController.UpdateUI(); else Destroy(draggedItemHandler); } public void AssignItem(GameItem newItem) { // Remove any existing ItemPrefab(Clone) from this slot DeleteItem(); if (itemPrefab == null) // Debug.LogError("ItemPrefab not set"); // return; itemPrefab = PrefabManager.Instance.GetItemPrefab(); var newItemInstance = Instantiate(itemPrefab, transform); itemComponent = newItemInstance.GetComponent(); itemComponent.SetItem(newItem); } public GameItem GetItem() { if (itemComponent == null) return null; return itemComponent.GetItem(); } public ItemComponent GetItemComponent() { Debug.Log("Getting item component"); return itemComponent; } public ItemHandler GetItemHandler() { return itemComponent.itemHandler; } public int GetSlotId() { return SlotId; } public void DeleteSlot() { if (itemComponent == null) return; Destroy(itemComponent.transform.parent.gameObject); // Destroy(itemComponent.itemHandler); // Destroy(itemComponent); itemComponent = null; } public void SetSlotDetails(Slot slotDetails) { SlotId = slotDetails.slotId; SlotType = slotDetails.slotType; if (slotDetails.item != null) AssignItem(slotDetails.item); if (slotDetails.item == null) DeleteItem(); // Addig onClick listener var button = transform.Find("WhiteBg").GetComponent