using DDD.Managers; using DDD.ScriptableObjects; using Sirenix.OdinInspector; using TMPro; using UnityEngine; using UnityEngine.UI; namespace DDD.Uis.Tycoon { public class SelectedCraftRecipeIngredient : MonoBehaviour { [Title("컴포넌트")] [SerializeField] private Image _image; [SerializeField] private TMP_Text _nameText; [SerializeField] private TMP_Text _countText; [Title("효과")] [SerializeField] private Color _craftableColor = Color.white; [SerializeField] private Color _uncraftableColor = Color.red; [field: Title("실시간 데이터")] [field: SerializeField] public bool IsCraftable { get; private set; } [SerializeField] private CraftingIngredient _currentCraftingIngredient; private void Start() { DataManager.Instance.Inventory.OnChangedInventoryItemSlot += UpdateIngredients; } private void OnDestroy() { DataManager.Instance.Inventory.OnChangedInventoryItemSlot -= UpdateIngredients; } public void UpdateIngredients(CraftingIngredient craftingIngredient) { _currentCraftingIngredient = craftingIngredient; ItemData itemData = ItemManager.Instance.ItemDataSo.GetDataByIdx(_currentCraftingIngredient.Idx); _image.sprite = itemData.Sprite; _nameText.text = itemData.Name; int inventoryCount = DataManager.Instance.Inventory.GetItemByIdx(itemData.Idx)?.Count ?? 0; int requiredCount = _currentCraftingIngredient.Count; _countText.text = $"{inventoryCount}/{requiredCount}"; IsCraftable = inventoryCount >= requiredCount; _countText.color = IsCraftable ? _craftableColor : _uncraftableColor; } /// /// 인벤토리 이벤트용 재료 업데이트 함수 /// /// /// private void UpdateIngredients(InventoryItemSlot inventoryItemSlot, bool addOrRemove) { ItemData itemData = ItemManager.Instance.ItemDataSo.GetDataByIdx(_currentCraftingIngredient.Idx); _image.sprite = itemData.Sprite; _nameText.text = itemData.Name; int inventoryCount = DataManager.Instance.Inventory.GetItemByIdx(itemData.Idx)?.Count ?? 0; int requiredCount = _currentCraftingIngredient.Count; _countText.text = $"{inventoryCount}/{requiredCount}"; IsCraftable = inventoryCount >= requiredCount; _countText.color = IsCraftable ? _craftableColor : _uncraftableColor; } public void UpdateCraftableCount(int craftableCount) { ItemData itemData = ItemManager.Instance.ItemDataSo.GetDataByIdx(_currentCraftingIngredient.Idx); int inventoryCount = DataManager.Instance.Inventory.GetItemByIdx(itemData.Idx)?.Count ?? 0; int requiredCount = _currentCraftingIngredient.Count * craftableCount; _countText.text = $"{inventoryCount}/{requiredCount}"; } public void ShowUi() => gameObject.SetActive(true); public void HideUi() => gameObject.SetActive(false); } }