Equipment.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Assets.HeroEditor4D.Common.Scripts.CharacterScripts;
  5. using Assets.HeroEditor4D.InventorySystem.Scripts.Data;
  6. using Assets.HeroEditor4D.InventorySystem.Scripts.Enums;
  7. using Assets.HeroEditor4D.InventorySystem.Scripts.Helpers;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. namespace Assets.HeroEditor4D.InventorySystem.Scripts.Elements
  11. {
  12. /// <summary>
  13. /// Represents hero (player) equipment. Based on equipment slots.
  14. /// </summary>
  15. public class Equipment : ItemContainer
  16. {
  17. public Action OnRefresh;
  18. /// <summary>
  19. /// Defines what kinds of items can be equipped.
  20. /// </summary>
  21. public List<ItemSlot> Slots;
  22. /// <summary>
  23. /// Equipped items will be instantiated in front of equipment slots.
  24. /// </summary>
  25. public GameObject ItemPrefab;
  26. /// <summary>
  27. /// Character preview.
  28. /// </summary>
  29. public Character Preview;
  30. public Transform Scheme;
  31. public int BagSize;
  32. public readonly List<InventoryItem> InventoryItems = new List<InventoryItem>();
  33. public void OnValidate()
  34. {
  35. if (Application.isPlaying) return;
  36. Slots = GetComponentsInChildren<ItemSlot>(true).ToList();
  37. //if (Character == null)
  38. //{
  39. // Character = FindObjectOfType<Character>();
  40. //}
  41. }
  42. public void Start()
  43. {
  44. //Character.Animator.SetBool("Ready", false);
  45. }
  46. public void SetBagSize(int size)
  47. {
  48. BagSize = size;
  49. var supplies = GetComponentsInChildren<ItemSlot>(true).Where(i => i.Types.Contains(ItemType.Supply)).ToList();
  50. for (var i = 0; i < supplies.Count; i++)
  51. {
  52. supplies[i].Locked = i >= size;
  53. }
  54. }
  55. public bool SelectAny()
  56. {
  57. if (InventoryItems.Count > 0)
  58. {
  59. InventoryItems[0].Select(true);
  60. return true;
  61. }
  62. return false;
  63. }
  64. public override void Refresh(Item selected)
  65. {
  66. var items = Slots.Select(FindItem).Where(i => i != null).ToList();
  67. var toggleGroup = GetComponentInParent<ToggleGroup>(includeInactive: true);
  68. Reset();
  69. foreach (var slot in Slots)
  70. {
  71. var item = FindItem(slot);
  72. slot.gameObject.SetActive(item == null);
  73. if (item == null) continue;
  74. var inventoryItem = Instantiate(ItemPrefab, slot.transform.parent).GetComponent<InventoryItem>();
  75. inventoryItem.Initialize(item, toggleGroup);
  76. inventoryItem.Count.text = null;
  77. inventoryItem.transform.position = slot.transform.position;
  78. inventoryItem.transform.SetSiblingIndex(slot.transform.GetSiblingIndex());
  79. if (AutoSelect) inventoryItem.Select(item == selected);
  80. InventoryItems.Add(inventoryItem);
  81. }
  82. if (Preview)
  83. {
  84. CharacterInventorySetup.Setup(Preview, items);
  85. Preview.Initialize();
  86. }
  87. OnRefresh?.Invoke();
  88. }
  89. private void Reset()
  90. {
  91. foreach (var inventoryItem in InventoryItems)
  92. {
  93. Destroy(inventoryItem.gameObject);
  94. }
  95. InventoryItems.Clear();
  96. }
  97. private Item FindItem(ItemSlot slot)
  98. {
  99. if (slot.Types.Contains(ItemType.Shield))
  100. {
  101. var copy = Items.SingleOrDefault(i => i.Params.Type == ItemType.Weapon && (i.IsTwoHanded || i.IsFirearm));
  102. if (copy != null)
  103. {
  104. return copy;
  105. }
  106. }
  107. var index = Slots.Where(i => i.Types.SequenceEqual(slot.Types)).ToList().IndexOf(slot);
  108. var items = Items.Where(slot.Supports).ToList();
  109. return index < items.Count ? items[index] : null;
  110. }
  111. }
  112. }