BagController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 Dictionary<string, ItemInfo> allBagDic => m_allBagDic;
  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, string guid = "")
  114. {
  115. string guidStr = string.IsNullOrEmpty(guid) ? itemId.ToString() : guid;
  116. ItemInfo item = GetItemInfo(itemId, guidStr);
  117. return AddItem(item, count);
  118. }
  119. public void AddItem(List<ItemInfo> itemInfos)
  120. {
  121. for (int i = 0; i < itemInfos.Count; i++)
  122. {
  123. ItemInfo itemInfo = itemInfos[i];
  124. AddItem(itemInfo, itemInfo.count.Value);
  125. }
  126. }
  127. /// <summary>
  128. /// 添加道具,最后都走这里
  129. /// </summary>
  130. /// <param name="item"></param>
  131. /// <param name="count"></param>
  132. /// <returns></returns>
  133. private bool AddItem(ItemInfo item, long count)
  134. {
  135. if (item.config.ID <= 0 && string.IsNullOrEmpty(item.guid))
  136. {
  137. return false;
  138. }
  139. item.count += count;
  140. EventManager.Instance.Dispatch(CustomEventType.ItemUpdate, new ItemUpdateData() { ItemInfo = item });
  141. AccountFileInfo.Instance.SaveItemData(item);
  142. return true;
  143. }
  144. // private void AddEqToDic(ItemInfo eqItemInfo)
  145. // {
  146. // if (eqItemInfo.eqInfo == null)
  147. // {
  148. // return;
  149. // }
  150. //
  151. // int pro = eqItemInfo.eqInfo.basicEquipConfig.profession;
  152. //
  153. // if (m_EqDic.ContainsKey(pro))
  154. // {
  155. // m_EqDic[pro].Add(eqItemInfo);
  156. // }
  157. // else
  158. // {
  159. // m_EqDic.Add(pro, new List<ItemInfo>());
  160. // m_EqDic[pro].Add(eqItemInfo);
  161. // }
  162. // }
  163. /// <summary>
  164. /// 添加装备道具
  165. /// </summary>
  166. /// <param name="item"></param>
  167. public void AddEquipmentItem(ItemInfo item)
  168. {
  169. //掉落的数量
  170. long count = item.count.Value;
  171. // AddEqToDic(eqItemInfo);
  172. ItemInfo eqItemInfo = GetItemInfo(item);
  173. PlayerManager.Instance.eqController.AddEquipment(eqItemInfo);
  174. // m_bagEqList.Add(eqItemInfo);
  175. AddItem(eqItemInfo, count);
  176. }
  177. /// <summary>
  178. /// 重新加载一次所有装备
  179. /// 一般用于,分解装备之后
  180. /// </summary>
  181. public void ReInitAllEqItem()
  182. {
  183. PlayerManager.Instance.eqController.allZyEqDic.Clear();
  184. foreach (KeyValuePair<string,ItemInfo> keyValuePair in m_allBagDic)
  185. {
  186. PlayerManager.Instance.eqController.AddEquipment(keyValuePair.Value);
  187. }
  188. }
  189. public void RemoveItem(ItemInfo itemInfo, bool saveNow, bool zeroDel)
  190. {
  191. itemInfo.count.Value = 0;
  192. if (zeroDel)
  193. {
  194. m_allBagDic.Remove(itemInfo.guid);
  195. }
  196. AccountFileInfo.Instance.SaveItemData(itemInfo, saveNow);
  197. }
  198. public void ModifyItem(ItemInfo itemInfo, bool saveNow)
  199. {
  200. itemInfo.count.Value -= itemInfo.count.Value;
  201. AccountFileInfo.Instance.SaveItemData(itemInfo, saveNow);
  202. }
  203. /// <summary>
  204. /// 一般是掉了(随机)一个东西(ItemInfo),然后来背包找,是否之前掉过一样的
  205. /// 一般用于有guid的item
  206. /// 比如掉了一个装备,装备有guid(组装的)
  207. /// 后续有其他类似装备这种,动态组织出来的有guid的物品,也可以走这里。宝石,词条等。
  208. /// </summary>
  209. /// <param name="itemInfo"></param>
  210. /// <returns></returns>
  211. public ItemInfo GetItemInfo(ItemInfo itemInfo)
  212. {
  213. string guidStr = string.IsNullOrEmpty(itemInfo.guid) ? itemInfo.itemID.ToString() : itemInfo.guid;
  214. if (m_allBagDic.TryGetValue(guidStr, out ItemInfo item))
  215. {
  216. return item;
  217. }
  218. //这里要设成0是因为,随机掉落的东西,第一次进背包时,先把数量去掉(否则会掉会多加1个)
  219. //该方法结束后,有统一加数量的代码,看后续代码。
  220. itemInfo.count.Value = 0l;
  221. m_allBagDic.Add(itemInfo.guid, itemInfo);
  222. return itemInfo;
  223. }
  224. /// <summary>
  225. /// 获取道具信息
  226. /// 不传guid,默认itemId就是guid
  227. /// </summary>
  228. /// <param name="itemId"></param>
  229. /// <param name="guid"></param>
  230. /// <returns></returns>
  231. public ItemInfo GetItemInfo(int itemId, string guid = "")
  232. {
  233. // for (int i = 0; i < m_bagList.Count; i++)
  234. // {
  235. // ItemInfo item = m_bagList[i];
  236. // if (item.itemID == itemId)
  237. // {
  238. // return item;
  239. // }
  240. // }
  241. string guidStr = string.IsNullOrEmpty(guid) ? itemId.ToString() : guid;
  242. if (m_allBagDic.TryGetValue(guidStr, out ItemInfo item))
  243. {
  244. return item;
  245. }
  246. ItemInfo newItem = new ItemInfo(itemId);
  247. if (newItem.config.ID > 0)
  248. {
  249. // m_bagList.Add(newItem);
  250. m_allBagDic.Add(newItem.guid, newItem);
  251. }
  252. return newItem;
  253. }
  254. /// <summary>
  255. /// 扣除道具,最后都这里
  256. /// </summary>
  257. /// <param name="item"></param>
  258. /// <param name="count"></param>
  259. /// <returns></returns>
  260. public bool DeductItem(ItemInfo item, long count)
  261. {
  262. if (item.config.ID <= 0)
  263. {
  264. return false;
  265. }
  266. if (item.count >= count)
  267. {
  268. item.count -= count;
  269. }
  270. else
  271. {
  272. return false;
  273. }
  274. EventManager.Instance.Dispatch(CustomEventType.ItemUpdate, new ItemUpdateData() { ItemInfo = item });
  275. AccountFileInfo.Instance.SaveItemData(item);
  276. return true;
  277. }
  278. public bool DeductItem(int itemId, long count)
  279. {
  280. ItemInfo item = GetItemInfo(itemId);
  281. return DeductItem(item, count);
  282. }
  283. public void DropHeroExp(Vector3 startPos_WorldPos, int showCount, int itemCount)
  284. {
  285. CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create();
  286. combatItemShowEventData.count = showCount;
  287. combatItemShowEventData.startPos_WorldPos = startPos_WorldPos;
  288. combatItemShowEventData.showName = "icon_res_upgrade_1";
  289. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.HeroExp;
  290. combatItemShowEventData.addValue = itemCount;
  291. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  292. }
  293. public void DropLevelExp(Vector3 startPos_WorldPos, int showCount, int itemCount)
  294. {
  295. CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create();
  296. combatItemShowEventData.count = showCount;
  297. combatItemShowEventData.startPos_WorldPos = startPos_WorldPos;
  298. combatItemShowEventData.showName = "icon_Energy";
  299. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.LevelExp;
  300. combatItemShowEventData.addValue = itemCount;
  301. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  302. }
  303. public void DropMonsterGold(Vector3 startPos_WorldPos, int showCount, int itemCount)
  304. {
  305. CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create();
  306. combatItemShowEventData.count = showCount;
  307. combatItemShowEventData.startPos_WorldPos = startPos_WorldPos;
  308. combatItemShowEventData.showName = "icon_Coin";
  309. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.MonsterGold;
  310. combatItemShowEventData.addValue = itemCount;
  311. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  312. }
  313. }
  314. }