ProjectDDD/Assets/_DDD/_Scripts/GameUi/New/TodayMenuView.cs
2025-07-25 16:58:53 +09:00

121 lines
4.5 KiB
C#

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