CapersProject/Assets/02.Scripts/DDD/Ui/Tycoon/SelectedCraftRecipe.cs
2025-02-18 06:47:56 +09:00

122 lines
3.9 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 SelectedCraftRecipe : MonoBehaviour
{
[Title("프리팹")]
[SerializeField]
private SelectedCraftRecipeIngredient _selectedCraftRecipeIngredient;
[Title("컴포넌트")]
[SerializeField]
private TMP_Text _nameText;
[SerializeField]
private Image _image;
[SerializeField]
private TMP_Text _priceText;
[SerializeField]
private TMP_Text _descriptionText;
[SerializeField]
private GameObject _selectedCraftRecipeIngredientsLocation;
[SerializeField]
private Button _addIngredientButton;
[field: Title("실시간 데이터")]
[field: SerializeField]
public bool IsCraftable { get; private set; }
[field: SerializeField]
public CraftRecipeData CurrentCraftRecipeData { get; set; }
private List<SelectedCraftRecipeIngredient> _selectedCraftRecipeIngredients;
private UnityAction _clickAction;
private void Awake()
{
foreach (Transform element in _selectedCraftRecipeIngredientsLocation.transform)
{
Destroy(element.gameObject);
}
}
private void Start()
{
_selectedCraftRecipeIngredients = new List<SelectedCraftRecipeIngredient>(3);
for (int i = 0; i < 3; i++)
{
SelectedCraftRecipeIngredient selectedCraftRecipeIngredient = Instantiate(_selectedCraftRecipeIngredient, _selectedCraftRecipeIngredientsLocation.transform);
_selectedCraftRecipeIngredients.Add(selectedCraftRecipeIngredient);
selectedCraftRecipeIngredient.gameObject.SetActive(false);
}
}
private void OnDestroy()
{
UnregisterButtonListener();
}
public void RegisterButtonListener(UnityAction clickAction)
{
_clickAction = clickAction;
_addIngredientButton.onClick.AddListener(_clickAction);
}
private void UnregisterButtonListener()
{
if (_clickAction == null) return;
_addIngredientButton.onClick.RemoveListener(_clickAction);
_clickAction = null;
}
public void OnSelected(CraftRecipeData selectedCraftRecipeData)
{
CurrentCraftRecipeData = selectedCraftRecipeData;
_nameText.text = CurrentCraftRecipeData.Name;
_image.sprite = CurrentCraftRecipeData.Sprite;
_priceText.text = CurrentCraftRecipeData.Price.ToString();
_descriptionText.text = CurrentCraftRecipeData.Description;
IsCraftable = true;
for (int i = 0; i < 3; i++)
{
if (i < CurrentCraftRecipeData.ValidIngredients.Count)
{
_selectedCraftRecipeIngredients[i].UpdateIngredients(CurrentCraftRecipeData.ValidIngredients[i]);
_selectedCraftRecipeIngredients[i].ShowUi();
if (!_selectedCraftRecipeIngredients[i].IsCraftable)
{
IsCraftable = false;
}
}
else
{
_selectedCraftRecipeIngredients[i].HideUi();
}
}
}
public void OnAddIngredient(CraftRecipeData selectedCraftRecipeData)
{
}
public void EnableInteractableAddIngredientButton() => _addIngredientButton.interactable = true;
public void DisableInteractableAddIngredientButton() => _addIngredientButton.interactable = false;
}
}