BagController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using System.Collections.Generic;
  2. using Common.Utility.CombatEvent;
  3. using Core.Utility;
  4. using Fort23.Core;
  5. using GameLogic.Equipment;
  6. using UnityEngine;
  7. using Utility;
  8. namespace GameLogic.Bag
  9. {
  10. public class BagController : Singleton<BagController>
  11. {
  12. /// <summary>
  13. /// 玩家的所有道具(包含货币)
  14. /// </summary>
  15. // private List<ItemInfo> m_bagList = new List<ItemInfo>();
  16. private Dictionary<string, ItemInfo> m_allBagDic = new Dictionary<string, ItemInfo>();
  17. /// <summary>
  18. /// 外部访问用
  19. /// </summary>
  20. // public List<ItemInfo> BagList => m_bagList;
  21. public void Init()
  22. {
  23. foreach (var itemData in AccountFileInfo.Instance.playerData.ItemListData)
  24. {
  25. ItemInfo itemInfo = new ItemInfo(itemData);
  26. // m_bagList.Add(itemInfo);
  27. m_allBagDic.Add(itemInfo.guid, itemInfo);
  28. PlayerManager.Instance.eqController.Init(itemInfo);
  29. }
  30. }
  31. #region 快速添加和扣除常用道具
  32. /// <summary>
  33. /// 加金币
  34. /// </summary>
  35. /// <param name="count"></param>
  36. public void AddCoin(long count)
  37. {
  38. AddItem(GlobalParam.Item_Coin_ID, count);
  39. }
  40. /// <summary>
  41. /// 扣金币
  42. /// </summary>
  43. /// <param name="count"></param>
  44. public bool DeductCoin(long count)
  45. {
  46. return DeductItem(GlobalParam.Item_Coin_ID, count);
  47. }
  48. /// <summary>
  49. /// 加钻石
  50. /// </summary>
  51. /// <param name="count"></param>
  52. public void AddDiamond(long count)
  53. {
  54. AddItem(GlobalParam.Item_Diamond_ID, count);
  55. }
  56. /// <summary>
  57. /// 扣钻石
  58. /// </summary>
  59. /// <param name="count"></param>
  60. public bool DeductDiamond(long count)
  61. {
  62. return DeductItem(GlobalParam.Item_Diamond_ID, count);
  63. }
  64. /// <summary>
  65. /// 加英雄经验
  66. /// </summary>
  67. /// <param name="count"></param>
  68. public void AddHeroExp(long count)
  69. {
  70. AddItem(GlobalParam.Item_HeroExp_ID, count);
  71. }
  72. /// <summary>
  73. /// 扣英雄经验
  74. /// </summary>
  75. /// <param name="count"></param>
  76. public bool DuctHeroExp(long count)
  77. {
  78. return DeductItem(GlobalParam.Item_HeroExp_ID, count);
  79. }
  80. /// <summary>
  81. /// 道具是否足够
  82. /// </summary>
  83. /// <param name="itemId"></param>
  84. /// <param name="count"></param>
  85. /// <returns></returns>
  86. public bool IsEnough(ItemInfo item, long count)
  87. {
  88. if (item.config.ID <= 0)
  89. {
  90. return false;
  91. }
  92. if (item.count >= count)
  93. {
  94. return true;
  95. }
  96. else
  97. {
  98. return false;
  99. }
  100. }
  101. /// <summary>
  102. /// 道具是否足够
  103. /// </summary>
  104. /// <param name="itemId"></param>
  105. /// <param name="count"></param>
  106. /// <returns></returns>
  107. public bool IsEnough(int itemId, long count)
  108. {
  109. ItemInfo item = GetItemInfo(itemId);
  110. return IsEnough(item, count);
  111. }
  112. #endregion
  113. public bool AddItem(int itemId, long count)
  114. {
  115. ItemInfo item = GetItemInfo(itemId);
  116. return AddItem(item, count);
  117. }
  118. public void AddItem(List<ItemInfo> itemInfos)
  119. {
  120. for (int i = 0; i < itemInfos.Count; i++)
  121. {
  122. ItemInfo itemInfo = itemInfos[i];
  123. AddItem(itemInfo, itemInfo.count.Value);
  124. }
  125. }
  126. /// <summary>
  127. /// 添加道具,最后都走这里
  128. /// </summary>
  129. /// <param name="item"></param>
  130. /// <param name="count"></param>
  131. /// <returns></returns>
  132. private bool AddItem(ItemInfo item, long count)
  133. {
  134. if (item.config.ID <= 0 && string.IsNullOrEmpty(item.guid))
  135. {
  136. return false;
  137. }
  138. item.count += count;
  139. EventManager.Instance.Dispatch(CustomEventType.ItemUpdate, new ItemUpdateData() { ItemInfo = item });
  140. AccountFileInfo.Instance.SaveItemData(item);
  141. return true;
  142. }
  143. // private void AddEqToDic(ItemInfo eqItemInfo)
  144. // {
  145. // if (eqItemInfo.eqInfo == null)
  146. // {
  147. // return;
  148. // }
  149. //
  150. // int pro = eqItemInfo.eqInfo.basicEquipConfig.profession;
  151. //
  152. // if (m_EqDic.ContainsKey(pro))
  153. // {
  154. // m_EqDic[pro].Add(eqItemInfo);
  155. // }
  156. // else
  157. // {
  158. // m_EqDic.Add(pro, new List<ItemInfo>());
  159. // m_EqDic[pro].Add(eqItemInfo);
  160. // }
  161. // }
  162. /// <summary>
  163. /// 添加装备道具
  164. /// </summary>
  165. /// <param name="item"></param>
  166. public void AddEquipmentItem(ItemInfo item)
  167. {
  168. //掉落的数量
  169. long count = item.count.Value;
  170. // AddEqToDic(eqItemInfo);
  171. ItemInfo eqItemInfo = GetItemInfo(item);
  172. PlayerManager.Instance.eqController.AddEquipment(eqItemInfo);
  173. // m_bagEqList.Add(eqItemInfo);
  174. AddItem(eqItemInfo, count);
  175. }
  176. /// <summary>
  177. /// 一般是掉了(随机)一个东西(ItemInfo),然后来背包找,是否之前掉过一样的
  178. /// 一般用于有guid的item
  179. /// 比如掉了一个装备,装备有guid(组装的)
  180. /// 后续有其他类似装备这种,动态组织出来的有guid的物品,也可以走这里。宝石,词条等。
  181. /// </summary>
  182. /// <param name="itemInfo"></param>
  183. /// <returns></returns>
  184. public ItemInfo GetItemInfo(ItemInfo itemInfo)
  185. {
  186. string guidStr = string.IsNullOrEmpty(itemInfo.guid) ? itemInfo.itemID.ToString() : itemInfo.guid;
  187. if (m_allBagDic.TryGetValue(guidStr, out ItemInfo item))
  188. {
  189. return item;
  190. }
  191. //这里要设成0是因为,随机掉落的东西,第一次进背包时,先把数量去掉(否则会掉会多加1个)
  192. //该方法结束后,有统一加数量的代码,看后续代码。
  193. itemInfo.count.Value = 0l;
  194. m_allBagDic.Add(itemInfo.guid, itemInfo);
  195. return itemInfo;
  196. }
  197. /// <summary>
  198. /// 获取道具信息
  199. /// 不传guid,默认itemId就是guid
  200. /// </summary>
  201. /// <param name="itemId"></param>
  202. /// <param name="guid"></param>
  203. /// <returns></returns>
  204. public ItemInfo GetItemInfo(int itemId, string guid = "")
  205. {
  206. // for (int i = 0; i < m_bagList.Count; i++)
  207. // {
  208. // ItemInfo item = m_bagList[i];
  209. // if (item.itemID == itemId)
  210. // {
  211. // return item;
  212. // }
  213. // }
  214. string guidStr = string.IsNullOrEmpty(guid) ? itemId.ToString() : guid;
  215. if (m_allBagDic.TryGetValue(guidStr, out ItemInfo item))
  216. {
  217. return item;
  218. }
  219. ItemInfo newItem = new ItemInfo(itemId);
  220. if (newItem.config.ID > 0)
  221. {
  222. // m_bagList.Add(newItem);
  223. m_allBagDic.Add(newItem.guid, newItem);
  224. }
  225. return newItem;
  226. }
  227. /// <summary>
  228. /// 扣除道具,最后都这里
  229. /// </summary>
  230. /// <param name="item"></param>
  231. /// <param name="count"></param>
  232. /// <returns></returns>
  233. public bool DeductItem(ItemInfo item, long count)
  234. {
  235. if (item.config.ID <= 0)
  236. {
  237. return false;
  238. }
  239. if (item.count >= count)
  240. {
  241. item.count -= count;
  242. }
  243. else
  244. {
  245. return false;
  246. }
  247. EventManager.Instance.Dispatch(CustomEventType.ItemUpdate, new ItemUpdateData() { ItemInfo = item });
  248. AccountFileInfo.Instance.SaveItemData(item);
  249. return true;
  250. }
  251. public bool DeductItem(int itemId, long count)
  252. {
  253. ItemInfo item = GetItemInfo(itemId);
  254. return DeductItem(item, count);
  255. }
  256. public void DropHeroExp(Vector3 startPos_WorldPos, int showCount, int itemCount)
  257. {
  258. CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create();
  259. combatItemShowEventData.count = showCount;
  260. combatItemShowEventData.startPos_WorldPos = startPos_WorldPos;
  261. combatItemShowEventData.showName = "icon_res_upgrade_1";
  262. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.HeroExp;
  263. combatItemShowEventData.addValue = itemCount;
  264. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  265. }
  266. public void DropLevelExp(Vector3 startPos_WorldPos, int showCount, int itemCount)
  267. {
  268. CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create();
  269. combatItemShowEventData.count = showCount;
  270. combatItemShowEventData.startPos_WorldPos = startPos_WorldPos;
  271. combatItemShowEventData.showName = "icon_Energy";
  272. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.LevelExp;
  273. combatItemShowEventData.addValue = itemCount;
  274. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  275. }
  276. public void DropMonsterGold(Vector3 startPos_WorldPos, int showCount, int itemCount)
  277. {
  278. CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create();
  279. combatItemShowEventData.count = showCount;
  280. combatItemShowEventData.startPos_WorldPos = startPos_WorldPos;
  281. combatItemShowEventData.showName = "icon_Coin";
  282. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.MonsterGold;
  283. combatItemShowEventData.addValue = itemCount;
  284. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  285. }
  286. }
  287. }