BagController.cs 12 KB

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