BagController.cs 17 KB

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