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

221 lines
7.8 KiB
C#

using System.Collections.Generic;
using DDD.Managers;
using DDD.ScriptableObjects;
using Sirenix.OdinInspector;
using UnityEngine;
namespace DDD.Uis.Tycoon
{
public class CookMenuUi : PopupUi
{
[Title("프리팹")]
[SerializeField]
private TodayMenuButton _todayMenuButtonPrefab;
[SerializeField]
private CraftRecipeButton _craftRecipeButtonPrefab;
[Title("컴포넌트")]
[SerializeField]
private GameObject _panel;
[SerializeField]
private GameObject _todayMenus;
[SerializeField]
private GameObject _unlockedCraftRecipes;
[Title("클래스")]
[SerializeField]
private SelectedCraftRecipe _selectedCraftRecipe;
[SerializeField]
private AddIngredientUi _addIngredientUi;
[Title("오늘의 메뉴")]
[SerializeField]
private int _maxTodayMenuCount = 6;
[Title("실시간 데이터")]
[SerializeField]
private List<TodayMenuButton> _todayMenuButtons;
[SerializeField]
private List<CraftRecipeButton> _craftRecipeButtons;
[SerializeField]
private TodayMenuButton _currentTodayMenuButton;
[SerializeField]
private CraftRecipeButton _currentCraftRecipeButton;
private void Awake()
{
// 더미 데이터 삭제
foreach (Transform element in _todayMenus.transform)
{
if (element)
{
Destroy(element.gameObject);
}
}
foreach (Transform element in _unlockedCraftRecipes.transform)
{
if (element)
{
Destroy(element.gameObject);
}
}
}
private void Start()
{
_todayMenuButtons = new List<TodayMenuButton>(_maxTodayMenuCount);
for (int i = 0; i < _maxTodayMenuCount; i++)
{
TodayMenuButton todayMenuButton = Instantiate(_todayMenuButtonPrefab, _todayMenus.transform);
todayMenuButton.RegisterButtonListener(() => OnClickedTodayMenu(todayMenuButton));
_todayMenuButtons.Add(todayMenuButton);
}
_craftRecipeButtons = new List<CraftRecipeButton>(ItemManager.Instance.CraftRecipeDataSo.GetDataCount());
foreach (CraftRecipeData craftRecipeData in ItemManager.Instance.AcquiredCraftRecipeDatas.Values)
{
CraftRecipeButton craftRecipeButton = Instantiate(_craftRecipeButtonPrefab, _unlockedCraftRecipes.transform);
craftRecipeButton.RegisterButtonListener(() => OnClickedCraftRecipe(craftRecipeData));
craftRecipeButton.SetCraftRecipeData(craftRecipeData);
_craftRecipeButtons.Add(craftRecipeButton);
}
_selectedCraftRecipe.RegisterButtonListener(() => OnClickedAddIngredient(_selectedCraftRecipe.CurrentCraftRecipeData));
_addIngredientUi.RegisterButtonListener(() => OnAddedTodayMenu());
_panel.SetActive(false);
}
public override void Open()
{
if (_todayMenuButtons.Count > 0)
{
_currentTodayMenuButton = _todayMenuButtons[0];
}
base.Open();
_panel.SetActive(true);
if (_craftRecipeButtons.Count > 0)
{
UpdateCraftRecipeButton();
}
}
public override void Close()
{
_panel.SetActive(false);
base.Close();
}
private void OnClickedTodayMenu(TodayMenuButton clickedTodayMenuButton)
{
_currentTodayMenuButton = clickedTodayMenuButton;
UpdateAddIngredientButton();
UpdateCraftRecipeButton();
}
private void OnAddedTodayMenu()
{
_currentTodayMenuButton.AddedMenu(_addIngredientUi.CurrentCraftRecipeData, _addIngredientUi.CraftableCount);
UpdateAddIngredientButton();
UpdateCraftRecipeButton();
}
private void OnClickedCraftRecipe(CraftRecipeData craftRecipeData)
{
_selectedCraftRecipe.OnSelected(craftRecipeData);
UpdateAddIngredientButton();
}
private void OnClickedAddIngredient(CraftRecipeData craftRecipeData)
{
_addIngredientUi.OnClicked(craftRecipeData);
}
private void UpdateCraftRecipeButton()
{
_currentCraftRecipeButton = null;
foreach (CraftRecipeButton craftRecipeButton in _craftRecipeButtons)
{
if (craftRecipeButton.gameObject.activeInHierarchy)
{
_currentCraftRecipeButton = craftRecipeButton;
break;
}
}
_currentCraftRecipeButton?.OnClicked();
}
private void UpdateAddIngredientButton()
{
if (_currentTodayMenuButton.IsAddedMenu)
{
if (_currentTodayMenuButton.AddedCraftRecipeData.Idx == _selectedCraftRecipe.CurrentCraftRecipeData.Idx
&& _selectedCraftRecipe.IsCraftable)
{
_selectedCraftRecipe.EnableInteractableAddIngredientButton();
}
else
{
_selectedCraftRecipe.DisableInteractableAddIngredientButton();
}
// 추가된 메뉴에서는 추가 재료 버튼 비활성화
// 현재 메뉴에 등록된 레시피의 Idx를 가져옵니다.
string currentRecipeIdx = _currentTodayMenuButton.AddedCraftRecipeData.Idx;
// _craftRecipeButtons 중에서 현재 메뉴의 레시피와 동일한 Idx를 가진 버튼만 표시하고, 나머지는 숨깁니다.
foreach (CraftRecipeButton button in _craftRecipeButtons)
{
bool shouldShow = (button.CraftRecipeData.Idx == currentRecipeIdx);
button.gameObject.SetActive(shouldShow);
}
}
else
{
if (_currentCraftRecipeButton == null)
{
_selectedCraftRecipe.DisableInteractableAddIngredientButton();
}
else
{
// 현재 TodayMenuButton이 등록되지 않은 경우,
// 선택된 레시피가 제작 가능한지 여부에 따라 추가 재료 버튼의 활성화 여부를 결정합니다.
if (_selectedCraftRecipe.IsCraftable)
{
_selectedCraftRecipe.EnableInteractableAddIngredientButton();
}
else
{
_selectedCraftRecipe.DisableInteractableAddIngredientButton();
}
}
// 이미 등록된 레시피의 Idx를 수집합니다.
HashSet<string> addedRecipeIdxSet = new HashSet<string>();
foreach (TodayMenuButton todayMenuButton in _todayMenuButtons)
{
if (todayMenuButton.IsAddedMenu)
{
addedRecipeIdxSet.Add(todayMenuButton.AddedCraftRecipeData.Idx);
}
}
// _craftRecipeButtons 중에서 등록되지 않은 레시피만 표시합니다.
foreach (CraftRecipeButton button in _craftRecipeButtons)
{
bool shouldShow = !addedRecipeIdxSet.Contains(button.CraftRecipeData.Idx);
button.gameObject.SetActive(shouldShow);
}
}
}
}
}