using Managers; using Unity.VisualScripting; using UnityEngine; public class SlotComponent : MonoBehaviour { public int SlotId; public Slot.SlotType SlotType; private readonly PrefabManager prefabManager = PrefabManager.Instance; private ItemComponent itemComponent; protected GameObject itemPrefab; protected PlayerManager playerManager; protected SlotHandler slotHandler; private void Awake() { playerManager = GameObject.Find("PlayerManager").GetComponent(); // 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.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()); 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.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 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(); } public Slot.SlotType GetSlotType() { return SlotType; } public void AssignItemComponent(ItemComponent destinationItem) { if (destinationItem == null) { DeleteItem(); return; } AssignItem(destinationItem.GetItem()); /*itemComponent.transform.SetParent(transform, false); itemComponent.GetComponent().anchoredPosition = Vector2.zero; itemComponent.GetComponent().offsetMin = Vector2.zero; itemComponent.GetComponent().offsetMax = Vector2.zero;*/ } public void DeleteItem() { if (itemComponent == null) return; itemComponent.DeleteItem(); itemComponent = null; } public bool IsAllowedItemTypes(Slot.SlotType slotType, int slotId, GameItem.ItemType type) { var slot = playerManager.GetSlot(slotType, slotId); return slot.IsAllowedItemTypes(type); } }