using System.Collections.Generic; using UnityEngine; namespace DDD { public class TodayMenuView : MonoBehaviour, IEventHandler { [SerializeField] private GameObject _slotPrefab; [SerializeField] private Transform _todayFoodContent; [SerializeField] private Transform _todayDrinkContent; private List _foodSlots; private List _drinkSlots; private TodayMenuDataSo _todayMenuDataSo; private async void Start() { EventBus.Register(this); _todayMenuDataSo = await AssetManager.LoadAsset(DataConstants.TodayMenuDataSo); foreach (Transform child in _todayFoodContent) { Destroy(child.gameObject); } int maxFoodCount = _todayMenuDataSo.MaxFoodCount; _foodSlots = new List(maxFoodCount); for (int i = 0; i < _todayMenuDataSo.MaxFoodCount; i++) { var go = Instantiate(_slotPrefab, _todayFoodContent); var slot = go.GetComponent(); slot.Initialize(null); slot.SetEmptyFood(); _foodSlots.Add(slot); } foreach (Transform child in _todayDrinkContent) { Destroy(child.gameObject); } int maxDrinkCount = _todayMenuDataSo.MaxDrinkCount; _drinkSlots = new List(maxDrinkCount); for (int i = 0; i < _todayMenuDataSo.MaxDrinkCount; i++) { var go = Instantiate(_slotPrefab, _todayDrinkContent); var slot = go.GetComponent(); slot.Initialize(null); slot.SetEmptyDrink(); _drinkSlots.Add(slot); } //RefreshView(); } private void OnDestroy() { EventBus.Unregister(this); } public void Invoke(TodayMenuChangedEvent evt) => RefreshView(); private void RefreshView() { for (int i = 0; i < _foodSlots.Count; i++) { if (i < _todayMenuDataSo.FoodRecipeIds.Count) { string recipeId = _todayMenuDataSo.FoodRecipeIds[i]; var recipe = DataManager.Instance.RecipeDataSo.GetDataById(recipeId); var item = InventoryManager.Instance.GetItemDataByIdOrNull(recipeId); var model = new ItemViewModel { Id = recipeId, Icon = DataManager.Instance.GetSprite(recipeId), ItemType = item.ItemType, NameKey = DataManager.Instance.FoodDataSo.GetDataById(recipe.ItemKey).NameKey, DescriptionKey = DataManager.Instance.FoodDataSo.GetDataById(recipe.ItemKey).DescriptionKey }; _foodSlots[i].Initialize(model); //_foodSlots[i].SetMark(true); // 등록 상태 마크 } else { //_foodSlots[i].gameObject.SetActive(false); // 또는 Clear() } } for (int i = 0; i < _drinkSlots.Count; i++) { if (i < _todayMenuDataSo.DrinkRecipeIds.Count) { string recipeId = _todayMenuDataSo.DrinkRecipeIds[i]; var recipe = DataManager.Instance.RecipeDataSo.GetDataById(recipeId); var item = InventoryManager.Instance.GetItemDataByIdOrNull(recipeId); var model = new ItemViewModel { Id = recipeId, Icon = DataManager.Instance.GetSprite(recipeId), ItemType = item.ItemType, NameKey = DataManager.Instance.DrinkDataSo.GetDataById(recipe.ItemKey).NameKey, DescriptionKey = DataManager.Instance.DrinkDataSo.GetDataById(recipe.ItemKey).DescriptionKey }; _drinkSlots[i].Initialize(model); //_drinkSlots[i].SetMark(true); } else { //_drinkSlots[i].gameObject.SetActive(false); } } } } }