ItemContainerInterface.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using UnityEngine;
  2. namespace HQFPSWeapons.UserInterface
  3. {
  4. public class ItemContainerInterface : ContainerInterface<ItemSlotInterface>
  5. {
  6. public ItemContainer ItemContainer
  7. {
  8. get
  9. {
  10. if(m_ItemContainer != null)
  11. return m_ItemContainer;
  12. else
  13. {
  14. Debug.LogError("There's no item container linked. Can't retrieve any!");
  15. return null;
  16. }
  17. }
  18. }
  19. [Header("Item Container")]
  20. [SerializeField]
  21. private bool m_IsPlayerContainer = true;
  22. [SerializeField]
  23. private string m_ContainerName = string.Empty;
  24. private ItemContainer m_ItemContainer = null;
  25. public void AttachToContainer(ItemContainer container)
  26. {
  27. bool generatedSlots = GenerateSlots(container.Count);
  28. if(generatedSlots)
  29. {
  30. m_ItemContainer = container;
  31. for(int i = 0;i < m_ItemContainer.Count;i ++)
  32. m_SlotInterfaces[i].LinkToSlot(m_ItemContainer[i]);
  33. }
  34. }
  35. public void DetachFromContainer()
  36. {
  37. if(m_ItemContainer == null)
  38. return;
  39. for(int i = 0;i < m_SlotInterfaces.Length;i++)
  40. m_SlotInterfaces[i].UnlinkFromSlot();
  41. }
  42. public override void OnAttachment()
  43. {
  44. if(m_IsPlayerContainer)
  45. {
  46. ItemContainer itemContainer = Player.Inventory.GetContainerWithName(m_ContainerName);
  47. if(itemContainer != null)
  48. AttachToContainer(itemContainer);
  49. }
  50. }
  51. }
  52. }