BagController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Common.Utility.CombatEvent;
  4. using Core.Utility;
  5. using Fort23.Core;
  6. using Fort23.UTool;
  7. using UnityEngine;
  8. namespace GameLogic.Bag
  9. {
  10. public class 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. LogTool.Log(itemInfo.guid);
  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. if(count <= 0)
  118. return false;
  119. ItemInfo item = GetItemInfo(itemId, guidStr);
  120. return AddItemCount(item, count);
  121. }
  122. /// <summary>
  123. /// 添加道具,装备和普通道具都可以
  124. /// </summary>
  125. /// <param name="itemInfos"></param>
  126. public void AddItem(List<ItemInfo> itemInfos)
  127. {
  128. for (int i = 0; i < itemInfos.Count; i++)
  129. {
  130. ItemInfo item = itemInfos[i];
  131. LogTool.Log("获得了道具" + item.itemID);
  132. AddItem(item);
  133. // //掉落的数量
  134. // long count = itemInfo.count.Value;
  135. // ItemInfo item = GetItemInfo(itemInfo);
  136. //
  137. // if (item.IsEquipItem())
  138. // {
  139. // PlayerManager.Instance.eqController.AddEquipment(item);
  140. // }
  141. //
  142. // AddItemCount(item, count);
  143. }
  144. }
  145. /// <summary>
  146. /// 添加道具数量,最后都走这里
  147. /// </summary>
  148. /// <param name="item"></param>
  149. /// <param name="count"></param>
  150. /// <returns></returns>
  151. private bool AddItemCount(ItemInfo item, long count)
  152. {
  153. if (item.config.ID <= 0 && string.IsNullOrEmpty(item.guid))
  154. {
  155. return false;
  156. }
  157. if( count <= 0 )
  158. return false;
  159. //法宝
  160. if (item.config.itemTag == 11)
  161. {
  162. AccountFileInfo.FaBaoData faaData = AccountFileInfo.Instance.playerData.AllFaBaoDatas.FirstOrDefault(f =>
  163. f.id == item.config.associateVlaue[1]);
  164. if (faaData != null)
  165. {
  166. return AddItem(item.config.associateVlaue[0], 1);
  167. }
  168. else
  169. {
  170. faaData = new AccountFileInfo.FaBaoData();
  171. faaData.id =item.config.associateVlaue[1];
  172. faaData.level = 1;
  173. faaData.useIndex = -1;
  174. AccountFileInfo.Instance.playerData.AllFaBaoDatas.Add(faaData);
  175. return true;
  176. }
  177. }
  178. //功法
  179. else if (item.config.itemTag == 13)
  180. {
  181. AccountFileInfo.SkillData skillData = AccountFileInfo.Instance.playerData.AllSkillDatas.FirstOrDefault(s =>
  182. s.id == item.config.associateVlaue[1]);
  183. if (skillData != null)
  184. {
  185. return AddItem(item.config.associateVlaue[0], 1);
  186. }
  187. else
  188. {
  189. skillData = new AccountFileInfo.SkillData();
  190. skillData.id =item.config.associateVlaue[1];
  191. skillData.star = 1;
  192. skillData.level = 1;
  193. skillData.useIndex = -1;
  194. AccountFileInfo.Instance.playerData.AllSkillDatas.Add(skillData);
  195. return true;
  196. }
  197. }
  198. else if (item.config.itemTag == 9)
  199. {
  200. return AddItem(item.config.associateVlaue[0], item.config.associateVlaue[1]);
  201. }
  202. item.count += count;
  203. EventManager.Instance.Dispatch(CustomEventType.ItemUpdate, new ItemUpdateData() { ItemInfo = item, Count = (int)count });
  204. EventManager.Instance.Dispatch(CustomEventType.AddItem, new ItemUpdateData() { ItemInfo = item, Count = (int)count });
  205. AccountFileInfo.Instance.SaveItemData(item);
  206. return true;
  207. }
  208. // private void AddEqToDic(ItemInfo eqItemInfo)
  209. // {
  210. // if (eqItemInfo.eqInfo == null)
  211. // {
  212. // return;
  213. // }
  214. //
  215. // int pro = eqItemInfo.eqInfo.basicEquipConfig.profession;
  216. //
  217. // if (m_EqDic.ContainsKey(pro))
  218. // {
  219. // m_EqDic[pro].Add(eqItemInfo);
  220. // }
  221. // else
  222. // {
  223. // m_EqDic.Add(pro, new List<ItemInfo>());
  224. // m_EqDic[pro].Add(eqItemInfo);
  225. // }
  226. // }
  227. /// <summary>
  228. /// 添加道具,装备和普通道具都可以
  229. /// </summary>
  230. /// <param name="item"></param>
  231. public void AddItem(ItemInfo item)
  232. {
  233. //掉落的数量
  234. long count = item.count.Value;
  235. ItemInfo itemInfo = GetItemInfo(item);
  236. // if (itemInfo.IsEquipItem())
  237. // {
  238. // PlayerManager.Instance.eqController.AddEquipment(itemInfo);
  239. // }
  240. AddItemCount(itemInfo, count);
  241. }
  242. // /// <summary>
  243. // /// 重新加载一次所有装备
  244. // /// 一般用于,分解装备之后
  245. // /// </summary>
  246. // public void ReInitAllEqItem()
  247. // {
  248. // PlayerManager.Instance.eqController.allZyEqDic.Clear();
  249. // foreach (KeyValuePair<string,ItemInfo> keyValuePair in m_allBagDic)
  250. // {
  251. // PlayerManager.Instance.eqController.AddEquipment(keyValuePair.Value);
  252. // }
  253. // }
  254. public void RemoveItem(ItemInfo itemInfo, bool saveNow, bool zeroDel)
  255. {
  256. itemInfo.count.Value = 0;
  257. if (zeroDel)
  258. {
  259. m_allBagDic.Remove(itemInfo.guid);
  260. }
  261. AccountFileInfo.Instance.SaveItemData(itemInfo, saveNow);
  262. }
  263. public void ModifyItem(ItemInfo itemInfo, bool saveNow)
  264. {
  265. itemInfo.count.Value -= itemInfo.count.Value;
  266. AccountFileInfo.Instance.SaveItemData(itemInfo, saveNow);
  267. }
  268. /// <summary>
  269. /// 获取道具信息
  270. /// 不传guid,默认itemId就是guid
  271. /// </summary>
  272. /// <param name="itemId"></param>
  273. /// <param name="guid"></param>
  274. /// <returns></returns>
  275. // public ItemInfo GetItemInfo(ItemInfo itemInfo)
  276. // {
  277. // string guidStr = string.IsNullOrEmpty(itemInfo.guid) ? itemInfo.itemID.ToString() : itemInfo.guid;
  278. //
  279. // if (m_allBagDic.TryGetValue(guidStr, out ItemInfo item))
  280. // {
  281. // return item;
  282. // }
  283. //
  284. // m_allBagDic.Add(itemInfo.guid, itemInfo);
  285. //
  286. // return itemInfo;
  287. // }
  288. /// <summary>
  289. /// 一般是掉了(随机)一个东西(ItemInfo),然后来背包找,是否之前掉过一样的
  290. /// 之前有,就把有的返回回去,加数量
  291. /// 之前无,就返回这个新东西
  292. /// /// 重要:加入背包管理都是通过这个方法(后续看是否优)
  293. /// </summary>
  294. /// <param name="itemInfo"></param>
  295. /// <returns></returns>
  296. public ItemInfo GetItemInfo(ItemInfo itemInfo)
  297. {
  298. string guidStr = string.IsNullOrEmpty(itemInfo.guid) ? itemInfo.itemID.ToString() : itemInfo.guid;
  299. if (m_allBagDic.TryGetValue(guidStr, out ItemInfo item))
  300. {
  301. return item;
  302. }
  303. //这里要设成0是因为,随机掉落的东西,第一次进背包时,先把数量去掉(否则会掉会多加1个)
  304. //该方法结束后,有统一加数量的代码,看后续代码。
  305. itemInfo.count.Value = 0l;
  306. m_allBagDic.Add(itemInfo.guid, itemInfo);
  307. return itemInfo;
  308. }
  309. public long GetItemCount(int itemId)
  310. {
  311. ItemInfo itemInfo = GetItemInfo(itemId);
  312. if (itemInfo == null)
  313. {
  314. return 0;
  315. }
  316. return itemInfo.count.Value;
  317. }
  318. /// <summary>
  319. /// 获取道具信息
  320. /// 不传guid,默认itemId就是guid
  321. /// </summary>
  322. /// <param name="itemId"></param>
  323. /// <param name="guid"></param>
  324. /// <returns></returns>
  325. public ItemInfo GetItemInfo(int itemId, string guid = "")
  326. {
  327. string guidStr = string.IsNullOrEmpty(guid) ? itemId.ToString() : guid;
  328. if (m_allBagDic.TryGetValue(guidStr, out ItemInfo item))
  329. {
  330. return item;
  331. }
  332. ItemInfo newItem = new ItemInfo(itemId);
  333. if (newItem.config.ID > 0)
  334. {
  335. m_allBagDic.Add(newItem.guid, newItem);
  336. }
  337. else
  338. {
  339. LogTool.Error("没有找到这个道具:" + itemId);
  340. return null;
  341. }
  342. return newItem;
  343. }
  344. /// <summary>
  345. /// 扣除道具,最后都这里
  346. /// </summary>
  347. /// <param name="item"></param>
  348. /// <param name="count"></param>
  349. /// <returns></returns>
  350. public bool DeductItem(ItemInfo item, long count)
  351. {
  352. if (item == null)
  353. {
  354. return false;
  355. }
  356. if (item.config.ID <= 0)
  357. {
  358. return false;
  359. }
  360. if (item.count >= count)
  361. {
  362. item.count -= count;
  363. }
  364. else
  365. {
  366. return false;
  367. }
  368. EventManager.Instance.Dispatch(CustomEventType.ItemUpdate, new ItemUpdateData() { ItemInfo = item });
  369. AccountFileInfo.Instance.SaveItemData(item);
  370. return true;
  371. }
  372. public bool DeductItem(int itemId, long count)
  373. {
  374. ItemInfo item = GetItemInfo(itemId);
  375. return DeductItem(item, count);
  376. }
  377. public void DropHeroExp(Vector3 startPos_WorldPos, int showCount, int itemCount)
  378. {
  379. CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create();
  380. combatItemShowEventData.count = showCount;
  381. combatItemShowEventData.startPos_WorldPos = startPos_WorldPos;
  382. combatItemShowEventData.showName = "icon_res_upgrade_1";
  383. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.HeroExp;
  384. combatItemShowEventData.addValue = itemCount;
  385. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  386. }
  387. public void DropLevelExp(Vector3 startPos_WorldPos, int showCount, int itemCount)
  388. {
  389. CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create();
  390. combatItemShowEventData.count = showCount;
  391. combatItemShowEventData.startPos_WorldPos = startPos_WorldPos;
  392. combatItemShowEventData.showName = "icon_Energy";
  393. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.LevelExp;
  394. combatItemShowEventData.addValue = itemCount;
  395. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  396. }
  397. public void DropMonsterGold(Vector3 startPos_WorldPos, int showCount, int itemCount)
  398. {
  399. CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create();
  400. combatItemShowEventData.count = showCount;
  401. combatItemShowEventData.startPos_WorldPos = startPos_WorldPos;
  402. combatItemShowEventData.showName = "icon_Coin";
  403. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.MonsterGold;
  404. combatItemShowEventData.addValue = itemCount;
  405. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  406. }
  407. }
  408. }