CapersProject/Assets/02.Scripts/Ui/Tycoon/BrewingIngredientSlotUi.cs
2024-09-12 16:36:24 +09:00

70 lines
2.2 KiB
C#

using System;
using BlueWater.Items;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace BlueWater.Uis
{
[Serializable]
public class BrewingIngredientSlotUi : MonoBehaviour
{
[SerializeField]
private Image _image;
[SerializeField]
private TMP_Text _quantity;
[SerializeField]
private Color _enoughColor = Color.black;
[SerializeField]
private Color _notEnoughColor = Color.red;
private string _ingredientIdx;
private int _inventoryQuantity;
private int _needQuantity;
private bool _isEnough;
/// <summary>
/// 양조장에서 레시피를 선택했을 때, 재료를 설정하는 함수
/// </summary>
/// <param name="ingredientIdx">재료의 Idx</param>
/// <param name="quantity">재료 요구량</param>
public void SetIngredient(string ingredientIdx, int quantity)
{
_ingredientIdx = ingredientIdx;
_needQuantity = quantity;
var ingredientItemData = ItemManager.Instance.ItemDataSo.GetDataByIdx(_ingredientIdx);
_image.sprite = ingredientItemData.Sprite;
_inventoryQuantity = DataManager.Instance.Inventory.GetItemByIdx(_ingredientIdx).Quantity;
SetQuantity();
}
/// <summary>
/// 재료 수량 변경하는 함수
/// 여러개를 제작하는 경우 multiply값을 조정
/// </summary>
/// <param name="multiply">제조하는 개수</param>
public void SetQuantity(int multiply = 1)
{
var finalNeedQuantity = _needQuantity * multiply;
_quantity.text = $"{_inventoryQuantity}/{finalNeedQuantity}";
_isEnough = _inventoryQuantity >= finalNeedQuantity;
_quantity.color = _isEnough ? _enoughColor : _notEnoughColor;
}
/// <summary>
/// 현재 인벤토리의 재료로 만들 수 있는 최대 개수 반환 함수
/// </summary>
/// <returns></returns>
public int GetMaxQuantity()
{
if (_needQuantity == 0) return 1;
return _inventoryQuantity / _needQuantity;
}
public bool GetIsEnough() => _isEnough;
}
}