2022-10-19 13:51:46 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace SimpleInventorySystem
|
|
|
|
|
{
|
|
|
|
|
public class GroupSlotUI : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public SlotUI slot;
|
2022-10-29 19:56:00 +00:00
|
|
|
|
public SlotUI[] slots;
|
|
|
|
|
public bool requireAllSlots;
|
|
|
|
|
public GameObject[] showGameObjects;
|
|
|
|
|
public SlotUI[] incompatibleSlots;
|
|
|
|
|
public GameObject[] hideGameObjects;
|
2022-11-15 22:58:54 +00:00
|
|
|
|
public List<ItemTags> AllowedItemTags;
|
2023-01-25 07:14:16 +00:00
|
|
|
|
private bool conditionMet;
|
2022-10-19 13:51:46 +00:00
|
|
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
2022-10-29 19:56:00 +00:00
|
|
|
|
if (slot != null)
|
2022-10-19 13:51:46 +00:00
|
|
|
|
{
|
2022-11-15 22:58:54 +00:00
|
|
|
|
slot.groupSlots.Add(this);
|
2022-10-19 13:51:46 +00:00
|
|
|
|
}
|
2022-10-29 19:56:00 +00:00
|
|
|
|
if (slots != null)
|
2022-10-19 13:51:46 +00:00
|
|
|
|
{
|
2022-10-29 19:56:00 +00:00
|
|
|
|
foreach (SlotUI s in slots)
|
|
|
|
|
{
|
2022-11-15 22:58:54 +00:00
|
|
|
|
s.groupSlots.Add(this);
|
2022-10-29 19:56:00 +00:00
|
|
|
|
}
|
2022-10-19 13:51:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-25 07:14:16 +00:00
|
|
|
|
public bool IsSlotInShowGameObjects(SlotUI slotUI)
|
|
|
|
|
{
|
|
|
|
|
foreach(GameObject sgo in showGameObjects)
|
|
|
|
|
{
|
|
|
|
|
if(sgo.GetComponent<SlotUI>() == slotUI)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsSlotBeingShown(SlotUI slotUI)
|
|
|
|
|
{
|
|
|
|
|
foreach (GameObject sgo in showGameObjects)
|
|
|
|
|
{
|
|
|
|
|
if (sgo.GetComponent<SlotUI>() == slotUI)
|
|
|
|
|
{
|
|
|
|
|
return ConditionMet();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasSlotOccupied()
|
|
|
|
|
{
|
|
|
|
|
return slot.GetItemUI() != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool ConditionMet()
|
|
|
|
|
{
|
|
|
|
|
return conditionMet;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 13:51:46 +00:00
|
|
|
|
public void ShowUIContainer()
|
|
|
|
|
{
|
2022-10-29 19:56:00 +00:00
|
|
|
|
foreach(GameObject uiContainerGameObject in showGameObjects)
|
2022-10-19 13:51:46 +00:00
|
|
|
|
{
|
|
|
|
|
uiContainerGameObject.SetActive(true);
|
|
|
|
|
}
|
2022-10-29 19:56:00 +00:00
|
|
|
|
foreach (GameObject uiContainerGameObject in hideGameObjects)
|
|
|
|
|
{
|
|
|
|
|
uiContainerGameObject.SetActive(false);
|
|
|
|
|
}
|
2023-01-25 07:14:16 +00:00
|
|
|
|
conditionMet = true;
|
2022-10-19 13:51:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void HideUIContainer()
|
|
|
|
|
{
|
2022-10-29 19:56:00 +00:00
|
|
|
|
foreach (GameObject uiContainerGameObject in showGameObjects)
|
2022-10-19 13:51:46 +00:00
|
|
|
|
{
|
2023-03-13 10:25:15 +00:00
|
|
|
|
//Debug.Log("Hide: " + uiContainerGameObject.name);
|
2022-10-19 13:51:46 +00:00
|
|
|
|
uiContainerGameObject.SetActive(false);
|
|
|
|
|
}
|
2022-10-29 19:56:00 +00:00
|
|
|
|
foreach (GameObject uiContainerGameObject in hideGameObjects)
|
|
|
|
|
{
|
|
|
|
|
uiContainerGameObject.SetActive(true);
|
|
|
|
|
}
|
2023-01-25 07:14:16 +00:00
|
|
|
|
conditionMet = false;
|
2022-10-19 13:51:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-29 19:56:00 +00:00
|
|
|
|
// TODO: Could use improvement
|
|
|
|
|
public bool HasSlotsOccupied()
|
|
|
|
|
{
|
|
|
|
|
if (slot != null && slot.GetItemUI() == null && (slots == null || slots.Length == 0))
|
|
|
|
|
{
|
2023-03-13 10:25:15 +00:00
|
|
|
|
//Debug.Log("A");
|
2022-10-29 19:56:00 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else if(slots != null && slots.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
bool atLesatOneSlot = false;
|
|
|
|
|
foreach(SlotUI s in slots)
|
|
|
|
|
{
|
|
|
|
|
if (s.GetItemUI() == null)
|
|
|
|
|
{
|
|
|
|
|
if (requireAllSlots)
|
|
|
|
|
{
|
2023-03-13 10:25:15 +00:00
|
|
|
|
//Debug.Log("B");
|
2022-10-29 19:56:00 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
atLesatOneSlot = true;
|
|
|
|
|
if (!requireAllSlots)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!atLesatOneSlot && !requireAllSlots)
|
|
|
|
|
{
|
2023-03-13 10:25:15 +00:00
|
|
|
|
//Debug.Log("C");
|
2022-10-29 19:56:00 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-13 10:25:15 +00:00
|
|
|
|
//Debug.Log("D");
|
2022-10-29 19:56:00 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasIncompatibleSlotsOccupied()
|
|
|
|
|
{
|
|
|
|
|
if(incompatibleSlots != null)
|
|
|
|
|
{
|
|
|
|
|
foreach(SlotUI s in incompatibleSlots)
|
|
|
|
|
{
|
|
|
|
|
if(s.GetItemUI() != null)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 13:51:46 +00:00
|
|
|
|
internal void OnItemDropped()
|
|
|
|
|
{
|
2022-11-15 22:58:54 +00:00
|
|
|
|
bool allowed = AllowedItemTags == null || AllowedItemTags.Count == 0 || AllowedItemTags.Contains(ItemTags.Any) || AllowedItemTags.Contains(slot.GetItemUI().ItemTag);
|
|
|
|
|
|
|
|
|
|
if (HasSlotsOccupied() && !HasIncompatibleSlotsOccupied() && allowed)
|
2022-10-29 19:56:00 +00:00
|
|
|
|
{
|
|
|
|
|
ShowUIContainer();
|
|
|
|
|
}
|
2022-10-19 13:51:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void OnItemRemoved()
|
|
|
|
|
{
|
2022-10-29 19:56:00 +00:00
|
|
|
|
Debug.Log("GroupSlotUI: Removed Item");
|
|
|
|
|
if (!HasSlotsOccupied())
|
|
|
|
|
{
|
2023-03-13 10:25:15 +00:00
|
|
|
|
Debug.Log("GroupSlotUI: Hide UI");
|
2022-10-29 19:56:00 +00:00
|
|
|
|
HideUIContainer();
|
|
|
|
|
}
|
2022-10-19 13:51:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|