265 lines
8.6 KiB
C#
265 lines
8.6 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using DDD.ScriptableObjects;
|
|
using DG.Tweening;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.UI;
|
|
using Sequence = DG.Tweening.Sequence;
|
|
|
|
namespace DDD.Uis.Tycoon
|
|
{
|
|
public class MenuBoardUi : PopupUi
|
|
{
|
|
[Title("프리팹")]
|
|
[SerializeField]
|
|
private TodayMenu _todayMenuPrefab;
|
|
|
|
[SerializeField]
|
|
private TodayMenu _reverseTodayMenuPrefab;
|
|
|
|
[Title("컴포넌트")]
|
|
[SerializeField]
|
|
private GameObject _panel;
|
|
|
|
[SerializeField]
|
|
private GameObject _todayMenusPanel;
|
|
|
|
[SerializeField]
|
|
private RectTransform _todayMenusPanelRect;
|
|
|
|
[SerializeField]
|
|
private AddMenuUi _addMenuUi;
|
|
|
|
[Title("클래스")]
|
|
[SerializeField]
|
|
private UiEventsController _uiEventsController;
|
|
|
|
[Title("오늘의 메뉴")]
|
|
[SerializeField, Tooltip("메뉴판에 표시할 메뉴 갯수")]
|
|
private int _maxTodayMenuCount = 6;
|
|
|
|
[Title("연출")]
|
|
[SerializeField]
|
|
private Vector3 _originalTodayMenuPanelPosition = new(0f, -75f, 0f);
|
|
|
|
[SerializeField]
|
|
private Vector2 _todayMenuPanelPositionX = new(0f, -395f);
|
|
|
|
[SerializeField]
|
|
private float _duration = 0.5f;
|
|
|
|
[SerializeField]
|
|
private Ease _ease = Ease.Linear;
|
|
|
|
[Title("실시간 데이터")]
|
|
[SerializeField]
|
|
private List<TodayMenu> _todayMenus;
|
|
|
|
private Sequence _openAddMenuUiSequence;
|
|
private Sequence _closeAddMenuUiSequence;
|
|
private InputAction _interactionEAction;
|
|
private InputAction _cancelAction;
|
|
private InputAction _pressFAction;
|
|
|
|
private void Awake()
|
|
{
|
|
// 더미 데이터 삭제
|
|
foreach (Transform element in _todayMenusPanel.transform)
|
|
{
|
|
if (element)
|
|
{
|
|
Destroy(element.gameObject);
|
|
}
|
|
}
|
|
|
|
_addMenuUi = FindAnyObjectByType<AddMenuUi>();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
EventManager.OnTycoonGameStarted += AddMealKit;
|
|
|
|
_interactionEAction = PlayerInputKeyManager.Instance.GetAction(InputActionMaps.TycoonUi, TycoonUiActions.InteractionE);
|
|
_cancelAction = PlayerInputKeyManager.Instance.GetAction(InputActionMaps.TycoonUi, TycoonUiActions.Cancel);
|
|
_pressFAction = PlayerInputKeyManager.Instance.GetAction(InputActionMaps.TycoonUi, TycoonUiActions.PressF);
|
|
|
|
_openAddMenuUiSequence = DOTween.Sequence()
|
|
.Append(_todayMenusPanelRect.DOAnchorPosX(_todayMenuPanelPositionX.y, _duration)
|
|
.SetEase(_ease))
|
|
.Append(_addMenuUi.OpenTween)
|
|
.SetAutoKill(false)
|
|
.Pause();
|
|
|
|
_closeAddMenuUiSequence = DOTween.Sequence()
|
|
.Append(_addMenuUi.CloseTween)
|
|
.Append(_todayMenusPanelRect.DOAnchorPosX(_todayMenuPanelPositionX.x, _duration)
|
|
.SetEase(_ease))
|
|
.SetAutoKill(false)
|
|
.Pause();
|
|
|
|
_todayMenus = new List<TodayMenu>(_maxTodayMenuCount);
|
|
for (int i = 0; i < _maxTodayMenuCount; i++)
|
|
{
|
|
TodayMenu menuPrefab = i % 2 == 0 ? _todayMenuPrefab : _reverseTodayMenuPrefab;
|
|
TodayMenu todayMenu = Instantiate(menuPrefab, _todayMenusPanel.transform);
|
|
todayMenu.RegisterButtonListener(() => OnClickEmptyButton(todayMenu));
|
|
_todayMenus.Add(todayMenu);
|
|
}
|
|
|
|
SetNavigationTodayMenus();
|
|
|
|
_todayMenusPanelRect.anchoredPosition = _originalTodayMenuPanelPosition;
|
|
_addMenuUi.SetCloseSequence(_closeAddMenuUiSequence);
|
|
_panel.SetActive(false);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
EventManager.OnTycoonGameStarted -= AddMealKit;
|
|
|
|
_openAddMenuUiSequence?.Kill();
|
|
_closeAddMenuUiSequence?.Kill();
|
|
_interactionEAction = null;
|
|
_cancelAction = null;
|
|
_pressFAction = null;
|
|
}
|
|
|
|
public override void Open()
|
|
{
|
|
_todayMenusPanelRect.anchoredPosition = _originalTodayMenuPanelPosition;
|
|
|
|
base.Open();
|
|
_panel.SetActive(true);
|
|
PlayerInputKeyManager.Instance.SwitchCurrentActionMap(InputActionMaps.TycoonUi);
|
|
}
|
|
|
|
public void OnClose(InputAction.CallbackContext context)
|
|
{
|
|
Close();
|
|
}
|
|
|
|
public override void Close()
|
|
{
|
|
_panel.SetActive(false);
|
|
base.Close();
|
|
|
|
if (PopupUiController.IsPopupListEmpty() || !PopupUiController.IsPausedPopupList())
|
|
{
|
|
PlayerInputKeyManager.Instance.SwitchCurrentActionMap(InputActionMaps.Tycoon);
|
|
}
|
|
}
|
|
|
|
public override void EnableInput()
|
|
{
|
|
_interactionEAction.performed += OnInteractionE;
|
|
_cancelAction.performed += OnClose;
|
|
_pressFAction.performed += OnClose;
|
|
_uiEventsController.EnableAutoNavigate();
|
|
|
|
GameObject selectObject = _todayMenus[0].MenuButton.gameObject;
|
|
foreach (TodayMenu todayMenu in _todayMenus)
|
|
{
|
|
if (todayMenu.IsAddedMenu) continue;
|
|
|
|
selectObject = todayMenu.MenuButton.gameObject;
|
|
break;
|
|
}
|
|
_uiEventsController.SetSelectObject(selectObject);
|
|
EventSystem.current.SetSelectedGameObject(_uiEventsController.SelectObject);
|
|
}
|
|
|
|
public override void DisableInput()
|
|
{
|
|
_interactionEAction.performed -= OnInteractionE;
|
|
_cancelAction.performed -= OnClose;
|
|
_pressFAction.performed -= OnClose;
|
|
_uiEventsController.DisableAutoNavigate();
|
|
}
|
|
|
|
private void OnInteractionE(InputAction.CallbackContext context)
|
|
{
|
|
if (!EventSystem.current) return;
|
|
|
|
EventSystem.current.currentSelectedGameObject.GetComponent<Button>()?.onClick.Invoke();
|
|
}
|
|
|
|
private void OnClickEmptyButton(TodayMenu clickedTodayMenu)
|
|
{
|
|
_addMenuUi.SetCurrentTodayMenu(clickedTodayMenu);
|
|
_addMenuUi.Open();
|
|
_openAddMenuUiSequence?.Restart();
|
|
}
|
|
|
|
private void SetNavigationTodayMenus()
|
|
{
|
|
for (int i = 0; i < _maxTodayMenuCount; i++)
|
|
{
|
|
Navigation navigation = _todayMenus[i].MenuButton.navigation;
|
|
navigation.mode = Navigation.Mode.Explicit;
|
|
|
|
if (i == 0)
|
|
{
|
|
navigation.selectOnDown = _todayMenus[i + 1].MenuButton;
|
|
}
|
|
else if (i == _maxTodayMenuCount - 1)
|
|
{
|
|
navigation.selectOnUp = _todayMenus[i - 1].MenuButton;
|
|
}
|
|
else
|
|
{
|
|
navigation.selectOnDown = _todayMenus[i + 1].MenuButton;
|
|
navigation.selectOnUp = _todayMenus[i - 1].MenuButton;
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool CanOpen()
|
|
{
|
|
bool canOpen = false;
|
|
foreach (TodayMenu todayMenu in _todayMenus)
|
|
{
|
|
if (!todayMenu.IsAddedMenu) continue;
|
|
|
|
canOpen = true;
|
|
break;
|
|
}
|
|
|
|
return canOpen;
|
|
}
|
|
|
|
private void AddMealKit()
|
|
{
|
|
EventManager.InvokeAddedMealKit(_todayMenus);
|
|
}
|
|
|
|
public int GetTodayMenusCount()
|
|
{
|
|
return _todayMenus.Sum(menu => menu.AddedCount);
|
|
}
|
|
|
|
public CraftRecipeData GetRandomTodayMenu()
|
|
{
|
|
List<TodayMenu> availableMenus = _todayMenus.Where(menu => menu.CanOrderMenu()).ToList();
|
|
|
|
if (availableMenus.Count == 0)
|
|
{
|
|
Debug.LogError("주문 가능한 메뉴가 없습니다");
|
|
return null;
|
|
}
|
|
|
|
// 랜덤으로 하나의 메뉴를 선택합니다.
|
|
int randomIndex = Random.Range(0, availableMenus.Count);
|
|
TodayMenu selectedMenu = availableMenus[randomIndex];
|
|
|
|
// 선택한 메뉴에 대해 주문 처리를 진행합니다.
|
|
selectedMenu.OrderMenu();
|
|
|
|
// 선택된 메뉴의 CraftRecipeData를 반환합니다.
|
|
return selectedMenu.AddedCraftRecipeData;
|
|
}
|
|
}
|
|
}
|