ProjectDDD/Assets/_DDD/_Scripts/GameUi/New/TodayMenuView.cs
2025-07-28 07:55:07 +09:00

113 lines
4.0 KiB
C#

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