ItemManager.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SoftKitty.InventoryEngine
  5. {
  6. /// <summary>
  7. /// The ItemManager component on the "InventoryEngine" prefab contains all the settings for the entire system. Use ItemManager.instance to access its data.
  8. /// </summary>
  9. public class ItemManager : MonoBehaviour
  10. {
  11. #region Variables
  12. public List<ClickSetting> clickSettings = new List<ClickSetting>();
  13. public List<StringColorData> itemTypes = new List<StringColorData>();
  14. public List<StringColorData> itemQuality = new List<StringColorData>();
  15. public List<Attribute> itemAttributes = new List<Attribute>();
  16. public List<Enchantment> itemEnchantments = new List<Enchantment>();
  17. public List<Currency> currencies = new List<Currency>();
  18. public List<Item> items = new List<Item>();
  19. public string NameAttributeKey = "name";
  20. public string LevelAttributeKey = "lvl";
  21. public string XpAttributeKey = "xp";
  22. public string MaxXpAttributeKey = "mxp";
  23. public string CoolDownAttributeKey = "cd";
  24. public float SharedGlobalCoolDown = 0.5F;
  25. public Color AttributeNameColor=new Color(0.17F,0.53F,0.82F,1F);
  26. public bool UseQualityColorForItemName = true;
  27. public int MerchantStyle=0;
  28. public bool HighlightEquipmentSlotWhenHoverItem = true;
  29. public bool AllowDropItem = true;
  30. public string CanvasTag="";
  31. public bool EnableCrafting = true;
  32. public int CraftingMaterialCategoryID = 0;
  33. public string CraftingBlueprintTag = "Blueprint";
  34. public string PlayerName = "Player";
  35. public float CraftingTime = 0.5F;
  36. public Vector2[] EnhancingMaterials = new Vector2[2];
  37. public int EnhancingCurrencyType=0;
  38. public int EnhancingCurrencyNeed = 0;
  39. public bool EnableEnhancing = true;
  40. public bool DestroyEquipmentWhenFail = false;
  41. public int DestroyEquipmentWhenFailLevel = 3;
  42. public int MaxiumEnhancingLevel = 10;
  43. public AnimationCurve EnhancingSuccessCurve;
  44. public int EnhancingCategoryID = 0;
  45. public float EnhancingTime = 0.5F;
  46. public bool EnableEnhancingGlow = true;
  47. public AnimationCurve EnhancingGlowCurve;
  48. public Vector2 EnchantingMaterial = new Vector2(1,1);
  49. public int EnchantingCurrencyType = 0;
  50. public int EnchantingCurrencyNeed = 0;
  51. public Vector2 EnchantmentNumberRange = new Vector2(1,3);
  52. public bool EnableEnchanting = true;
  53. public bool RandomEnchantmentsForNewItem = false;
  54. public int EnchantingSuccessRate=30;
  55. public int EnchantingCategoryID = 0;
  56. public float EnchantingTime = 0.5F;
  57. public Color EnchantingPrefixesColor=new Color(0.32F, 0.55F,0.18F,1F);
  58. public Color EnchantingSuffxesColor = new Color(0.32F, 0.55F, 0.18F,1F);
  59. public Color EnchantingNameColor = new Color(0.32F, 0.55F, 0.18F,1F);
  60. public bool EnableSocketing = true;
  61. public string SocketingTagFilter = "";
  62. public int SocketingCategoryFilter = 0;
  63. public int SocketedCategoryFilter = 0;
  64. public bool EnableRemoveSocketing = true;
  65. public int RemoveSocketingPrice = 100;
  66. public int RemoveSocketingCurrency = 0;
  67. public bool DestroySocketItemWhenRemove = false;
  68. public bool RandomSocketingSlotsNummber = true;
  69. public int MinimalSocketingSlotsNumber = 0;
  70. public int MaxmiumSocketingSlotsNumber = 3;
  71. public bool LockSocketingSlotsByDefault = true;
  72. public int RandomChanceToLockSocketingSlots = 25;
  73. public int UnlockSocketingSlotsPrice = 50;
  74. public int UnlockSocketingSlotsCurrency = 0;
  75. #endregion
  76. #region Internal Methods
  77. void Awake()
  78. {
  79. if (instance != null)
  80. {
  81. Destroy(gameObject);
  82. return;
  83. }
  84. instance = this;
  85. GameObject.DontDestroyOnLoad(gameObject);
  86. itemTypesDic.Clear();
  87. itemQualityDic.Clear();
  88. itemDic.Clear();
  89. enchantmentDic.Clear();
  90. for (int i = 0; i < itemTypes.Count; i++)
  91. {
  92. itemTypesDic.Add(i, itemTypes[i]);
  93. }
  94. for (int i = 0; i < itemQuality.Count; i++)
  95. {
  96. itemQualityDic.Add(i, itemQuality[i]);
  97. }
  98. for (int i = 0; i < items.Count; i++)
  99. {
  100. itemDic.Add(items[i].uid, items[i]);
  101. }
  102. for (int i = 0; i < itemEnchantments.Count; i++)
  103. {
  104. enchantmentDic.Add(itemEnchantments[i].uid, itemEnchantments[i]);
  105. }
  106. SetCoolDownForAll(0F);
  107. }
  108. public void UpdatePrefab()
  109. {
  110. #if UNITY_EDITOR
  111. UnityEditor.EditorUtility.SetDirty(this);
  112. #endif
  113. }
  114. #endregion
  115. /// <summary>
  116. /// Get the instance of the ItemManager component, so you can access its data.
  117. /// </summary>
  118. public static ItemManager instance;
  119. public static void SetCoolDownForAll(float _coolDownTime, bool _onlyUseableItem = true)
  120. {
  121. foreach (var key in itemDic.Keys)
  122. {
  123. if (itemDic[key].useable || !_onlyUseableItem) itemDic[key].SetRemainCoolDownTime(_coolDownTime);
  124. }
  125. }
  126. public static void AddCoolDownForAll(float _addValue,bool _onlyUseableItem=true)
  127. {
  128. foreach (var key in itemDic.Keys)
  129. {
  130. if (itemDic[key].useable || !_onlyUseableItem) itemDic[key].AddRemainCoolDownTime(_addValue);
  131. }
  132. }
  133. public static void SetSharedGlobalCoolDown(float _coolDownTime, bool _onlyUseableItem = true)
  134. {
  135. foreach (var key in itemDic.Keys)
  136. {
  137. if (itemDic[key].GetRemainCoolDownTime()< _coolDownTime && (itemDic[key].useable || !_onlyUseableItem)) itemDic[key].SetRemainCoolDownTime(_coolDownTime);
  138. }
  139. }
  140. /// <summary>
  141. /// Access the [Item Catogory] data by the id.
  142. /// </summary>
  143. public static Dictionary<int, StringColorData> itemTypesDic = new Dictionary<int, StringColorData>();
  144. /// <summary>
  145. /// Access the [Item Quality] data by the id.
  146. /// </summary>
  147. public static Dictionary<int, StringColorData> itemQualityDic = new Dictionary<int, StringColorData>();
  148. /// <summary>
  149. /// Access the item data by its UID. Use itemDic[_uid].Copy() to get an instance of the item data with specified UID.
  150. /// </summary>
  151. public static Dictionary<int, Item> itemDic = new Dictionary<int, Item>();
  152. /// <summary>
  153. /// Access the [Item Enchantment] data by the id.
  154. /// </summary>
  155. public static Dictionary<int, Enchantment> enchantmentDic = new Dictionary<int, Enchantment>();
  156. /// <summary>
  157. /// Get the InventoryHolder component of the player's equipments.
  158. /// </summary>
  159. public static InventoryHolder PlayerEquipmentHolder;
  160. /// <summary>
  161. /// Get the InventoryHolder component of the player's inventory.
  162. /// </summary>
  163. public static InventoryHolder PlayerInventoryHolder;
  164. /// <summary>
  165. /// Try get the item data by uid.
  166. /// </summary>
  167. /// <param name="_id"></param>
  168. /// <returns></returns>
  169. public static Item TryGetItem(int _id)
  170. {
  171. if (instance == null) return null;
  172. if (_id < instance.items.Count && _id >= 0)
  173. {
  174. return instance.items[_id];
  175. }
  176. else
  177. {
  178. return null;
  179. }
  180. }
  181. /// <summary>
  182. /// Get the Attribute setting by its script key.
  183. /// </summary>
  184. /// <param name="_attributeKey"></param>
  185. /// <returns></returns>
  186. public Attribute GetAtttibute(string _attributeKey)
  187. {
  188. foreach (var obj in itemAttributes)
  189. {
  190. if (obj.key == _attributeKey) return obj;
  191. }
  192. return null;
  193. }
  194. /// <summary>
  195. /// The InventoryHolder of player will auto call this method on Awake(), but if you can call this if you want to manual set this.
  196. /// </summary>
  197. /// <param name="_equipmentHolder"></param>
  198. /// <param name="_inventoryHolder"></param>
  199. public static void SetPlayer(InventoryHolder _equipmentHolder, InventoryHolder _inventoryHolder)
  200. {
  201. PlayerEquipmentHolder = _equipmentHolder;
  202. PlayerInventoryHolder = _inventoryHolder;
  203. }
  204. /// <summary>
  205. /// Set the player's nick name, so when they craft item, their name will show as the creator name attribute.
  206. /// </summary>
  207. /// <param name="_name"></param>
  208. public static void SetPlayerName(string _name)
  209. {
  210. instance.PlayerName = _name;
  211. }
  212. }
  213. }