CapersProject/Assets/02.Scripts/Ui/Tycoon/BrewingUi.cs
2024-09-02 22:45:46 +09:00

189 lines
5.3 KiB
C#

using System.Collections.Generic;
using BlueWater.Items;
using BlueWater.Tycoons;
using Sirenix.OdinInspector;
using TMPro;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
namespace BlueWater.Uis
{
public class BrewingUi : SwitchActionPopupUi
{
[SerializeField, Required]
private DrinkRecipeSlotUi _drinkRecipeSlotUiPrefab;
[SerializeField, Required]
private Transform _drinkRecipeSpawnLocation;
[SerializeField]
private TMP_Text _recipeName;
[SerializeField]
private Image _drinkImage;
[SerializeField]
private Button _convertButton;
[Title("실시간 데이터")]
[SerializeField]
private List<BrewingIngredientSlotUi> _brewingIngredientSlotUis = new(3);
[FormerlySerializedAs("_selectedDrinkData")] [SerializeField]
private LiquidData selectedLiquidData;
[SerializeField]
private Brewing _currentBrewing;
private List<DrinkRecipeSlotUi> _drinkRecipeSlotUis = new();
private void Start()
{
Initialize();
TycoonEvents.OnDrinkRecipeAcquired += AddDrinkRecipe;
TycoonEvents.OnDrinkRecipeSelected += SelectDrinkRecipe;
}
private void OnDestroy()
{
TycoonEvents.OnDrinkRecipeAcquired -= AddDrinkRecipe;
TycoonEvents.OnDrinkRecipeSelected -= SelectDrinkRecipe;
}
private void Initialize()
{
foreach (Transform element in _drinkRecipeSpawnLocation)
{
Destroy(element.gameObject);
}
var drinkRecipes = DataManager.Instance.TycoonData.DrinkRecipes;
foreach (var element in drinkRecipes)
{
AddDrinkRecipe(element);
}
}
public override void Open(List<PopupUi> popupUiList)
{
base.Open(popupUiList);
ResetBrewingUi();
TycoonEvents.OnBrewingUiOpened?.Invoke();
}
public override void Close()
{
base.Close();
_currentBrewing = null;
ResetBrewingUi();
TycoonEvents.OnBrewingUiClosed?.Invoke();
}
private void ResetBrewingUi()
{
selectedLiquidData = null;
_recipeName.text = null;
_drinkImage.enabled = false;
SetBrewingIngredient();
foreach (var element in _brewingIngredientSlotUis)
{
element.gameObject.SetActive(false);
}
}
public void SetBrewing(Brewing brewing)
{
_currentBrewing = brewing;
ShowDrinkRecipeByCategory();
}
private void AddDrinkRecipe(string idx)
{
var instance = Instantiate(_drinkRecipeSlotUiPrefab, _drinkRecipeSpawnLocation);
instance.Initialize(idx);
_drinkRecipeSlotUis.Add(instance);
}
private void ShowDrinkRecipeByCategory()
{
var currentDrinkCategory = _currentBrewing.GetDrinkCategory();
foreach (var element in _drinkRecipeSlotUis)
{
element.gameObject.SetActive(currentDrinkCategory == element.GetDrinkData().Category);
}
}
public void SelectDrinkRecipe(LiquidData liquidData)
{
selectedLiquidData = liquidData;
_recipeName.text = selectedLiquidData.Name;
_drinkImage.sprite = liquidData.Sprite;
_drinkImage.enabled = true;
var ingredients = selectedLiquidData.GetValidIngredients();
for (var i = 0; i < 3; i++)
{
if (ingredients.Count > i)
{
_brewingIngredientSlotUis[i].SetIngredient(ingredients[i].Idx, ingredients[i].Quantity);
_brewingIngredientSlotUis[i].gameObject.SetActive(true);
}
else
{
_brewingIngredientSlotUis[i].gameObject.SetActive(false);
}
}
SetBrewingIngredient();
}
private void SetBrewingIngredient()
{
foreach (var element in _brewingIngredientSlotUis)
{
element.SetQuantity();
}
CheckConvertButton();
}
private void CheckConvertButton()
{
if (selectedLiquidData == null || !_currentBrewing.IsEmptyDrink())
{
_convertButton.interactable = false;
return;
}
var ingredients = selectedLiquidData.GetValidIngredients();
if (ingredients.Count <= 0)
{
_convertButton.interactable = false;
return;
}
for (var i = 0; i < ingredients.Count; i++)
{
if (_brewingIngredientSlotUis[i].GetIsEnough()) continue;
_convertButton.interactable = false;
return;
}
_convertButton.interactable = true;
}
/// <summary>
/// 술을 만들 때 최종 버튼
/// </summary>
public void ConvertButton()
{
_currentBrewing.SetDrink(selectedLiquidData);
}
}
}