215 lines
6.5 KiB
C#
215 lines
6.5 KiB
C#
using System.Collections.Generic;
|
|
using DDD.ScriptableObjects;
|
|
using Sirenix.OdinInspector;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
|
|
namespace DDD.Uis.Tycoon
|
|
{
|
|
public class AddIngredientUi : PopupUi
|
|
{
|
|
[Title("프리팹")]
|
|
[SerializeField]
|
|
private SelectedCraftRecipeIngredient _selectedCraftRecipeIngredientPrefab;
|
|
|
|
[Title("컴포넌트")]
|
|
[SerializeField]
|
|
private GameObject _panel;
|
|
|
|
[SerializeField]
|
|
private RectTransform _panelRect;
|
|
|
|
[SerializeField]
|
|
private GameObject _ingredientsLocation;
|
|
|
|
[SerializeField]
|
|
private TMP_Text _countText;
|
|
|
|
[SerializeField]
|
|
private Image _image;
|
|
|
|
[SerializeField]
|
|
private TMP_Text _nameText;
|
|
|
|
[SerializeField]
|
|
private Button _confirmButton;
|
|
|
|
private List<SelectedCraftRecipeIngredient> _ingredients = new(3);
|
|
public CraftRecipeData CurrentCraftRecipeData { get; private set; }
|
|
public int CraftableCount { get; private set; }
|
|
|
|
private UnityAction _clickAction;
|
|
|
|
private void Awake()
|
|
{
|
|
foreach (Transform element in _ingredientsLocation.transform)
|
|
{
|
|
Destroy(element.gameObject);
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
for (int i = 0; i < 3; i++)
|
|
{
|
|
SelectedCraftRecipeIngredient selectedCraftRecipeIngredient = Instantiate(_selectedCraftRecipeIngredientPrefab, _ingredientsLocation.transform);
|
|
_ingredients.Add(selectedCraftRecipeIngredient);
|
|
selectedCraftRecipeIngredient.HideUi();
|
|
}
|
|
|
|
_panel.SetActive(false);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
UnregisterButtonListener();
|
|
}
|
|
|
|
public override void Open()
|
|
{
|
|
base.Open();
|
|
_panel.SetActive(true);
|
|
}
|
|
|
|
public override void Close()
|
|
{
|
|
for (int i = 0; i < 3; i++)
|
|
{
|
|
_ingredients[i].HideUi();
|
|
}
|
|
|
|
_panel.SetActive(false);
|
|
base.Close();
|
|
}
|
|
|
|
public void RegisterButtonListener(UnityAction clickAction)
|
|
{
|
|
_clickAction = clickAction;
|
|
_confirmButton.onClick.AddListener(_clickAction);
|
|
}
|
|
|
|
private void UnregisterButtonListener()
|
|
{
|
|
if (_clickAction == null) return;
|
|
|
|
_confirmButton.onClick.RemoveListener(_clickAction);
|
|
_clickAction = null;
|
|
}
|
|
|
|
public void OnClicked(CraftRecipeData craftRecipeData)
|
|
{
|
|
CurrentCraftRecipeData = craftRecipeData;
|
|
|
|
CraftableCount = 1;
|
|
_countText.text = CraftableCount.ToString();
|
|
_nameText.text = CurrentCraftRecipeData.Name;
|
|
_image.sprite = CurrentCraftRecipeData.Sprite;
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
{
|
|
if (i < CurrentCraftRecipeData.ValidIngredients.Count)
|
|
{
|
|
_ingredients[i].UpdateIngredients(CurrentCraftRecipeData.ValidIngredients[i]);
|
|
_ingredients[i].ShowUi();
|
|
}
|
|
else
|
|
{
|
|
_ingredients[i].HideUi();
|
|
}
|
|
}
|
|
|
|
Open();
|
|
|
|
// content size fitter 즉시 적용
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(_panelRect);
|
|
}
|
|
|
|
public void MaxButton()
|
|
{
|
|
if (CurrentCraftRecipeData == null)
|
|
{
|
|
Debug.LogWarning("현재 선택된 레시피가 없습니다.");
|
|
return;
|
|
}
|
|
|
|
int maxCraftCount = int.MaxValue;
|
|
foreach (CraftingIngredient craftingIngredient in CurrentCraftRecipeData.ValidIngredients)
|
|
{
|
|
var slot = DataManager.Instance.Inventory.GetItemByIdx(craftingIngredient.Idx);
|
|
int available = slot != null ? slot.Count : 0;
|
|
|
|
int possible = available / craftingIngredient.Count;
|
|
if (possible < maxCraftCount)
|
|
{
|
|
maxCraftCount = possible;
|
|
}
|
|
}
|
|
|
|
CraftableCount = maxCraftCount;
|
|
_countText.text = CraftableCount.ToString();
|
|
|
|
for (int i = 0; i < CurrentCraftRecipeData.ValidIngredients.Count; i++)
|
|
{
|
|
_ingredients[i].UpdateCraftableCount(CraftableCount);
|
|
}
|
|
}
|
|
|
|
public void IncreaseButton()
|
|
{
|
|
if (CurrentCraftRecipeData == null)
|
|
{
|
|
Debug.LogWarning("현재 선택된 레시피가 없습니다.");
|
|
return;
|
|
}
|
|
|
|
int newCount = CraftableCount + 1;
|
|
foreach (var ingredient in CurrentCraftRecipeData.ValidIngredients)
|
|
{
|
|
var slot = DataManager.Instance.Inventory.GetItemByIdx(ingredient.Idx);
|
|
int available = slot != null ? slot.Count : 0;
|
|
|
|
if (available < newCount * ingredient.Count) return;
|
|
}
|
|
|
|
CraftableCount = newCount;
|
|
_countText.text = CraftableCount.ToString();
|
|
|
|
for (int i = 0; i < CurrentCraftRecipeData.ValidIngredients.Count; i++)
|
|
{
|
|
_ingredients[i].UpdateCraftableCount(CraftableCount);
|
|
}
|
|
}
|
|
|
|
public void DecreaseButton()
|
|
{
|
|
if (CraftableCount <= 1) return;
|
|
|
|
CraftableCount--;
|
|
_countText.text = CraftableCount.ToString();
|
|
|
|
for (int i = 0; i < CurrentCraftRecipeData.ValidIngredients.Count; i++)
|
|
{
|
|
_ingredients[i].UpdateCraftableCount(CraftableCount);
|
|
}
|
|
}
|
|
|
|
public void AddMenuButton()
|
|
{
|
|
if (CurrentCraftRecipeData == null)
|
|
{
|
|
Debug.LogWarning("현재 선택된 레시피가 없습니다.");
|
|
return;
|
|
}
|
|
|
|
foreach (var ingredient in CurrentCraftRecipeData.ValidIngredients)
|
|
{
|
|
var slot = DataManager.Instance.Inventory.GetItemByIdx(ingredient.Idx);
|
|
DataManager.Instance.Inventory.RemoveItem(slot, CraftableCount * ingredient.Count);
|
|
}
|
|
|
|
Close();
|
|
}
|
|
}
|
|
} |