evershade/Assets/Scripts/Components/UI/SlotComponent.cs

209 lines
6.8 KiB
C#

using Data.ScriptableObjects.Items;
using Handlers;
using Managers.Item;
using Types.Items;
using Types.UI;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using Utils;
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>();
SlotHandler.SetSlotComponent(this);
if (SlotHandler == null) GameLogger.LogError("SlotHandler not found");
}
// public void PrintHierarchy(GameObject obj, string indent = "")
// {
// // Print the name of the current GameObject
// GameLogger.Log(indent + obj.name);
//
// // Print the names of all components attached to the current GameObject
// foreach (var component in obj.GetComponents<Component>()) GameLogger.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) GameLogger.Log(component.GetType().Name);
}
public void ListAllComponentsInChildren()
{
var components = GetComponentsInChildren<Component>();
foreach (var component in components) GameLogger.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<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.GetSlot().GetType() != GameSlot.GetType())
{
GameLogger.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)
// GameLogger.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()
{
GameLogger.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<Button>();
GameLogger.Log("Slot details set, added handler.");
}
public GameSlot GetSlot()
{
return GameSlot;
}
public void AssignItemComponent(ItemComponent destinationItem)
{
if (destinationItem == null)
{
DeleteItem();
return;
}
AssignItem(destinationItem.GetItem());
}
public void DeleteItem()
{
if (_itemComponent == null) return;
_itemComponent.DeleteItem();
_itemComponent = null;
}
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))
{
GameLogger.LogError("Invalid color string: " + originalColorHex);
originalColor = Color.white; // default to white if the color string is invalid
}
if (!ColorUtility.TryParseHtmlString(selectedColorHex, out selectedColor))
{
GameLogger.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.SetActiveSlot(GameSlot.GetSlotId());
}
public void SetSlotType(SlotType type)
{
GameSlot.SetSlotType(type);
}
public bool IsAllowedItemTypes(ItemType type)
{
return GameSlot.IsAllowedItemTypes(type);
}
}
}