projectEli/Assets/SimpleInventorySystem/Scripts/UI/GroupSlotUI.cs

141 lines
3.9 KiB
C#
Raw Normal View History

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<ItemTags> AllowedItemTags;
// 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 void ShowUIContainer()
{
foreach(GameObject uiContainerGameObject in showGameObjects)
{
uiContainerGameObject.SetActive(true);
}
foreach (GameObject uiContainerGameObject in hideGameObjects)
{
uiContainerGameObject.SetActive(false);
}
}
public void HideUIContainer()
{
foreach (GameObject uiContainerGameObject in showGameObjects)
{
Debug.Log("Hide: " + uiContainerGameObject.name);
uiContainerGameObject.SetActive(false);
}
foreach (GameObject uiContainerGameObject in hideGameObjects)
{
uiContainerGameObject.SetActive(true);
}
}
// 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())
{
HideUIContainer();
}
}
}
}