WarehouseBase.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Assets.HeroEditor4D.Common.Scripts.Common;
  4. using Assets.HeroEditor4D.InventorySystem.Scripts.Data;
  5. using Assets.HeroEditor4D.InventorySystem.Scripts.Enums;
  6. using Assets.HeroEditor4D.InventorySystem.Scripts.Elements;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. namespace Assets.HeroEditor4D.InventorySystem.Scripts
  10. {
  11. /// <summary>
  12. /// High-level inventory interface.
  13. /// </summary>
  14. public class WarehouseBase : ItemWorkspace
  15. {
  16. public ScrollInventory WarehouseInventory;
  17. public ScrollInventory PlayerInventory;
  18. public InputField AmountInput;
  19. public Button PutButton;
  20. public Button TakeButton;
  21. public AudioClip MoveSound;
  22. public AudioSource AudioSource;
  23. public bool ExampleInitialize;
  24. public string CurrencyId = "Gold";
  25. public int Amount;
  26. public void Awake()
  27. {
  28. // You must to set an active collection (as there may be several different collections in Resources).
  29. ItemCollection.Active = ItemCollection;
  30. }
  31. public void Start()
  32. {
  33. if (ExampleInitialize)
  34. {
  35. TestInitialize();
  36. }
  37. }
  38. /// <summary>
  39. /// Initialize owned items and trader items (just for example).
  40. /// </summary>
  41. public void TestInitialize()
  42. {
  43. var warehouse = new List<Item>();
  44. var inventory = ItemCollection.Active.Items.Select(i => new Item(i.Id, 2)).ToList();
  45. RegisterCallbacks();
  46. WarehouseInventory.Initialize(ref warehouse);
  47. PlayerInventory.Initialize(ref inventory);
  48. if (!WarehouseInventory.SelectAny() && !PlayerInventory.SelectAny())
  49. {
  50. ItemInfo.Reset();
  51. }
  52. }
  53. public void Initialize(ref List<Item> playerItems, ref List<Item> storageItems)
  54. {
  55. RegisterCallbacks();
  56. PlayerInventory.Initialize(ref playerItems);
  57. WarehouseInventory.Initialize(ref storageItems);
  58. if (!PlayerInventory.SelectAny() && !WarehouseInventory.SelectAny())
  59. {
  60. ItemInfo.Reset();
  61. }
  62. }
  63. public void RegisterCallbacks()
  64. {
  65. InventoryItem.OnLeftClick = SelectItem;
  66. InventoryItem.OnRightClick = OnDoubleClick;
  67. }
  68. private void OnDoubleClick(Item item)
  69. {
  70. SelectItem(item);
  71. if (PlayerInventory.Items.Contains(item))
  72. {
  73. Take();
  74. }
  75. else if (CanMoveSelectedItem())
  76. {
  77. Put();
  78. }
  79. }
  80. public void SelectItem(Item item)
  81. {
  82. SelectedItem = item;
  83. SetAmount(1);
  84. ItemInfo.Initialize(SelectedItem, SelectedItem.Params.Price * Amount);
  85. Refresh();
  86. }
  87. public void Put()
  88. {
  89. if (!CanMoveSelectedItem()) return;
  90. MoveItem(SelectedItem, PlayerInventory, WarehouseInventory, Amount);
  91. SelectItem(SelectedItem);
  92. AudioSource.PlayOneShot(MoveSound, SfxVolume);
  93. }
  94. public void Take()
  95. {
  96. if (!CanMoveSelectedItem()) return;
  97. MoveItem(SelectedItem, WarehouseInventory, PlayerInventory, Amount);
  98. SelectItem(SelectedItem);
  99. AudioSource.PlayOneShot(MoveSound, SfxVolume);
  100. }
  101. public override void Refresh()
  102. {
  103. if (SelectedItem == null)
  104. {
  105. ItemInfo.Reset();
  106. PutButton.SetActive(false);
  107. TakeButton.SetActive(false);
  108. }
  109. else
  110. {
  111. var stored = WarehouseInventory.Items.Contains(SelectedItem);
  112. PutButton.SetActive(!stored && CanMoveSelectedItem());
  113. TakeButton.SetActive(stored && CanMoveSelectedItem());
  114. }
  115. }
  116. public void SetMinAmount()
  117. {
  118. SetAmount(1);
  119. }
  120. public void IncAmount(int value)
  121. {
  122. SetAmount(Amount + value);
  123. }
  124. public void SetMaxAmount()
  125. {
  126. SetAmount(SelectedItem.Count);
  127. }
  128. public void OnAmountChanged(string value)
  129. {
  130. if (value.IsEmpty()) return;
  131. SetAmount(int.Parse(value));
  132. }
  133. public void OnAmountEndEdit(string value)
  134. {
  135. if (value.IsEmpty())
  136. {
  137. SetAmount(1);
  138. }
  139. }
  140. public void Drop()
  141. {
  142. foreach (var item in PlayerInventory.Items.ToList())
  143. {
  144. if (item.Params.Type != ItemType.Currency && !item.Params.Tags.Contains(ItemTag.Quest))
  145. {
  146. #if TAP_HEROES
  147. if (item.Params.Class == ItemClass.Gunpowder) continue;
  148. #endif
  149. MoveItem(item, PlayerInventory, WarehouseInventory, item.Count);
  150. }
  151. }
  152. AudioSource.PlayOneShot(MoveSound, SfxVolume);
  153. }
  154. private void SetAmount(int amount)
  155. {
  156. Amount = Mathf.Max(1, Mathf.Min(SelectedItem.Count, amount));
  157. AmountInput?.SetTextWithoutNotify(Amount.ToString());
  158. ItemInfo.UpdatePrice(SelectedItem, SelectedItem.Params.Price * Amount, false);
  159. }
  160. protected virtual bool CanMoveSelectedItem()
  161. {
  162. return true;
  163. }
  164. }
  165. }