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

293 lines
11 KiB
C#
Raw Normal View History

2024-11-15 07:28:13 +00:00
using System.Collections;
2024-10-24 05:04:40 +00:00
using System.Collections.Generic;
using System.Linq;
2024-12-05 10:30:41 +00:00
using System.Text;
2024-11-17 15:36:08 +00:00
using BlueWater.Audios;
2024-10-24 05:04:40 +00:00
using BlueWater.Items;
2024-11-15 07:28:13 +00:00
using BlueWater.Utility;
2024-12-05 10:30:41 +00:00
using Sirenix.OdinInspector;
2024-10-24 05:04:40 +00:00
using TMPro;
using UnityEngine;
2024-12-02 01:48:44 +00:00
using UnityEngine.EventSystems;
2024-11-17 04:29:57 +00:00
using UnityEngine.InputSystem;
2024-11-15 07:28:13 +00:00
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.ResourceManagement.AsyncOperations;
2024-12-10 06:49:15 +00:00
using UnityEngine.Serialization;
2024-10-24 05:04:40 +00:00
using UnityEngine.UI;
2024-11-17 04:29:57 +00:00
namespace BlueWater.Uis
2024-10-24 05:04:40 +00:00
{
2024-11-30 17:07:45 +00:00
public class ManualBook : PopupUi
2024-10-24 05:04:40 +00:00
{
2024-11-17 04:29:57 +00:00
[SerializeField]
private GameObject _panel;
2024-11-19 06:03:43 +00:00
[SerializeField]
private TMP_Text _openManualKeyText;
2024-12-05 10:30:41 +00:00
[Title("선택된 칵테일")]
2024-11-17 04:29:57 +00:00
[SerializeField]
2024-12-05 10:30:41 +00:00
private TMP_Text _selectedCocktailName;
[SerializeField]
private Image _selectedCocktailImage;
[SerializeField]
private TMP_Text _ratioRange;
[SerializeField]
private TMP_Text _selectedCocktailDescription;
2024-11-17 04:29:57 +00:00
2024-12-05 10:30:41 +00:00
[Title("제작 방법")]
[SerializeField]
private Transform _craftingContents;
[SerializeField]
private List<CraftingIngredient> _craftingIngredients = new(3);
2024-11-17 04:29:57 +00:00
2024-12-05 10:30:41 +00:00
[Title("사운드")]
2024-11-17 15:36:08 +00:00
[SerializeField]
2024-12-10 06:49:15 +00:00
private string _openManualSfxName = "OpenManualBook";
[SerializeField]
private string _closeManualSfxName = "CloseManualBook";
2024-11-17 15:36:08 +00:00
2024-12-05 10:30:41 +00:00
[Title("참조")]
[SerializeField]
2024-12-03 12:30:09 +00:00
private UiEventsController uiEventsController;
2024-12-02 01:48:44 +00:00
2024-12-05 10:30:41 +00:00
[Title("실시간 데이터")]
[SerializeField]
private List<CocktailRecipeButton> _cocktailRecipeButtons;
[ShowInInspector]
private HashSet<string> _unlockLiquidIdxs = new(7);
private CocktailRecipeButton _selectedCocktailRecipeButton;
2024-11-17 04:29:57 +00:00
private Coroutine _changedLocaleInstance;
private InputAction _openManualBookAction;
private InputAction _pressQAction;
private InputAction _cancelAction;
2024-11-19 06:03:43 +00:00
private string _openManualKeyBinding;
2024-12-05 10:30:41 +00:00
private void Awake()
{
2024-12-06 13:20:10 +00:00
_cocktailRecipeButtons = transform.GetComponentsInChildren<CocktailRecipeButton>(true).ToList();
foreach (var element in _cocktailRecipeButtons)
{
element.Initialize();
element.AddSelectedAction(SelectItem);
}
_craftingIngredients = _craftingContents.GetComponentsInChildren<CraftingIngredient>(true).ToList();
2024-12-05 10:30:41 +00:00
EventManager.OnLevelUp += UpdateManualBook;
LocalizationSettings.SelectedLocaleChanged += OnChangedLocale;
}
2024-11-17 04:29:57 +00:00
private void Start()
{
2024-11-18 10:07:19 +00:00
_openManualBookAction = PlayerInputKeyManager.Instance.GetAction(InputActionMaps.Tycoon, TycoonActions.OpenManualBook);
_pressQAction = PlayerInputKeyManager.Instance.GetAction(InputActionMaps.TycoonUi, TycoonUiActions.PressQ);
_cancelAction = PlayerInputKeyManager.Instance.GetAction(InputActionMaps.TycoonUi, TycoonUiActions.Cancel);
2024-11-19 06:03:43 +00:00
_openManualKeyBinding = PlayerInputKeyManager.Instance.GetBoundKey(_openManualBookAction);
SetKeyText(_openManualKeyBinding);
2024-10-29 06:46:50 +00:00
2024-11-19 01:12:01 +00:00
_openManualBookAction.performed += OnOpen;
2024-12-05 10:30:41 +00:00
uiEventsController.SetSelectObject(_cocktailRecipeButtons[0].gameObject);
EventSystem.current.SetSelectedGameObject(uiEventsController.SelectObject);
2024-10-24 05:04:40 +00:00
}
2024-11-17 04:29:57 +00:00
private void OnDestroy()
{
2024-11-18 03:51:28 +00:00
EventManager.OnLevelUp -= UpdateManualBook;
2024-11-17 04:29:57 +00:00
LocalizationSettings.SelectedLocaleChanged -= OnChangedLocale;
2024-11-18 03:51:28 +00:00
_openManualBookAction.performed -= OnOpen;
_pressQAction.performed -= OnClose;
_cancelAction.performed -= OnClose;
2024-11-17 04:29:57 +00:00
}
2024-10-29 06:46:50 +00:00
2024-11-17 04:29:57 +00:00
private void OnChangedLocale(Locale locale)
2024-10-24 05:04:40 +00:00
{
2024-11-17 04:29:57 +00:00
Utils.StartUniqueCoroutine(this, ref _changedLocaleInstance, ChangeLocaleCoroutine(locale));
StartCoroutine(ChangeLocaleCoroutine(locale));
2024-10-24 05:04:40 +00:00
}
2024-11-17 04:29:57 +00:00
private IEnumerator ChangeLocaleCoroutine(Locale locale)
{
var loadingOperation = Utils.GetTableAsync();
yield return loadingOperation;
2024-11-15 07:28:13 +00:00
2024-11-17 04:29:57 +00:00
if (loadingOperation.Status == AsyncOperationStatus.Succeeded)
{
2024-12-05 10:30:41 +00:00
if (_selectedCocktailRecipeButton != null)
2024-12-03 12:30:09 +00:00
{
2024-12-05 10:30:41 +00:00
_selectedCocktailName.text = Utils.GetLocalizedString(_selectedCocktailRecipeButton.CocktailData.Idx);
2024-12-06 13:20:10 +00:00
_ratioRange.text = $"{Utils.GetLocalizedString("MarginOfError")} : {_selectedCocktailRecipeButton.CocktailData.RatioRange}%";
2024-12-05 10:30:41 +00:00
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append($"{_selectedCocktailRecipeButton.CocktailData.Idx}Description");
_selectedCocktailDescription.text = Utils.GetLocalizedString(stringBuilder.ToString());
2024-12-03 12:30:09 +00:00
}
2024-11-17 04:29:57 +00:00
}
}
public void OnOpen(InputAction.CallbackContext context)
2024-11-15 07:28:13 +00:00
{
2024-11-17 04:29:57 +00:00
Open();
2024-11-15 07:28:13 +00:00
}
2024-10-28 09:09:18 +00:00
2024-11-17 04:29:57 +00:00
public override void Open()
{
2024-11-19 01:12:01 +00:00
if (!PopupUiController.IsPopupListEmpty()) return;
2024-11-17 04:29:57 +00:00
VisualFeedbackManager.Instance.SetBaseTimeScale(0.0f);
2024-11-30 17:07:45 +00:00
PlayerInputKeyManager.Instance.SwitchCurrentActionMap(InputActionMaps.TycoonUi);
2024-11-17 04:29:57 +00:00
PopupUiController.RegisterPopup(this);
_panel.SetActive(true);
IsOpened = true;
2024-12-10 06:49:15 +00:00
AudioManager.Instance.PlaySfx(_openManualSfxName, ignoreTimeScale: true);
2024-12-03 12:30:09 +00:00
EventSystem.current.SetSelectedGameObject(uiEventsController.SelectObject);
2024-11-17 04:29:57 +00:00
}
2024-10-29 10:43:07 +00:00
2024-11-17 04:29:57 +00:00
public void OnClose(InputAction.CallbackContext context)
{
Close();
}
2024-10-24 05:04:40 +00:00
2024-11-17 04:29:57 +00:00
public override void Close()
{
2024-12-10 06:49:15 +00:00
AudioManager.Instance.PlaySfx(_closeManualSfxName, ignoreTimeScale: true);
2024-11-17 04:29:57 +00:00
_panel.SetActive(false);
PopupUiController.UnregisterPopup(this);
2024-11-30 17:07:45 +00:00
PlayerInputKeyManager.Instance.SwitchCurrentActionMap(InputActionMaps.Tycoon);
2024-11-17 04:29:57 +00:00
IsOpened = false;
VisualFeedbackManager.Instance.ResetTimeScale();
}
2024-10-24 05:04:40 +00:00
2024-11-17 04:29:57 +00:00
public override void EnableInput()
{
_pressQAction.performed += OnClose;
_cancelAction.performed += OnClose;
2024-12-03 12:30:09 +00:00
uiEventsController.EnableAutoNavigate();
2024-11-17 04:29:57 +00:00
}
2024-10-24 05:04:40 +00:00
2024-11-17 04:29:57 +00:00
public override void DisableInput()
{
2024-12-03 12:30:09 +00:00
uiEventsController.DisableAutoNavigate();
2024-11-17 04:29:57 +00:00
_pressQAction.performed -= OnClose;
_cancelAction.performed -= OnClose;
}
2024-11-19 06:03:43 +00:00
private void SetKeyText(string bindingKey)
{
_openManualKeyText.text = bindingKey;
}
2024-10-24 05:04:40 +00:00
2024-12-05 10:30:41 +00:00
// private void SetNavigation()
// {
// int maxRow = 4;
//
// for (int i = 0; i < _button.Count; i++)
// {
// Navigation navigation = _button[i].Button.navigation;
// navigation.mode = Navigation.Mode.Explicit;
//
// // 좌측 네비게이션 설정
// if (i % maxRow != 0)
// {
// navigation.selectOnLeft = _button[i - 1].Button;
// }
//
// // 우측 네비게이션 설정
// if ((i + 1) % maxRow != 0 && (i + 1) < _button.Count)
// {
// navigation.selectOnRight = _button[i + 1].Button;
// }
//
// // 위쪽 네비게이션 설정
// if (i - maxRow >= 0)
// {
// navigation.selectOnUp = _button[i - maxRow].Button;
// }
//
// // 아래쪽 네비게이션 설정
// if (i + maxRow < _button.Count)
// {
// navigation.selectOnDown = _button[i + maxRow].Button;
// }
//
// // 설정된 네비게이션을 버튼에 적용
// _button[i].Button.navigation = navigation;
// }
// }
public void SelectItem(CocktailRecipeButton cocktailRecipeButton)
2024-12-02 01:48:44 +00:00
{
2024-12-05 10:30:41 +00:00
_selectedCocktailRecipeButton = cocktailRecipeButton;
uiEventsController.SetSelectObject(_selectedCocktailRecipeButton.gameObject);
_selectedCocktailName.text = Utils.GetLocalizedString(_selectedCocktailRecipeButton.CocktailData.Idx);
_selectedCocktailImage.sprite = _selectedCocktailRecipeButton.CocktailData.Sprite;
2024-12-06 13:20:10 +00:00
_ratioRange.text = $"{Utils.GetLocalizedString("MarginOfError")} : {_selectedCocktailRecipeButton.CocktailData.RatioRange}%";
2024-12-05 10:30:41 +00:00
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append($"{_selectedCocktailRecipeButton.CocktailData.Idx}Description");
_selectedCocktailDescription.text = Utils.GetLocalizedString(stringBuilder.ToString());
2024-12-02 01:48:44 +00:00
2024-12-05 10:30:41 +00:00
List<CocktailIngredient> validIngredients = _selectedCocktailRecipeButton.CocktailData.ValidIngredients;
2024-12-02 01:48:44 +00:00
2024-12-05 10:30:41 +00:00
for (int i = 0; i < _craftingIngredients.Count; i++)
{
if (i >= validIngredients.Count)
2024-12-02 01:48:44 +00:00
{
2024-12-05 10:30:41 +00:00
_craftingIngredients[i].Hide();
2024-12-02 01:48:44 +00:00
}
2024-12-05 10:30:41 +00:00
else
2024-12-02 01:48:44 +00:00
{
2024-12-05 10:30:41 +00:00
_craftingIngredients[i].Initialize(validIngredients[i], i);
_craftingIngredients[i].Show();
2024-12-02 01:48:44 +00:00
}
}
}
2024-12-05 10:30:41 +00:00
private void UpdateManualBook(LevelData levelData)
2024-10-24 05:04:40 +00:00
{
2024-12-05 10:30:41 +00:00
if (string.IsNullOrEmpty(levelData.OpenUpgrade)) return;
int currentLevel = int.Parse(levelData.Idx);
switch (currentLevel)
2024-10-24 05:04:40 +00:00
{
2024-12-05 10:30:41 +00:00
case 1:
_unlockLiquidIdxs.Add("LiquidA");
break;
case 5:
_unlockLiquidIdxs.Add("LiquidB");
break;
case 10:
_unlockLiquidIdxs.Add("LiquidC");
break;
case 15:
_unlockLiquidIdxs.Add("LiquidD");
break;
case 20:
_unlockLiquidIdxs.Add("LiquidE");
break;
case 25:
_unlockLiquidIdxs.Add("Garnish1");
break;
case 30:
_unlockLiquidIdxs.Add("Garnish2");
break;
2024-10-24 05:04:40 +00:00
}
2024-12-05 10:30:41 +00:00
foreach (var element in _cocktailRecipeButtons)
2024-11-17 04:29:57 +00:00
{
2024-12-05 10:30:41 +00:00
element.CheckUnlock(_unlockLiquidIdxs);
2024-10-24 05:04:40 +00:00
}
2024-11-17 04:29:57 +00:00
}
2024-10-24 05:04:40 +00:00
}
2024-11-17 04:29:57 +00:00
}