using Data.ScriptableObjects.Items; using Handlers; using Managers.Item; using Types.Items; using Types.UI; using Unity.VisualScripting; using UnityEngine; using UnityEngine.UI; using ColorUtility = UnityEngine.ColorUtility; namespace Components.UI { public class SlotComponent : MonoBehaviour { [SerializeField] private ToolbarManager toolbarManager; private ItemComponent _itemComponent; [SerializeField] public GameSlot GameSlot; protected GameObject ItemPrefab; protected SlotHandler SlotHandler; private void Awake() { SlotHandler = gameObject.AddComponent(); SlotHandler.SetSlotComponent(this); if (SlotHandler == null) Debug.LogError("SlotHandler not found"); } // 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(GameItemSo 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.GetSlot().GetType() != GameSlot.GetType()) { 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.Clone()); // 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(GameSlot gameSlotDetails) { GameSlot = gameSlotDetails; if (gameSlotDetails.Item != null) AssignItem(gameSlotDetails.Item); if (gameSlotDetails.Item == null) DeleteItem(); // var button = transform.Find("WhiteBg").GetComponent