using System.Collections.Generic; using UnityEngine; namespace DDD { public class TodayMenuView : MonoBehaviour, IEventHandler, IEventHandler { [SerializeField] private GameObject _slotPrefab; [SerializeField] private Transform _todayFoodContent; [SerializeField] private Transform _todayDrinkContent; private List _foodSlots; private List _drinkSlots; private RestaurantManagementSo _restaurantManagementSo; private async void Start() { EventBus.Register(this); EventBus.Register(this); _restaurantManagementSo = await AssetManager.LoadAsset(DataConstants.RestaurantManagementSo); foreach (Transform child in _todayFoodContent) { Destroy(child.gameObject); } int maxFoodCount = _restaurantManagementSo.MaxFoodCount; _foodSlots = new List(maxFoodCount); for (int i = 0; i < _restaurantManagementSo.MaxFoodCount; i++) { var go = Instantiate(_slotPrefab, _todayFoodContent); var slot = go.GetComponent(); slot.Initialize(null, RecipeType.FoodRecipe); var todayMenuInteractor = go.GetComponent(); todayMenuInteractor.Initialize(TodayMenuEventType.Remove); _foodSlots.Add(slot); } foreach (Transform child in _todayDrinkContent) { Destroy(child.gameObject); } int maxDrinkCount = _restaurantManagementSo.MaxDrinkCount; _drinkSlots = new List(maxDrinkCount); for (int i = 0; i < _restaurantManagementSo.MaxDrinkCount; i++) { var go = Instantiate(_slotPrefab, _todayDrinkContent); var slot = go.GetComponent(); slot.Initialize(null, RecipeType.DrinkRecipe); var todayMenuInteractor = go.GetComponent(); todayMenuInteractor.Initialize(TodayMenuEventType.Remove); _drinkSlots.Add(slot); } UpdateView(); } private void OnDestroy() { EventBus.Unregister(this); EventBus.Unregister(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); } } } } }