LootPack.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SoftKitty.InventoryEngine
  5. {
  6. public class LootPack : MonoBehaviour
  7. {
  8. #region Variables
  9. public string UID = "LootPack0000001";
  10. public List<int> ItemPool=new List<int>();
  11. public List<int> CurrencyMin = new List<int>();
  12. public List<int> CurrencyMax = new List<int>();
  13. public int MaxiumItemCount = 5;
  14. public int MaxiumCountEachItem = 3;
  15. public float DropChanceMultiplier = 1F;
  16. public bool RandomLevel = false;
  17. public bool DestoryWhenPlayerCloseLootWindow = true;
  18. private LootUi lootWindow;
  19. private InventoryHolder holder;
  20. private bool inited = false;
  21. private bool windowWasOpen = false;
  22. #endregion
  23. #region Internal Methods
  24. public static void GetRandomLoot(InventoryHolder _holder, List<int> _itemIdPool, int[] _currencyMinArray, int[] _currencyMaxArray, int _maxiumItemCount = 5, float _dropChanceMultiplier = 1F, bool _randomLevel=false, int _maxiumCountEachItem = 3)
  25. {
  26. List<Item> _dropList = new List<Item>();
  27. Random.InitState(System.DateTime.Now.Millisecond + System.DateTime.Now.Hour * 10000 + System.DateTime.Now.Minute * 1000);
  28. foreach (int obj in _itemIdPool)
  29. {
  30. if (Random.Range(0, 100) < ItemManager.itemDic[obj].dropRates * _dropChanceMultiplier)
  31. {
  32. Item _newItem = new Item(obj,true, true);
  33. if (_randomLevel && ItemManager.instance.EnableEnhancing && _newItem.type == ItemManager.instance.EnhancingCategoryID)
  34. {
  35. int _level = Random.Range(0, ItemManager.instance.MaxiumEnhancingLevel);
  36. if (Random.Range(0, 100) < ItemManager.instance.EnhancingSuccessCurve.Evaluate(_level*1F/ ItemManager.instance.MaxiumEnhancingLevel)) {
  37. _newItem.upgradeLevel = _level;
  38. }
  39. }
  40. _dropList.Add(_newItem);
  41. }
  42. }
  43. if (_dropList.Count <= 0)
  44. {
  45. Item _newItem = new Item(_itemIdPool[Random.Range(0, _itemIdPool.Count)], true, true);
  46. if (_randomLevel && ItemManager.instance.EnableEnhancing && _newItem.type == ItemManager.instance.EnhancingCategoryID)
  47. {
  48. int _level = Random.Range(0, ItemManager.instance.MaxiumEnhancingLevel);
  49. if (Random.Range(0, 100) < ItemManager.instance.EnhancingSuccessCurve.Evaluate(_level * 1F / ItemManager.instance.MaxiumEnhancingLevel))
  50. {
  51. _newItem.upgradeLevel = _level;
  52. }
  53. }
  54. _dropList.Add(_newItem);
  55. }
  56. _dropList.Sort(SortByRandom);
  57. int _dropCount = Mathf.Min(_dropList.Count, Random.Range(1, _maxiumItemCount + 1));
  58. _holder.InventorySize = _dropCount;
  59. for (int i = 0; i < _dropCount; i++)
  60. {
  61. _holder.Stacks.Add(new InventoryStack());
  62. }
  63. for (int i = 0; i < _dropCount; i++)
  64. {
  65. _holder.AddItem(_dropList[i], Random.Range(1, Mathf.Min(_dropList[i].maxiumStack, _maxiumCountEachItem) + 1));
  66. }
  67. for (int i = 0; i < Mathf.Min(_currencyMinArray.Length, _currencyMaxArray.Length, ItemManager.instance.currencies.Count); i++)
  68. {
  69. _holder.AddCurrency(i, Random.Range(_currencyMinArray[i], _currencyMaxArray[i]));
  70. }
  71. }
  72. private static int SortByRandom(Item a, Item b)
  73. {
  74. return Random.Range(0, 100).CompareTo(Random.Range(0, 100));
  75. }
  76. private void Update()
  77. {
  78. if (lootWindow != null)
  79. {
  80. windowWasOpen = true;
  81. if (lootWindow.isEmpty()) DestoryWhenPlayerCloseLootWindow = true;
  82. }
  83. else
  84. {
  85. if (windowWasOpen && DestoryWhenPlayerCloseLootWindow) DestroyPack();
  86. }
  87. }
  88. public void UpdatePrefab()
  89. {
  90. #if UNITY_EDITOR
  91. UnityEditor.EditorUtility.SetDirty(this);
  92. #endif
  93. }
  94. private void Init()
  95. {
  96. holder = gameObject.AddComponent<InventoryHolder>();
  97. holder.name = UID;
  98. GetRandomLoot(holder, ItemPool, CurrencyMin.ToArray(), CurrencyMax.ToArray(), MaxiumItemCount, DropChanceMultiplier,RandomLevel, MaxiumCountEachItem);
  99. inited = true;
  100. }
  101. #endregion
  102. //Call this to open the interface of this loot pack.
  103. public void OpenPack()
  104. {
  105. if (!inited) Init();
  106. if (lootWindow == null)
  107. {
  108. lootWindow = LootUi.ShowLoot(holder);
  109. }
  110. }
  111. //Destroy this loot pack
  112. public void DestroyPack()
  113. {
  114. Destroy(gameObject);
  115. }
  116. }
  117. }