77 lines
2.2 KiB
C#
77 lines
2.2 KiB
C#
using System.Collections.Generic;
|
|
using Components.UI;
|
|
using Data.ScriptableObjects.World;
|
|
using Managers.Item;
|
|
using UnityEngine;
|
|
using Utils;
|
|
|
|
// InventoryUI.cs
|
|
|
|
public class BoxUIController : MonoBehaviour
|
|
{
|
|
public Transform itemParent;
|
|
|
|
private readonly Dictionary<int, GameObject> _slots = new();
|
|
|
|
private void Start()
|
|
{
|
|
GameLogger.Log("BoxUI Start");
|
|
if (itemParent == null)
|
|
{
|
|
GameLogger.LogWarning("ItemParent is null! Can not attach");
|
|
return;
|
|
}
|
|
|
|
BoxManager.Instance.OnActiveBoxChanged += UpdateUI;
|
|
|
|
// Delete existing children if any.
|
|
foreach (Transform child in itemParent) Destroy(child.gameObject);
|
|
|
|
if (BoxManager.Instance.ActiveBoxInteractable == null)
|
|
return;
|
|
|
|
if (BoxManager.Instance.ActiveBoxInteractable.Slots.Count == 0)
|
|
BoxManager.Instance.Initialize();
|
|
|
|
UpdateUI(BoxManager.Instance.ActiveBoxInteractable);
|
|
|
|
GameLogger.Log("Finished adding items to toolbar slots");
|
|
}
|
|
|
|
|
|
public void ListChildren(GameObject parent)
|
|
{
|
|
foreach (Transform child in parent.transform) GameLogger.Log("Child: " + child.name);
|
|
}
|
|
|
|
public void UpdateUI(BoxInteractable boxInteractable)
|
|
{
|
|
// clear all exisitings slots
|
|
GameLogger.Log("Updating UI displaying box: " + boxInteractable.id + " / " + boxInteractable.name);
|
|
_slots.Clear();
|
|
foreach (Transform child in itemParent) Destroy(child.gameObject);
|
|
|
|
// Get values from inventory and redraw the inventory slots based on that.
|
|
var slotPrefab = PrefabManager.Instance.GetSlotPrefab();
|
|
// var itemPrefab = prefabManager.GetItemPrefab();
|
|
|
|
|
|
foreach (var slot in boxInteractable.Slots)
|
|
{
|
|
// Draw slots to screen
|
|
var slotDisplay = Instantiate(slotPrefab, itemParent);
|
|
|
|
slotDisplay.transform.localScale = new Vector3((float)0.5, (float)0.5, (float)0.5);
|
|
|
|
var i = slot.Key;
|
|
|
|
|
|
var slotComponent = slotDisplay.GetComponent<SlotComponent>();
|
|
|
|
// Add prefab to slot
|
|
slotComponent.SetSlotDetails(slot.Value);
|
|
|
|
_slots.Add(i, slotDisplay);
|
|
}
|
|
}
|
|
} |