BagController.cs 18 KB

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