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

113 lines
4.0 KiB
C#
Raw Normal View History

2025-07-25 07:58:53 +00:00
using System.Collections.Generic;
using UnityEngine;
namespace DDD
{
2025-07-27 19:32:41 +00:00
public class TodayMenuView : MonoBehaviour, IEventHandler<TodayMenuAddedEvent>, IEventHandler<TodayMenuRemovedEvent>
2025-07-25 07:58:53 +00:00
{
[SerializeField] private Transform _todayFoodContent;
[SerializeField] private Transform _todayDrinkContent;
2025-07-27 19:32:41 +00:00
private List<IInventorySlotUi> _foodSlots;
private List<IInventorySlotUi> _drinkSlots;
2025-07-25 07:58:53 +00:00
2025-07-27 19:51:11 +00:00
private RestaurantManagementSo _restaurantManagementSo;
2025-07-25 07:58:53 +00:00
private async void Start()
{
2025-07-27 19:32:41 +00:00
EventBus.Register<TodayMenuAddedEvent>(this);
EventBus.Register<TodayMenuRemovedEvent>(this);
2025-07-27 19:51:11 +00:00
_restaurantManagementSo = await AssetManager.LoadAsset<RestaurantManagementSo>(DataConstants.RestaurantManagementSo);
2025-07-27 19:32:41 +00:00
2025-07-25 07:58:53 +00:00
foreach (Transform child in _todayFoodContent)
{
Destroy(child.gameObject);
}
2025-07-27 19:51:11 +00:00
int maxFoodCount = _restaurantManagementSo.MaxFoodCount;
2025-07-27 19:32:41 +00:00
_foodSlots = new List<IInventorySlotUi>(maxFoodCount);
2025-07-27 19:51:11 +00:00
for (int i = 0; i < _restaurantManagementSo.MaxFoodCount; i++)
2025-07-25 07:58:53 +00:00
{
2025-07-27 22:55:07 +00:00
var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayFoodContent);
2025-07-27 19:32:41 +00:00
var slot = go.GetComponent<IInventorySlotUi>();
slot.Initialize(null, RecipeType.FoodRecipe);
var todayMenuInteractor = go.GetComponent<TodayMenuInteractor>();
todayMenuInteractor.Initialize(TodayMenuEventType.Remove);
2025-07-25 07:58:53 +00:00
_foodSlots.Add(slot);
}
2025-07-27 19:32:41 +00:00
2025-07-25 07:58:53 +00:00
foreach (Transform child in _todayDrinkContent)
{
Destroy(child.gameObject);
}
2025-07-27 19:32:41 +00:00
2025-07-27 19:51:11 +00:00
int maxDrinkCount = _restaurantManagementSo.MaxDrinkCount;
2025-07-27 19:32:41 +00:00
_drinkSlots = new List<IInventorySlotUi>(maxDrinkCount);
2025-07-27 19:51:11 +00:00
for (int i = 0; i < _restaurantManagementSo.MaxDrinkCount; i++)
2025-07-25 07:58:53 +00:00
{
2025-07-27 22:55:07 +00:00
var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayDrinkContent);
2025-07-27 19:32:41 +00:00
var slot = go.GetComponent<IInventorySlotUi>();
slot.Initialize(null, RecipeType.DrinkRecipe);
var todayMenuInteractor = go.GetComponent<TodayMenuInteractor>();
todayMenuInteractor.Initialize(TodayMenuEventType.Remove);
2025-07-25 07:58:53 +00:00
_drinkSlots.Add(slot);
}
2025-07-27 19:32:41 +00:00
UpdateView();
2025-07-25 07:58:53 +00:00
}
private void OnDestroy()
{
2025-07-27 19:32:41 +00:00
EventBus.Unregister<TodayMenuAddedEvent>(this);
EventBus.Unregister<TodayMenuRemovedEvent>(this);
}
public void Invoke(TodayMenuAddedEvent evt)
{
UpdateView();
2025-07-25 07:58:53 +00:00
}
2025-07-27 19:32:41 +00:00
public void Invoke(TodayMenuRemovedEvent evt)
{
UpdateView();
}
2025-07-25 07:58:53 +00:00
2025-07-27 19:32:41 +00:00
private void UpdateView()
2025-07-25 07:58:53 +00:00
{
for (int i = 0; i < _foodSlots.Count; i++)
{
2025-07-27 19:51:11 +00:00
if (i < _restaurantManagementSo.FoodRecipeIds.Count)
2025-07-25 07:58:53 +00:00
{
2025-07-27 19:51:11 +00:00
string recipeId = _restaurantManagementSo.FoodRecipeIds[i];
2025-07-27 19:32:41 +00:00
var model = ItemViewModelFactory.CreateByRecipeId(recipeId);
2025-07-25 07:58:53 +00:00
_foodSlots[i].Initialize(model);
2025-07-27 19:32:41 +00:00
_foodSlots[i].SetActive(true);
2025-07-25 07:58:53 +00:00
}
else
{
2025-07-27 19:32:41 +00:00
_foodSlots[i].Initialize(null, RecipeType.FoodRecipe);
2025-07-25 07:58:53 +00:00
}
}
2025-07-27 19:32:41 +00:00
2025-07-25 07:58:53 +00:00
for (int i = 0; i < _drinkSlots.Count; i++)
{
2025-07-27 19:51:11 +00:00
if (i < _restaurantManagementSo.DrinkRecipeIds.Count)
2025-07-25 07:58:53 +00:00
{
2025-07-27 19:51:11 +00:00
string recipeId = _restaurantManagementSo.DrinkRecipeIds[i];
2025-07-27 19:32:41 +00:00
var model = ItemViewModelFactory.CreateByRecipeId(recipeId);
2025-07-25 07:58:53 +00:00
_drinkSlots[i].Initialize(model);
2025-07-27 19:32:41 +00:00
_drinkSlots[i].SetActive(true);
2025-07-25 07:58:53 +00:00
}
else
{
2025-07-27 19:32:41 +00:00
_drinkSlots[i].Initialize(null, RecipeType.DrinkRecipe);
2025-07-25 07:58:53 +00:00
}
}
}
}
}