LootUi.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace SoftKitty.InventoryEngine
  6. {
  7. public class LootUi : ContainerBase
  8. {
  9. #region Variables
  10. public CurrenyInfo CurrencyPrefab;
  11. private List<CurrenyInfo> currencyList = new List<CurrenyInfo>();
  12. private float checkTime = 0F;
  13. #endregion
  14. #region MonoBehaviour
  15. public override void Update()
  16. {
  17. base.Update();
  18. checkTime = Mathf.MoveTowards(checkTime, 1F, Time.deltaTime * 2F);
  19. if (checkTime >= 1F)
  20. {
  21. checkTime = 0F;
  22. if (isEmpty()) Close();
  23. }
  24. }
  25. #endregion
  26. public static LootUi ShowLoot(InventoryHolder _holder)//Show the loot window
  27. {
  28. UiWindow _window = WindowsManager.GetWindow("Loot", _holder, true);
  29. _window.GetComponent<LootUi>().PopLoot(_holder);
  30. return _window.GetComponent<LootUi>();
  31. }
  32. public void PopLoot(InventoryHolder _holder)//Pop the window
  33. {
  34. Initialize(_holder, null);
  35. SoundManager.Play2D("LootPop");
  36. }
  37. public override void Initialize(InventoryHolder _inventoryHolder, InventoryHolder _equipHolder, string _name = "Loot")//Initialize the loot window
  38. {
  39. base.Initialize(_inventoryHolder, _equipHolder, _name);
  40. GetComponent<RectTransform>().sizeDelta = new Vector2(328F,122F+68F*Mathf.CeilToInt(Items.Length/4F));
  41. for (int i = 0; i < Holder.Currency.Count; i++)
  42. {
  43. if (Holder.Currency.GetCurrency(i,false) > 0)
  44. {
  45. GameObject _newItem = Instantiate(CurrencyPrefab.gameObject, CurrencyPrefab.transform.parent);
  46. _newItem.transform.localScale = Vector3.one;
  47. _newItem.SetActive(true);
  48. _newItem.GetComponent<CurrenyInfo>().Initialize(i, Holder);
  49. currencyList.Add(_newItem.GetComponent<CurrenyInfo>());
  50. }
  51. }
  52. inited = true;
  53. }
  54. public override void OnItemClick(int _index, int _button)//Callback for when player click an item
  55. {
  56. LootItem(_index);
  57. SoundManager.Play2D("Loot");
  58. }
  59. private void LootItem(int _index)//Move this item to player's inventory
  60. {
  61. if (!Items[_index].isEmpty() && Items[_index].GetItem()!=null)
  62. {
  63. DynamicMsg.PopItem(Items[_index].GetItem(), Items[_index].GetNumber());
  64. InventoryStack _leftStack = ItemManager.PlayerInventoryHolder.AddItem(Items[_index].GetItem(), Items[_index].GetNumber());
  65. Items[_index].Copy(_leftStack);
  66. checkTime = 0F;
  67. }
  68. }
  69. public void CollectAll()//Move all items and currencies to player's inventory
  70. {
  71. for (int i=0;i< Items.Length;i++) {
  72. LootItem(i);
  73. }
  74. foreach (var obj in currencyList) {
  75. CollectCurrency(obj);
  76. }
  77. SoundManager.Play2D("Loot");
  78. }
  79. public void CollectCurrency(CurrenyInfo _info)//Move this currency to player's inventory
  80. {
  81. if (_info.GetCurrencyValue()>0) {
  82. ItemManager.PlayerInventoryHolder.AddCurrency(_info.GetCurrencyId(), _info.GetCurrencyValue());
  83. Holder.AddCurrency(_info.GetCurrencyId(),-_info.GetCurrencyValue());
  84. checkTime = 0F;
  85. }
  86. SoundManager.Play2D("ItemDrop");
  87. }
  88. public bool isEmpty()//Check if everything is collected
  89. {
  90. bool _empty = true;
  91. foreach (var obj in Holder.Stacks) {
  92. if (!obj.Empty) _empty = false;
  93. }
  94. foreach (var obj in currencyList)
  95. {
  96. if (obj.GetCurrencyValue() > 0) _empty = false;
  97. }
  98. return _empty;
  99. }
  100. private void OnDestroy()//Destroy LootPack prefab when this window is closed.
  101. {
  102. if (isEmpty()) if(Holder!=null)Destroy(Holder.gameObject);
  103. }
  104. }
  105. }