80 lines
2.2 KiB
C#
80 lines
2.2 KiB
C#
using DDD.ScriptableObjects;
|
|
using Sirenix.OdinInspector;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
|
|
namespace DDD.Uis.Tycoon
|
|
{
|
|
public class CraftRecipeButton : MonoBehaviour
|
|
{
|
|
[Title("컴포넌트")]
|
|
[SerializeField]
|
|
private Button _button;
|
|
|
|
[SerializeField]
|
|
private Image _image;
|
|
|
|
[field: Title("레시피 데이터")]
|
|
[field: SerializeField]
|
|
public CraftRecipeData CraftRecipeData { get; private set; }
|
|
|
|
private UnityAction _clickAction;
|
|
|
|
private void OnDestroy()
|
|
{
|
|
UnregisterButtonListener();
|
|
}
|
|
|
|
public void RegisterButtonListener(UnityAction clickAction)
|
|
{
|
|
_clickAction = clickAction;
|
|
_button.onClick.AddListener(_clickAction);
|
|
}
|
|
|
|
private void UnregisterButtonListener()
|
|
{
|
|
if (_clickAction == null) return;
|
|
|
|
_button.onClick.RemoveListener(_clickAction);
|
|
_clickAction = null;
|
|
}
|
|
|
|
public void SetCraftRecipeData(CraftRecipeData craftRecipeData)
|
|
{
|
|
if (craftRecipeData != null)
|
|
{
|
|
CraftRecipeData = craftRecipeData;
|
|
_image.sprite = CraftRecipeData.Sprite;
|
|
UpdateIngredients();
|
|
}
|
|
}
|
|
|
|
public void OnClicked()
|
|
{
|
|
_button.onClick.Invoke();
|
|
}
|
|
|
|
private void UpdateIngredients()
|
|
{
|
|
int maxCraftingCount = int.MaxValue;
|
|
foreach (CraftingIngredient craftingIngredient in CraftRecipeData.ValidIngredients)
|
|
{
|
|
string ingredientIdx = craftingIngredient.Idx;
|
|
int requiredCount = craftingIngredient.Count;
|
|
int inventoryCount = DataManager.Instance.Inventory.GetItemByIdx(ingredientIdx)?.Count ?? 0;
|
|
int craftingCount = inventoryCount / requiredCount;
|
|
|
|
if (maxCraftingCount > craftingCount)
|
|
{
|
|
maxCraftingCount = craftingCount;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ShowUi() => gameObject.SetActive(true);
|
|
public void HideUi() => gameObject.SetActive(false);
|
|
}
|
|
}
|