using System.Collections.Generic; using System.Threading.Tasks; using UnityEngine; namespace DDD { public class TodayRestaurantStateView : MonoBehaviour, IEventHandler, IEventHandler { [SerializeField] private Transform _todayWorkerContent; [SerializeField] private Transform _todayCookwareContent; private List _workerSlots; private List _cookwareSlots; private RestaurantManagementSo _restaurantManagementSo; private void Start() { _ = Initialize(); } private void OnDestroy() { EventBus.Unregister(this); EventBus.Unregister(this); } private async Task Initialize() { _restaurantManagementSo = await AssetManager.LoadAsset(DataConstants.RestaurantManagementSo); Debug.Assert(_restaurantManagementSo != null, "_restaurantManagementSo != null"); foreach (Transform child in _todayWorkerContent) { Destroy(child.gameObject); } int maxCookwareCount = _restaurantManagementSo!.MaxCookwareCount; _workerSlots = new List(maxCookwareCount); for (int i = 0; i < _restaurantManagementSo.MaxCookwareCount; i++) { var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayWorkerContent); var slot = go.GetComponent(); await slot.Initialize(null, new TodayWorkerSlotUiStrategy()); var itemSlotInteractor = go.GetComponent(); await itemSlotInteractor.Initialize(TodayMenuEventType.Remove, new TodayCookwareInteractorStrategy()); _workerSlots.Add(slot); } foreach (Transform child in _todayCookwareContent) { Destroy(child.gameObject); } _cookwareSlots = new List(maxCookwareCount); for (int i = 0; i < _restaurantManagementSo.MaxCookwareCount; i++) { var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayCookwareContent); var slot = go.GetComponent(); await slot.Initialize(null, new TodayCookwareSlotUiStrategy()); var itemSlotInteractor = go.GetComponent(); await itemSlotInteractor.Initialize(TodayMenuEventType.Remove, new TodayCookwareInteractorStrategy()); _cookwareSlots.Add(slot); } UpdateView(); EventBus.Register(this); EventBus.Register(this); } public void Invoke(TodayMenuAddedEvent evt) { UpdateView(); } public void Invoke(TodayMenuRemovedEvent evt) { UpdateView(); } private void UpdateView() { int workerIndex = 0; foreach (var workerKey in _restaurantManagementSo.TodayWorkerIds) { if (workerIndex >= _workerSlots.Count) break; var model = ItemViewModelFactory.CreateByItemId(workerKey); var newWorkerSlot = _workerSlots[workerIndex]; _ = newWorkerSlot.Initialize(model, new TodayWorkerSlotUiStrategy()); newWorkerSlot.Model.SetCount(1); workerIndex++; } for (int i = workerIndex; i < _workerSlots.Count; i++) { _ = _workerSlots[i].Initialize(null, new TodayWorkerSlotUiStrategy()); } int cookwareIndex = 0; foreach (var cookwareKey in _restaurantManagementSo.TodayCookwareIds) { if (cookwareIndex >= _cookwareSlots.Count) break; var model = ItemViewModelFactory.CreateByItemId(cookwareKey); var newCookwareSlot = _cookwareSlots[cookwareIndex]; _ = newCookwareSlot.Initialize(model, new TodayCookwareSlotUiStrategy()); newCookwareSlot.Model.SetCount(1); cookwareIndex++; } for (int i = cookwareIndex; i < _cookwareSlots.Count; i++) { _ = _cookwareSlots[i].Initialize(null, new TodayCookwareSlotUiStrategy()); } } } }