CapersProject/Assets/02.Scripts/Ui/Tycoon/BrewingUi.cs

205 lines
5.7 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using BlueWater.Items;
using BlueWater.Tycoons;
using Sirenix.OdinInspector;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace BlueWater.Uis
{
public class BrewingUi : SwitchActionPopupUi
{
[SerializeField, Required]
private DrinkRecipeSlotUi _drinkRecipeSlotUiPrefab;
[SerializeField, Required]
private Transform _drinkRecipeSpawnLocation;
[SerializeField]
private Image _drinkImage;
[SerializeField]
private TMP_Text _brewingQuantityText;
[SerializeField]
private Button _convertButton;
[Title("실시간 데이터")]
[SerializeField]
private List<BrewingIngredientSlotUi> _brewingIngredientSlotUis = new(3);
[SerializeField]
private DrinkData _selectedDrinkData;
private Brewing _currentBrewing;
private int _brewingQuantity;
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();
ResetBrewingUi();
TycoonEvents.OnBrewingUiClosed?.Invoke();
}
private void ResetBrewingUi()
{
_currentBrewing = null;
_drinkImage.enabled = false;
_selectedDrinkData = null;
SetBrewingQuantity(1);
foreach (var element in _brewingIngredientSlotUis)
{
element.gameObject.SetActive(false);
}
}
public void SetBrewing(Brewing brewing)
{
_currentBrewing = brewing;
}
private void AddDrinkRecipe(string idx)
{
var instance = Instantiate(_drinkRecipeSlotUiPrefab, _drinkRecipeSpawnLocation);
instance.Initialize(idx);
}
public void SelectDrinkRecipe(DrinkData drinkData)
{
_selectedDrinkData = drinkData;
_drinkImage.sprite = drinkData.Sprite;
_drinkImage.enabled = true;
var ingredients = _selectedDrinkData.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);
}
}
SetBrewingQuantity(1);
}
private void SetBrewingQuantity(int quantity)
{
_brewingQuantity = Mathf.Clamp(quantity, 1, 99);
_brewingQuantityText.text = _brewingQuantity.ToString();
foreach (var element in _brewingIngredientSlotUis)
{
element.SetQuantity(quantity);
}
CheckConvertButton();
}
public void IncreaseBrewingQuantity()
{
_brewingQuantity++;
SetBrewingQuantity(_brewingQuantity);
}
public void DecreaseBrewingQuantity()
{
_brewingQuantity--;
SetBrewingQuantity(_brewingQuantity);
}
/// <summary>
/// 현재 가지고 있는 재료의 개수만큼 최대 수량으로 설정하는 버튼
/// </summary>
public void MaxButton()
{
var maxQuantity = int.MaxValue;
foreach (var element in _brewingIngredientSlotUis)
{
var newMaxQuantity = element.GetMaxQuantity();
if (maxQuantity <= newMaxQuantity) continue;
maxQuantity = newMaxQuantity;
}
SetBrewingQuantity(maxQuantity <= 0 ? 1 : maxQuantity);
}
private void CheckConvertButton()
{
if (_selectedDrinkData == null || !_currentBrewing.IsEmptyDrink())
{
_convertButton.interactable = false;
return;
}
var ingredients = _selectedDrinkData.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(_selectedDrinkData.Idx, );
}
}
}