BagController.cs 16 KB

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