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