ProjectDDD/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayRestaurantStateUi/TodayRestaurantStateView.cs

122 lines
4.5 KiB
C#
Raw Normal View History

2025-08-03 23:09:01 +00:00
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
namespace DDD
{
public class TodayRestaurantStateView : MonoBehaviour, IEventHandler<TodayMenuAddedEvent>, IEventHandler<TodayMenuRemovedEvent>
{
[SerializeField] private Transform _todayWorkerContent;
[SerializeField] private Transform _todayCookwareContent;
private List<ItemSlotUi> _workerSlots;
private List<ItemSlotUi> _cookwareSlots;
private RestaurantManagementStateSo restaurantManagementStateSo;
private RestaurantManagementDataSo restaurantManagementDataSo;
2025-08-03 23:09:01 +00:00
private void Start()
{
Initialize();
2025-08-03 23:09:01 +00:00
}
private void OnDestroy()
{
EventBus.Unregister<TodayMenuAddedEvent>(this);
EventBus.Unregister<TodayMenuRemovedEvent>(this);
}
private void Initialize()
2025-08-03 23:09:01 +00:00
{
restaurantManagementStateSo = RestaurantState.instance.ManagementState;
restaurantManagementDataSo = RestaurantDataSo.instance.ManagementData;
2025-08-03 23:09:01 +00:00
foreach (Transform child in _todayWorkerContent)
{
Destroy(child.gameObject);
}
int maxCookwareCount = restaurantManagementStateSo!.MaxCookwareCount;
2025-08-03 23:09:01 +00:00
_workerSlots = new List<ItemSlotUi>(maxCookwareCount);
for (int i = 0; i < restaurantManagementStateSo.MaxCookwareCount; i++)
2025-08-03 23:09:01 +00:00
{
var go = Instantiate(restaurantManagementDataSo.ItemSlotUiPrefab, _todayWorkerContent);
2025-08-03 23:09:01 +00:00
var slot = go.GetComponent<ItemSlotUi>();
slot.Initialize(null, new TodayWorkerSlotUiStrategy());
2025-08-03 23:09:01 +00:00
var itemSlotInteractor = go.GetComponent<ItemSlotInteractor>();
itemSlotInteractor.Initialize(TodayMenuEventType.Remove, new TodayCookwareInteractorStrategy());
2025-08-03 23:09:01 +00:00
_workerSlots.Add(slot);
}
foreach (Transform child in _todayCookwareContent)
{
Destroy(child.gameObject);
}
_cookwareSlots = new List<ItemSlotUi>(maxCookwareCount);
for (int i = 0; i < restaurantManagementStateSo.MaxCookwareCount; i++)
2025-08-03 23:09:01 +00:00
{
var go = Instantiate(restaurantManagementDataSo.ItemSlotUiPrefab, _todayCookwareContent);
2025-08-03 23:09:01 +00:00
var slot = go.GetComponent<ItemSlotUi>();
slot.Initialize(null, new TodayCookwareSlotUiStrategy());
2025-08-03 23:09:01 +00:00
var itemSlotInteractor = go.GetComponent<ItemSlotInteractor>();
itemSlotInteractor.Initialize(TodayMenuEventType.Remove, new TodayCookwareInteractorStrategy());
2025-08-03 23:09:01 +00:00
_cookwareSlots.Add(slot);
}
UpdateView();
EventBus.Register<TodayMenuAddedEvent>(this);
EventBus.Register<TodayMenuRemovedEvent>(this);
}
public void Invoke(TodayMenuAddedEvent evt)
{
UpdateView();
}
public void Invoke(TodayMenuRemovedEvent evt)
{
UpdateView();
}
private void UpdateView()
{
int workerIndex = 0;
foreach (var workerKey in restaurantManagementStateSo.TodayWorkerIds)
2025-08-03 23:09:01 +00:00
{
if (workerIndex >= _workerSlots.Count) break;
var model = ItemViewModelFactory.CreateByItemId(workerKey);
var newWorkerSlot = _workerSlots[workerIndex];
newWorkerSlot.Initialize(model, new TodayWorkerSlotUiStrategy());
2025-08-03 23:09:01 +00:00
newWorkerSlot.Model.SetCount(1);
workerIndex++;
}
for (int i = workerIndex; i < _workerSlots.Count; i++)
{
_workerSlots[i].Initialize(null, new TodayWorkerSlotUiStrategy());
2025-08-03 23:09:01 +00:00
}
int cookwareIndex = 0;
foreach (var cookwareKey in restaurantManagementStateSo.CookwareToRecipeIds.Keys)
2025-08-03 23:09:01 +00:00
{
if (cookwareIndex >= _cookwareSlots.Count) break;
var model = ItemViewModelFactory.CreateByItemId(cookwareKey);
var newCookwareSlot = _cookwareSlots[cookwareIndex];
newCookwareSlot.Initialize(model, new TodayCookwareSlotUiStrategy());
2025-08-03 23:09:01 +00:00
newCookwareSlot.Model.SetCount(1);
cookwareIndex++;
}
for (int i = cookwareIndex; i < _cookwareSlots.Count; i++)
{
_cookwareSlots[i].Initialize(null, new TodayCookwareSlotUiStrategy());
2025-08-03 23:09:01 +00:00
}
}
}
}