182 lines
5.9 KiB
C#
182 lines
5.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace Kryz.CharacterStats.Examples
|
|
{
|
|
public class Inventory : MonoBehaviour
|
|
{
|
|
[SerializeField] List<ItemInstance> items;
|
|
[SerializeField] Transform itemsParent;
|
|
[SerializeField] ItemSlot[] itemSlots;
|
|
[SerializeField] GameObject characterPanelUI;
|
|
|
|
[SerializeField] GameEventListener onJoinedRoom;
|
|
[SerializeField] GameEventListener OnGameOptionsOpenned;
|
|
|
|
public UnityEvent_ItemInstance OnItemRightClickedEvent = new UnityEvent_ItemInstance();
|
|
public UnityEvent_ItemInstance OnItemRightClickedEventWhileVendoring = new UnityEvent_ItemInstance();
|
|
public UnityEvent_ItemInstance OnItemRightClickedEventWhileCrafting = new UnityEvent_ItemInstance();
|
|
|
|
InventoryInstanceData inventoryData = new InventoryInstanceData();
|
|
|
|
public bool isVendoring;
|
|
public bool isCrafting;
|
|
|
|
#region Singleton
|
|
private static Inventory _instance;
|
|
|
|
// Public reference to the singleton instance
|
|
public static Inventory Instance
|
|
{
|
|
get
|
|
{
|
|
// If the instance doesn't exist, try to find it in the scene
|
|
if (_instance == null)
|
|
{
|
|
_instance = FindObjectOfType<Inventory>();
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
private void Awake()
|
|
{
|
|
for (int i = 0; i < itemSlots.Length; i++)
|
|
{
|
|
itemSlots[i].OnRightClickEvent.AddListener(HandleItemSlotOnRightClick);
|
|
}
|
|
|
|
onJoinedRoom.Response.AddListener(LoadInventory);
|
|
|
|
OnGameOptionsOpenned.Response.AddListener(() => isVendoring = false);
|
|
OnGameOptionsOpenned.Response.AddListener(() => isCrafting = false);
|
|
}
|
|
|
|
private void HandleItemSlotOnRightClick(ItemInstance item)
|
|
{
|
|
if (isCrafting)
|
|
{
|
|
OnItemRightClickedEventWhileCrafting.Invoke(item);
|
|
}
|
|
else if (isVendoring)
|
|
{
|
|
OnItemRightClickedEventWhileVendoring.Invoke(item);
|
|
}
|
|
else
|
|
{
|
|
OnItemRightClickedEvent.Invoke(item);
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
RefreshUI(false);
|
|
}
|
|
|
|
private void OnValidate()
|
|
{
|
|
if (itemsParent != null)
|
|
{
|
|
itemSlots = itemsParent.GetComponentsInChildren<ItemSlot>();
|
|
RefreshUI(false);
|
|
}
|
|
}
|
|
|
|
private void RefreshUI(bool shouldSave)
|
|
{
|
|
int i = 0;
|
|
for (; i < items.Count && i < itemSlots.Length; i++)
|
|
{
|
|
itemSlots[i].Item = items[i];
|
|
|
|
inventoryData.inventoryItems[i] = items[i];
|
|
//inventoryData.inventoryItems[i] = ItemIndexer.Instance.Items.IndexOf(items[i]);
|
|
|
|
}
|
|
|
|
for (; i < itemSlots.Length; i++)
|
|
{
|
|
itemSlots[i].Item = null;
|
|
|
|
inventoryData.inventoryItems[i] = null;
|
|
}
|
|
if (!Application.isPlaying) return;
|
|
if (!shouldSave) return;
|
|
|
|
PlayerDataHandler.Instance.SaveCharacterInventoryData(PlayerDataHandler.Instance.currentPlayerName.Value, PlayerDataHandler.Instance.currentCharacterName.Value, inventoryData);
|
|
}
|
|
|
|
public bool AddItem(ItemInstance item)
|
|
{
|
|
if (IsFull())
|
|
return false;
|
|
|
|
items.Add(item);
|
|
|
|
RefreshUI(true);
|
|
return true;
|
|
}
|
|
|
|
public bool RemoveItem(ItemInstance item)
|
|
{
|
|
if (items.Remove(item))
|
|
{
|
|
RefreshUI(true);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool IsFull()
|
|
{
|
|
Debug.Log("Is Full: item count = " + items.Count + " itemSlots lenght = " + itemSlots.Length + " findAllnull/empty names " + items.FindAll(x => string.IsNullOrEmpty(x.ItemName)).Count);
|
|
Debug.Log("Is Full: " + (items.Count >= itemSlots.Length || items.FindAll(x => string.IsNullOrEmpty(x.ItemName)).Count <= 0));
|
|
return items.Count >= itemSlots.Length;
|
|
//return items.FindAll(x => string.IsNullOrEmpty(x.ItemName)).Count <= 0;
|
|
}
|
|
|
|
public void LoadInventory()
|
|
{
|
|
items.Clear();
|
|
|
|
inventoryData = PlayerDataHandler.Instance.LoadCharacterInventoryData(PlayerDataHandler.Instance.currentPlayerName.Value, PlayerDataHandler.Instance.currentCharacterName.Value);
|
|
|
|
if (inventoryData != null)
|
|
{
|
|
|
|
Debug.Log("Success Loading Inventory");
|
|
for (int i = 0; i < inventoryData.inventoryItems.Length; i++)
|
|
{
|
|
if (inventoryData.inventoryItems[i] == null) continue;
|
|
if (string.IsNullOrEmpty(inventoryData.inventoryItems[i].ItemName)) continue;
|
|
|
|
inventoryData.inventoryItems[i].Icon = ItemIndexer.Instance.Items[inventoryData.inventoryItems[i].templateIndex].Icon;
|
|
|
|
items.Add(inventoryData.inventoryItems[i]);
|
|
//items.Add(ItemIndexer.Instance.Items[inventoryData.inventoryItems[i]]);
|
|
}
|
|
|
|
RefreshUI(false);
|
|
}
|
|
else
|
|
{
|
|
inventoryData = new InventoryInstanceData();
|
|
|
|
RefreshUI(true);
|
|
//PlayerDataHandler.Instance.SaveCharacterInventoryData(PlayerDataHandler.Instance.currentPlayerName.Value, PlayerDataHandler.Instance.currentCharacterName.Value, inventoryData);
|
|
}
|
|
}
|
|
|
|
public void OpenInventory()
|
|
{
|
|
characterPanelUI.SetActive(true);
|
|
}
|
|
public void CloseInventory()
|
|
{
|
|
characterPanelUI.SetActive(false);
|
|
}
|
|
}
|
|
}
|