Scriptum/Assets/Scripts/Chest/ChestPanelController.cs

78 lines
1.9 KiB
C#
Raw Normal View History

2022-05-29 12:33:43 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ChestPanelController : MonoBehaviour
{
[Header("Inventory Information")]
[SerializeField] private GameObject blankChestSlot;
[SerializeField] private GameObject chestPanel;
[SerializeField] private Button chestPanelCloseButton;
[SerializeField] private GameObject chest; // chest to which the panel belongs
[SerializeField] public const int MAX_SLOT_CUNT = 6*8;
public List<InventorySlot> content = new List<InventorySlot>();
void Awake()
{
InitInventorySlots();
}
// Start is called before the first frame update
void Start()
{
if(chestPanelCloseButton)
{
chestPanelCloseButton.onClick.AddListener(CloseOnClick);
}
}
// Update is called once per frame
void Update()
{
}
void CloseOnClick()
{
Destroy(gameObject); // destroy panel
if(chest)
{
chest.GetComponent<Chest>().CloseChest();
}
}
void InitInventorySlots()
{
if(chestPanel)
{
for(int i = 0; i < MAX_SLOT_CUNT; i++)
{
InventorySlot newSlot = Instantiate(blankChestSlot, chestPanel.transform.position, Quaternion.identity).GetComponent<InventorySlot>();
newSlot.transform.SetParent(chestPanel.transform);
newSlot.SetupInventorySlot(null, this);
content.Add(newSlot);
}
}
}
public void SetupChest(GameObject _chest, List<Item> _chestItems)
{
chest = _chest;
SetPanelItems(_chestItems);
}
private void SetPanelItems(List<Item> _itemsList)
{
for(int i=0; i < _itemsList.Count; i++)
{
content[i].SetItem(_itemsList[i]);
}
}
}