91 lines
3.1 KiB
C#
91 lines
3.1 KiB
C#
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 _boxImage;
|
|
|
|
[SerializeField]
|
|
private Image _itemImage;
|
|
|
|
[SerializeField]
|
|
private TMP_Text _countText;
|
|
|
|
[Title("효과")]
|
|
[SerializeField]
|
|
private Color _craftableColor = Color.black;
|
|
|
|
[SerializeField]
|
|
private Color _uncraftableColor = Color.red;
|
|
|
|
[SerializeField]
|
|
private Sprite _craftableSprite;
|
|
|
|
[SerializeField]
|
|
private Sprite _uncraftableSprite;
|
|
|
|
[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()
|
|
{
|
|
if (!DataManager.Instance || DataManager.Instance.Inventory == null) return;
|
|
|
|
DataManager.Instance.Inventory.OnChangedInventoryItemSlot -= UpdateIngredients;
|
|
}
|
|
|
|
public void UpdateIngredients(CraftingIngredient craftingIngredient)
|
|
{
|
|
_currentCraftingIngredient = craftingIngredient;
|
|
ItemData itemData = ItemManager.Instance.ItemDataSo.GetDataByIdx(_currentCraftingIngredient.Idx);
|
|
_itemImage.sprite = itemData.Sprite;
|
|
|
|
int inventoryCount = DataManager.Instance.Inventory.GetItemByIdx(itemData.Idx)?.Count ?? 0;
|
|
int requiredCount = _currentCraftingIngredient.Count;
|
|
_countText.text = requiredCount.ToString();
|
|
|
|
IsCraftable = inventoryCount >= requiredCount;
|
|
|
|
_boxImage.sprite = IsCraftable ? _craftableSprite : _uncraftableSprite;
|
|
_countText.color = IsCraftable ? _craftableColor : _uncraftableColor;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 인벤토리 이벤트용 재료 업데이트 함수
|
|
/// </summary>
|
|
/// <param name="inventoryItemSlot"></param>
|
|
/// <param name="addOrRemove"></param>
|
|
private void UpdateIngredients(InventoryItemSlot inventoryItemSlot, bool addOrRemove)
|
|
{
|
|
UpdateIngredients(_currentCraftingIngredient);
|
|
}
|
|
|
|
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 = requiredCount.ToString();
|
|
}
|
|
|
|
public void ShowUi() => gameObject.SetActive(true);
|
|
public void HideUi() => gameObject.SetActive(false);
|
|
}
|
|
} |