139 lines
4.8 KiB
C#
139 lines
4.8 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace Kryz.CharacterStats.Examples
|
|
{
|
|
public class EquipmentPanel : MonoBehaviour
|
|
{
|
|
[SerializeField] Transform equipmentSlotsParent;
|
|
[SerializeField] EquipmentSlot[] equipmentSlots;
|
|
|
|
[SerializeField] GameEventListener onJoinedRoom;
|
|
|
|
public UnityEvent_ItemInstance OnItemRightClickedEvent = new UnityEvent_ItemInstance();
|
|
|
|
EquipmentInstanceData equipmentData = new EquipmentInstanceData();
|
|
|
|
#region Singleton
|
|
private static EquipmentPanel _instance;
|
|
|
|
// Public reference to the singleton instance
|
|
public static EquipmentPanel Instance
|
|
{
|
|
get
|
|
{
|
|
// If the instance doesn't exist, try to find it in the scene
|
|
if (_instance == null)
|
|
{
|
|
_instance = FindObjectOfType<EquipmentPanel>();
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
private void Awake()
|
|
{
|
|
onJoinedRoom.Response.AddListener(LoadEquipment);
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
for (int i = 0; i < equipmentSlots.Length; i++)
|
|
{
|
|
equipmentSlots[i].OnRightClickEvent.AddListener((X) => OnItemRightClickedEvent.Invoke(X));
|
|
}
|
|
}
|
|
|
|
private void OnValidate()
|
|
{
|
|
if (equipmentSlotsParent != null)
|
|
equipmentSlots = equipmentSlotsParent.GetComponentsInChildren<EquipmentSlot>();
|
|
}
|
|
|
|
public EquippableItemInstance GetEquippedItemOnSpecificSlot(EquipmentType slot)
|
|
{
|
|
for (int i = 0; i < equipmentSlots.Length; i++)
|
|
{
|
|
if(equipmentSlots[i].EquipmentType == slot)
|
|
{
|
|
return (EquippableItemInstance)equipmentSlots[i].Item;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public bool AddItem(EquippableItemInstance item, out EquippableItemInstance previousItem)
|
|
{
|
|
for (int i = 0; i < equipmentSlots.Length; i++)
|
|
{
|
|
if (equipmentSlots[i].EquipmentType == item.EquipmentType)
|
|
{
|
|
previousItem = (EquippableItemInstance)equipmentSlots[i].Item;
|
|
equipmentSlots[i].Item = item;
|
|
|
|
equipmentData.equippedItems[i] = item;
|
|
PlayerDataHandler.Instance.SaveCharacterEquipmentData(PlayerDataHandler.Instance.currentPlayerName.Value, PlayerDataHandler.Instance.currentCharacterName.Value, equipmentData);
|
|
return true;
|
|
}
|
|
}
|
|
previousItem = null;
|
|
return false;
|
|
}
|
|
|
|
public bool RemoveItem(EquippableItemInstance item)
|
|
{
|
|
for (int i = 0; i < equipmentSlots.Length; i++)
|
|
{
|
|
if (equipmentSlots[i].Item == item)
|
|
{
|
|
equipmentSlots[i].Item = null;
|
|
equipmentData.equippedItems[i] = null;
|
|
PlayerDataHandler.Instance.SaveCharacterEquipmentData(PlayerDataHandler.Instance.currentPlayerName.Value, PlayerDataHandler.Instance.currentCharacterName.Value, equipmentData);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public EquipmentSlot[] GetCurrentSlots()
|
|
{
|
|
return equipmentSlots;
|
|
}
|
|
|
|
public void LoadEquipment()
|
|
{
|
|
equipmentData = PlayerDataHandler.Instance.LoadCharacterEquipmentData(PlayerDataHandler.Instance.currentPlayerName.Value, PlayerDataHandler.Instance.currentCharacterName.Value);
|
|
|
|
if (equipmentData != null)
|
|
{
|
|
Debug.Log("Success Loading Equipment");
|
|
for (int i = 0; i < equipmentData.equippedItems.Length; i++)
|
|
{
|
|
if (equipmentData.equippedItems[i] == null || string.IsNullOrEmpty(equipmentData.equippedItems[i].ItemName))
|
|
{
|
|
equipmentSlots[i].Item = null;
|
|
}
|
|
else
|
|
{
|
|
equipmentData.equippedItems[i].Icon = ItemIndexer.Instance.Items[equipmentData.equippedItems[i].templateIndex].Icon;
|
|
equipmentSlots[i].Item = equipmentData.equippedItems[i];
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (int i = 0; i < equipmentSlots.Length; i++)
|
|
{
|
|
equipmentSlots[i].Item = null;
|
|
}
|
|
|
|
equipmentData = new EquipmentInstanceData();
|
|
|
|
PlayerDataHandler.Instance.SaveCharacterEquipmentData(PlayerDataHandler.Instance.currentPlayerName.Value, PlayerDataHandler.Instance.currentCharacterName.Value, equipmentData);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|