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

63 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
namespace DDD
{
public class InventoryView : MonoBehaviour, IEventHandler<InventoryChangedEvent>
{
[SerializeField] private Transform _slotParent;
[SerializeField] private GameObject _slotPrefab;
private readonly Dictionary<string, IInventorySlotUi> _slotLookup = new();
private void OnEnable()
{
EventBus.Register<InventoryChangedEvent>(this);
}
private void OnDisable()
{
EventBus.Unregister<InventoryChangedEvent>(this);
}
public void ShowItems(Func<ItemData, bool> predicate)
{
Clear();
var models = ItemViewModelFactory.CreateRestaurantManagementInventoryItem(predicate);
foreach (var model in models)
{
var go = Instantiate(_slotPrefab, _slotParent);
var slot = go.GetComponent<IInventorySlotUi>();
slot.Initialize(model);
// 슬롯 참조 저장
_slotLookup[model.Id] = slot;
}
if (_slotParent.childCount > 0)
{
EventSystem.current.SetSelectedGameObject(_slotParent.GetChild(0).gameObject);
}
}
private void Clear()
{
foreach (Transform child in _slotParent)
{
Destroy(child.gameObject);
}
}
public void Invoke(InventoryChangedEvent evt)
{
if (_slotLookup.TryGetValue(evt.ItemId, out var slot))
{
slot.UpdateCount(evt.NewCount);
}
}
}
}