BagController.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Common.Utility.CombatEvent;
  4. using Core.Event.Event;
  5. using Core.Utility;
  6. using Fort23.Core;
  7. using Fort23.UTool;
  8. using GameLogic.Hero;
  9. using UnityEngine;
  10. namespace GameLogic.Bag
  11. {
  12. public class BagController
  13. {
  14. /// <summary>
  15. /// 玩家的所有道具(包含货币)
  16. /// </summary>
  17. // private List<ItemInfo> m_bagList = new List<ItemInfo>();
  18. private Dictionary<string, ItemInfo> m_allBagDic = new Dictionary<string, ItemInfo>();
  19. /// <summary>
  20. /// 外部访问用
  21. /// </summary>
  22. public Dictionary<string, ItemInfo> allBagDic => m_allBagDic;
  23. public void Init()
  24. {
  25. foreach (var itemData in AccountFileInfo.Instance.playerData.ItemListData)
  26. {
  27. ItemInfo itemInfo = new ItemInfo(itemData);
  28. // m_bagList.Add(itemInfo);
  29. LogTool.Log(itemInfo.guid);
  30. if (!m_allBagDic.ContainsKey(itemInfo.guid))
  31. {
  32. m_allBagDic.Add(itemInfo.guid, itemInfo);
  33. }
  34. // PlayerManager.Instance.eqController.Init(itemInfo);
  35. }
  36. }
  37. #region 快速添加和扣除常用道具
  38. /// <summary>
  39. /// 加金币
  40. /// </summary>
  41. /// <param name="count"></param>
  42. public void AddCoin(long count)
  43. {
  44. AddItem(GlobalParam.Item_Coin_ID, count);
  45. }
  46. /// <summary>
  47. /// 扣金币
  48. /// </summary>
  49. /// <param name="count"></param>
  50. public bool DeductCoin(long count)
  51. {
  52. return DeductItem(GlobalParam.Item_Coin_ID, count);
  53. }
  54. /// <summary>
  55. /// 加钻石
  56. /// </summary>
  57. /// <param name="count"></param>
  58. public void AddDiamond(long count)
  59. {
  60. AddItem(GlobalParam.Item_Diamond_ID, count);
  61. }
  62. /// <summary>
  63. /// 扣钻石
  64. /// </summary>
  65. /// <param name="count"></param>
  66. public bool DeductDiamond(long count)
  67. {
  68. return DeductItem(GlobalParam.Item_Diamond_ID, count);
  69. }
  70. /// <summary>
  71. /// 加英雄经验
  72. /// </summary>
  73. /// <param name="count"></param>
  74. public void AddHeroExp(long count)
  75. {
  76. AddItem(GlobalParam.Item_HeroExp_ID, count);
  77. }
  78. /// <summary>
  79. /// 扣英雄经验
  80. /// </summary>
  81. /// <param name="count"></param>
  82. public bool DuctHeroExp(long count)
  83. {
  84. return DeductItem(GlobalParam.Item_HeroExp_ID, count);
  85. }
  86. /// <summary>
  87. /// 道具是否足够
  88. /// </summary>
  89. /// <param name="itemId"></param>
  90. /// <param name="count"></param>
  91. /// <returns></returns>
  92. public bool IsEnough(ItemInfo item, long count)
  93. {
  94. if (item.config.ID <= 0)
  95. {
  96. return false;
  97. }
  98. if (item.count >= count)
  99. {
  100. return true;
  101. }
  102. else
  103. {
  104. return false;
  105. }
  106. }
  107. /// <summary>
  108. /// 道具是否足够
  109. /// </summary>
  110. /// <param name="itemId"></param>
  111. /// <param name="count"></param>
  112. /// <returns></returns>
  113. public bool IsEnough(int itemId, long count)
  114. {
  115. ItemInfo item = GetItemInfo(itemId);
  116. return IsEnough(item, count);
  117. }
  118. #endregion
  119. public bool AddItem(int itemId, long count, string guid = "")
  120. {
  121. string guidStr = string.IsNullOrEmpty(guid) ? itemId.ToString() : guid;
  122. if (count <= 0)
  123. return false;
  124. ItemInfo item = GetItemInfo(itemId, guidStr);
  125. return AddItemCount(item, count);
  126. }
  127. /// <summary>
  128. /// 添加道具,装备和普通道具都可以
  129. /// </summary>
  130. /// <param name="itemInfos"></param>
  131. public void AddItem(List<ItemInfo> itemInfos)
  132. {
  133. for (int i = 0; i < itemInfos.Count; i++)
  134. {
  135. ItemInfo item = itemInfos[i];
  136. LogTool.Log("获得了道具" + item.itemID);
  137. AddItem(item);
  138. // //掉落的数量
  139. // long count = itemInfo.count.Value;
  140. // ItemInfo item = GetItemInfo(itemInfo);
  141. //
  142. // if (item.IsEquipItem())
  143. // {
  144. // PlayerManager.Instance.eqController.AddEquipment(item);
  145. // }
  146. //
  147. // AddItemCount(item, count);
  148. }
  149. }
  150. /// <summary>
  151. /// 添加道具数量,最后都走这里
  152. /// </summary>
  153. /// <param name="item"></param>
  154. /// <param name="count"></param>
  155. /// <returns></returns>
  156. private bool AddItemCount(ItemInfo item, long count)
  157. {
  158. if (item.config.ID <= 0 && string.IsNullOrEmpty(item.guid))
  159. {
  160. return false;
  161. }
  162. if (count <= 0)
  163. return false;
  164. //法宝
  165. if (item.config.itemTag == 11)
  166. {
  167. AccountFileInfo.FaBaoData faaData = AccountFileInfo.Instance.playerData.AllFaBaoDatas.FirstOrDefault(
  168. f =>
  169. f.id == item.config.associateVlaue[1]);
  170. if (faaData != null)
  171. {
  172. // item.isSuiPian = true;
  173. return AddItem(item.config.associateVlaue[0], item.config.associateVlaue[2] * count);
  174. }
  175. else
  176. {
  177. faaData = new AccountFileInfo.FaBaoData();
  178. faaData.id = item.config.associateVlaue[1];
  179. faaData.level = 1;
  180. faaData.useIndex = -1;
  181. AccountFileInfo.Instance.playerData.AllFaBaoDatas.Add(faaData);
  182. FaBaoInfo faBaoInfo = new FaBaoInfo(faaData);
  183. PlayerManager.Instance.FaBaoControl.AddFaBao(faBaoInfo);
  184. return true;
  185. }
  186. }
  187. //功法
  188. else if (item.config.itemTag == 13)
  189. {
  190. AccountFileInfo.SkillData skillData = AccountFileInfo.Instance.playerData.AllSkillDatas.FirstOrDefault(
  191. s =>
  192. s.id == item.config.associateVlaue[1]);
  193. if (skillData != null)
  194. {
  195. // item.isSuiPian = true;
  196. return AddItem(item.config.associateVlaue[0], item.config.associateVlaue[2] * count);
  197. }
  198. else
  199. {
  200. skillData = new AccountFileInfo.SkillData();
  201. skillData.id = item.config.associateVlaue[1];
  202. skillData.star = 1;
  203. skillData.level = 1;
  204. skillData.useIndex = -1;
  205. AccountFileInfo.Instance.playerData.AllSkillDatas.Add(skillData);
  206. SkillInfo skillInfo = new SkillInfo(skillData);
  207. skillInfo.index = skillData.useIndex;
  208. PlayerManager.Instance.GongFaControl.AddSkillInfo(skillInfo);
  209. return true;
  210. }
  211. }
  212. else if (item.config.associateID == 13)
  213. {
  214. PlayerManager.Instance.myHero.heroData.exp += count;
  215. AccountFileInfo.Instance.SavePlayerData();
  216. return true;
  217. }
  218. else if (item.config.itemTag == 9)
  219. {
  220. return AddItem(item.config.associateVlaue[0], item.config.associateVlaue[1]);
  221. }
  222. else if (item.config.itemTag == 28)
  223. {
  224. EventManager.Instance.Dispatch(CustomEventType.AddEvent,
  225. new DefaultEventData() { eventData = (int)count });
  226. return true;
  227. }
  228. item.count += count;
  229. EventManager.Instance.Dispatch(CustomEventType.ItemUpdate,
  230. new ItemUpdateData() { ItemInfo = item, Count = (int)count });
  231. EventManager.Instance.Dispatch(CustomEventType.AddItem,
  232. new ItemUpdateData() { ItemInfo = item, Count = (int)count });
  233. AccountFileInfo.Instance.SaveItemData(item);
  234. RedDotManager.Instance.AllRedDotUpDate();
  235. //合成免广告道具
  236. if (item.config.associateID == 10)
  237. {
  238. if (item.count >= item.config.associateVlaue[0])
  239. {
  240. AddItem(item.config.associateVlaue[1], 1);
  241. }
  242. }
  243. return true;
  244. }
  245. // private void AddEqToDic(ItemInfo eqItemInfo)
  246. // {
  247. // if (eqItemInfo.eqInfo == null)
  248. // {
  249. // return;
  250. // }
  251. //
  252. // int pro = eqItemInfo.eqInfo.basicEquipConfig.profession;
  253. //
  254. // if (m_EqDic.ContainsKey(pro))
  255. // {
  256. // m_EqDic[pro].Add(eqItemInfo);
  257. // }
  258. // else
  259. // {
  260. // m_EqDic.Add(pro, new List<ItemInfo>());
  261. // m_EqDic[pro].Add(eqItemInfo);
  262. // }
  263. // }
  264. /// <summary>
  265. /// 添加道具,装备和普通道具都可以
  266. /// </summary>
  267. /// <param name="item"></param>
  268. public void AddItem(ItemInfo item)
  269. {
  270. //掉落的数量
  271. long count = item.count.Value;
  272. ItemInfo itemInfo = GetItemInfo(item);
  273. // if (itemInfo.IsEquipItem())
  274. // {
  275. // PlayerManager.Instance.eqController.AddEquipment(itemInfo);
  276. // }
  277. //法宝
  278. if (item.config.itemTag == 11)
  279. {
  280. AccountFileInfo.FaBaoData faaData = AccountFileInfo.Instance.playerData.AllFaBaoDatas.FirstOrDefault(
  281. f =>
  282. f.id == item.config.associateVlaue[1]);
  283. if (faaData != null)
  284. {
  285. item.isSuiPian = true;
  286. }
  287. }
  288. //功法
  289. else if (item.config.itemTag == 13)
  290. {
  291. AccountFileInfo.SkillData skillData = AccountFileInfo.Instance.playerData.AllSkillDatas.FirstOrDefault(
  292. s =>
  293. s.id == item.config.associateVlaue[1]);
  294. if (skillData != null)
  295. {
  296. item.isSuiPian = true;
  297. }
  298. }
  299. AddItemCount(itemInfo, count);
  300. }
  301. // /// <summary>
  302. // /// 重新加载一次所有装备
  303. // /// 一般用于,分解装备之后
  304. // /// </summary>
  305. // public void ReInitAllEqItem()
  306. // {
  307. // PlayerManager.Instance.eqController.allZyEqDic.Clear();
  308. // foreach (KeyValuePair<string,ItemInfo> keyValuePair in m_allBagDic)
  309. // {
  310. // PlayerManager.Instance.eqController.AddEquipment(keyValuePair.Value);
  311. // }
  312. // }
  313. public void RemoveItem(ItemInfo itemInfo, bool saveNow, bool zeroDel)
  314. {
  315. itemInfo.count.Value = 0;
  316. if (zeroDel)
  317. {
  318. m_allBagDic.Remove(itemInfo.guid);
  319. }
  320. AccountFileInfo.Instance.SaveItemData(itemInfo, saveNow);
  321. }
  322. public void ModifyItem(ItemInfo itemInfo, bool saveNow)
  323. {
  324. itemInfo.count.Value -= itemInfo.count.Value;
  325. AccountFileInfo.Instance.SaveItemData(itemInfo, saveNow);
  326. }
  327. /// <summary>
  328. /// 获取道具信息
  329. /// 不传guid,默认itemId就是guid
  330. /// </summary>
  331. /// <param name="itemId"></param>
  332. /// <param name="guid"></param>
  333. /// <returns></returns>
  334. // public ItemInfo GetItemInfo(ItemInfo itemInfo)
  335. // {
  336. // string guidStr = string.IsNullOrEmpty(itemInfo.guid) ? itemInfo.itemID.ToString() : itemInfo.guid;
  337. //
  338. // if (m_allBagDic.TryGetValue(guidStr, out ItemInfo item))
  339. // {
  340. // return item;
  341. // }
  342. //
  343. // m_allBagDic.Add(itemInfo.guid, itemInfo);
  344. //
  345. // return itemInfo;
  346. // }
  347. /// <summary>
  348. /// 一般是掉了(随机)一个东西(ItemInfo),然后来背包找,是否之前掉过一样的
  349. /// 之前有,就把有的返回回去,加数量
  350. /// 之前无,就返回这个新东西
  351. /// /// 重要:加入背包管理都是通过这个方法(后续看是否优)
  352. /// </summary>
  353. /// <param name="itemInfo"></param>
  354. /// <returns></returns>
  355. public ItemInfo GetItemInfo(ItemInfo itemInfo)
  356. {
  357. string guidStr = string.IsNullOrEmpty(itemInfo.guid) ? itemInfo.itemID.ToString() : itemInfo.guid;
  358. if (m_allBagDic.TryGetValue(guidStr, out ItemInfo item))
  359. {
  360. return item;
  361. }
  362. //这里要设成0是因为,随机掉落的东西,第一次进背包时,先把数量去掉(否则会掉会多加1个)
  363. //该方法结束后,有统一加数量的代码,看后续代码。
  364. // itemInfo.count.Value = 0l;
  365. ItemInfo newIteminfo = new ItemInfo(itemInfo.itemID, 0);
  366. m_allBagDic.Add(itemInfo.guid, newIteminfo);
  367. return newIteminfo;
  368. }
  369. public long GetItemCount(int itemId)
  370. {
  371. ItemInfo itemInfo = GetItemInfo(itemId);
  372. if (itemInfo == null)
  373. {
  374. return 0;
  375. }
  376. return itemInfo.count.Value;
  377. }
  378. /// <summary>
  379. /// 获取道具信息
  380. /// 不传guid,默认itemId就是guid
  381. /// </summary>
  382. /// <param name="itemId"></param>
  383. /// <param name="guid"></param>
  384. /// <returns></returns>
  385. public ItemInfo GetItemInfo(int itemId, string guid = "")
  386. {
  387. string guidStr = string.IsNullOrEmpty(guid) ? itemId.ToString() : guid;
  388. if (m_allBagDic.TryGetValue(guidStr, out ItemInfo item))
  389. {
  390. return item;
  391. }
  392. ItemInfo newItem = new ItemInfo(itemId);
  393. if (newItem.config.ID > 0)
  394. {
  395. m_allBagDic.Add(newItem.guid, newItem);
  396. }
  397. else
  398. {
  399. LogTool.Error("没有找到这个道具:" + itemId);
  400. return null;
  401. }
  402. return newItem;
  403. }
  404. /// <summary>
  405. /// 扣除道具,最后都这里
  406. /// </summary>
  407. /// <param name="item"></param>
  408. /// <param name="count"></param>
  409. /// <returns></returns>
  410. public bool DeductItem(ItemInfo item, long count)
  411. {
  412. if (item == null)
  413. {
  414. return false;
  415. }
  416. if (item.config.ID <= 0)
  417. {
  418. return false;
  419. }
  420. if (item.count >= count)
  421. {
  422. item.count -= count;
  423. }
  424. else
  425. {
  426. return false;
  427. }
  428. EventManager.Instance.Dispatch(CustomEventType.ItemUpdate, new ItemUpdateData() { ItemInfo = item });
  429. AccountFileInfo.Instance.SaveItemData(item);
  430. RedDotManager.Instance.AllRedDotUpDate();
  431. return true;
  432. }
  433. public bool DeductItem(int itemId, long count)
  434. {
  435. ItemInfo item = GetItemInfo(itemId);
  436. return DeductItem(item, count);
  437. }
  438. public void DropHeroExp(Vector3 startPos_WorldPos, int showCount, int itemCount)
  439. {
  440. CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create();
  441. combatItemShowEventData.count = showCount;
  442. combatItemShowEventData.startPos_WorldPos = startPos_WorldPos;
  443. combatItemShowEventData.showName = "icon_res_upgrade_1";
  444. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.HeroExp;
  445. combatItemShowEventData.addValue = itemCount;
  446. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  447. }
  448. public void DropLevelExp(Vector3 startPos_WorldPos, int showCount, int itemCount)
  449. {
  450. CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create();
  451. combatItemShowEventData.count = showCount;
  452. combatItemShowEventData.startPos_WorldPos = startPos_WorldPos;
  453. combatItemShowEventData.showName = "icon_Energy";
  454. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.LevelExp;
  455. combatItemShowEventData.addValue = itemCount;
  456. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  457. }
  458. public void DropMonsterGold(Vector3 startPos_WorldPos, int showCount, int itemCount)
  459. {
  460. CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create();
  461. combatItemShowEventData.count = showCount;
  462. combatItemShowEventData.startPos_WorldPos = startPos_WorldPos;
  463. combatItemShowEventData.showName = "icon_Coin";
  464. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.MonsterGold;
  465. combatItemShowEventData.addValue = itemCount;
  466. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  467. }
  468. }
  469. }