evershade/Assets/Scripts/Components/SlotComponent.cs

220 lines
6.6 KiB
C#

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>();
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<Component>()) 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<Component>();
foreach (var component in components) Debug.Log(component.GetType().Name);
}
public void ListAllComponentsInChildren()
{
var components = GetComponentsInChildren<Component>();
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<SlotComponent>();
var destinationSlot = GetComponent<SlotComponent>();
// 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>();
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<Button>();
// button.onClick.AddListener(slotHandler.TriggerOnClick);
Debug.Log("Slot details set, added handler.");
}
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<RectTransform>().anchoredPosition = Vector2.zero;
itemComponent.GetComponent<RectTransform>().offsetMin = Vector2.zero;
itemComponent.GetComponent<RectTransform>().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.Instance.GetSlot(slotType, slotId);
return slot.IsAllowedItemTypes(type);
}
public void SetSlotActive(bool isActive)
{
// change the bg color of the RawImage that is on the child of this slot called WhiteBg
var whiteBg = transform.Find("WhiteBg").GetComponent<RawImage>();
var originalColorHex = "#FFFFFF";
var selectedColorHex = "#D58220";
Color originalColor;
Color selectedColor;
// Try to convert the hex color strings to Color objects
if (!ColorUtility.TryParseHtmlString(originalColorHex, out originalColor))
{
Debug.LogError("Invalid color string: " + originalColorHex);
originalColor = Color.white; // default to white if the color string is invalid
}
if (!ColorUtility.TryParseHtmlString(selectedColorHex, out selectedColor))
{
Debug.LogError("Invalid color string: " + selectedColorHex);
selectedColor = Color.white; // default to white if the color string is invalid
}
// Set the color of the RawImage
whiteBg.color = isActive ? selectedColor : originalColor;
}
public void OnSlotClick()
{
ToolbarManager.Instance.SetActiveSlot(SlotId);
}
public void SetSlotType(Slot.SlotType type)
{
SlotType = type;
}
}