ProjectDDD/Assets/_DDD/_Scripts/GameUi/New/TodayMenuInteractor.cs

76 lines
2.2 KiB
C#

using System;
using System.Threading.Tasks;
using UnityEngine;
namespace DDD
{
public enum TodayMenuEventType
{
None = 0,
Add,
Remove
}
public class TodayMenuInteractor : MonoBehaviour, IInteractableUi
{
private ItemSlotUi _itemSlotUi;
private RestaurantManagementSo _restaurantManagementSo;
private TaskCompletionSource<bool> _isInitialized = new();
private TodayMenuEventType _todayMenuEventType = TodayMenuEventType.None;
private void Awake()
{
_itemSlotUi = GetComponent<ItemSlotUi>();
}
public async void Initialize(TodayMenuEventType todayMenuEventType)
{
_todayMenuEventType = todayMenuEventType;
_restaurantManagementSo = await AssetManager.LoadAsset<RestaurantManagementSo>(DataConstants.RestaurantManagementSo);
_isInitialized.SetResult(true);
}
public async void OnInteract()
{
await _isInitialized.Task;
switch (_todayMenuEventType)
{
case TodayMenuEventType.Add:
OnAdded();
break;
case TodayMenuEventType.Remove:
OnRemoved();
break;
case TodayMenuEventType.None:
default:
throw new ArgumentOutOfRangeException();
}
}
private void OnAdded()
{
if (_itemSlotUi.Strategy is not InventorySlotUiStrategy inventorySlotUiStrategy) return;
if (inventorySlotUiStrategy.CanCrafting(_itemSlotUi))
{
_restaurantManagementSo.TryAddTodayMenu(_itemSlotUi);
}
else
{
var evt = GameEvents.RequestShowGlobalMessageEvent;
// TODO : 테스트용 메세지 추후 삭제 및 변경
evt.NewMessageKey = "today_menu_added_error_message_001";
evt.FadeDuration = 0.5f;
evt.ShowDuration = 1f;
EventBus.Broadcast(evt);
}
}
private void OnRemoved()
{
_restaurantManagementSo.TryRemoveTodayMenu(_itemSlotUi);
}
}
}