using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace DDD.Restaurant { public class BillHud : MonoBehaviour, IEventHandler { [SerializeField] private RectTransform _billItemsLayoutTransform; [SerializeField] private GameObject _billItemPrefab; private readonly Dictionary _billItemMap = new(); private void Start() { EventBus.Register(this); Utils.DestroyAllChildren(_billItemsLayoutTransform); } public void HandleEvent(RestaurantOrderEvent evt) { if (evt.OrderPhase == RestaurantOrderType.Order) { var orderState = evt.OrderObjectState; if (orderState == null) return; var billItem = Instantiate(_billItemPrefab, _billItemsLayoutTransform); var sprite = DataManager.Instance.GetSprite(orderState.FoodId); billItem.GetComponent().sprite = sprite; _billItemMap[orderState.Customer] = billItem; } else if (evt.OrderPhase == RestaurantOrderType.Serve) { // Find bill item by customer from _billItemMap, then destroy it. if (_billItemMap.TryGetValue(evt.OrderObjectState.Customer, out var billItem)) { Destroy(billItem); _billItemMap.Remove(evt.OrderObjectState.Customer); } } } } }