BagController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using System.Collections.Generic;
  2. using Common.Utility.CombatEvent;
  3. using Core.Utility;
  4. using Fort23.Core;
  5. using Fort23.UTool;
  6. using GameLogic.Equipment;
  7. using UnityEngine;
  8. using Utility;
  9. namespace GameLogic.Bag
  10. {
  11. public class BagController : Singleton<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 List<ItemInfo> BagList => m_bagList;
  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. 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)
  115. {
  116. ItemInfo item = GetItemInfo(itemId);
  117. return AddItem(item, count);
  118. }
  119. public void AddItem(List<ItemInfo> itemInfos)
  120. {
  121. for (int i = 0; i < itemInfos.Count; i++)
  122. {
  123. ItemInfo itemInfo = itemInfos[i];
  124. LogTool.Log("获得了道具"+itemInfo.itemID);
  125. AddItem(itemInfo, itemInfo.count.Value);
  126. }
  127. }
  128. /// <summary>
  129. /// 添加道具,最后都走这里
  130. /// </summary>
  131. /// <param name="item"></param>
  132. /// <param name="count"></param>
  133. /// <returns></returns>
  134. private bool AddItem(ItemInfo item, long count)
  135. {
  136. if (item.config.ID <= 0 && string.IsNullOrEmpty(item.guid))
  137. {
  138. return false;
  139. }
  140. item.count += count;
  141. EventManager.Instance.Dispatch(CustomEventType.ItemUpdate, new ItemUpdateData() { ItemInfo = item });
  142. AccountFileInfo.Instance.SaveItemData(item);
  143. return true;
  144. }
  145. // private void AddEqToDic(ItemInfo eqItemInfo)
  146. // {
  147. // if (eqItemInfo.eqInfo == null)
  148. // {
  149. // return;
  150. // }
  151. //
  152. // int pro = eqItemInfo.eqInfo.basicEquipConfig.profession;
  153. //
  154. // if (m_EqDic.ContainsKey(pro))
  155. // {
  156. // m_EqDic[pro].Add(eqItemInfo);
  157. // }
  158. // else
  159. // {
  160. // m_EqDic.Add(pro, new List<ItemInfo>());
  161. // m_EqDic[pro].Add(eqItemInfo);
  162. // }
  163. // }
  164. /// <summary>
  165. /// 添加装备道具
  166. /// </summary>
  167. /// <param name="item"></param>
  168. public void AddEquipmentItem(ItemInfo item)
  169. {
  170. //掉落的数量
  171. long count = item.count.Value;
  172. // AddEqToDic(eqItemInfo);
  173. ItemInfo eqItemInfo = GetItemInfo(item);
  174. PlayerManager.Instance.eqController.AddEquipment(eqItemInfo);
  175. // m_bagEqList.Add(eqItemInfo);
  176. AddItem(eqItemInfo, count);
  177. }
  178. /// <summary>
  179. /// 一般是掉了(随机)一个东西(ItemInfo),然后来背包找,是否之前掉过一样的
  180. /// 一般用于有guid的item
  181. /// 比如掉了一个装备,装备有guid(组装的)
  182. /// 后续有其他类似装备这种,动态组织出来的有guid的物品,也可以走这里。宝石,词条等。
  183. /// </summary>
  184. /// <param name="itemInfo"></param>
  185. /// <returns></returns>
  186. public ItemInfo GetItemInfo(ItemInfo itemInfo)
  187. {
  188. string guidStr = string.IsNullOrEmpty(itemInfo.guid) ? itemInfo.itemID.ToString() : itemInfo.guid;
  189. if (m_allBagDic.TryGetValue(guidStr, out ItemInfo item))
  190. {
  191. return item;
  192. }
  193. //这里要设成0是因为,随机掉落的东西,第一次进背包时,先把数量去掉(否则会掉会多加1个)
  194. //该方法结束后,有统一加数量的代码,看后续代码。
  195. itemInfo.count.Value = 0l;
  196. m_allBagDic.Add(itemInfo.guid, itemInfo);
  197. return itemInfo;
  198. }
  199. /// <summary>
  200. /// 获取道具信息
  201. /// 不传guid,默认itemId就是guid
  202. /// </summary>
  203. /// <param name="itemId"></param>
  204. /// <param name="guid"></param>
  205. /// <returns></returns>
  206. public ItemInfo GetItemInfo(int itemId, string guid = "")
  207. {
  208. // for (int i = 0; i < m_bagList.Count; i++)
  209. // {
  210. // ItemInfo item = m_bagList[i];
  211. // if (item.itemID == itemId)
  212. // {
  213. // return item;
  214. // }
  215. // }
  216. string guidStr = string.IsNullOrEmpty(guid) ? itemId.ToString() : guid;
  217. if (m_allBagDic.TryGetValue(guidStr, out ItemInfo item))
  218. {
  219. return item;
  220. }
  221. ItemInfo newItem = new ItemInfo(itemId);
  222. if (newItem.config.ID > 0)
  223. {
  224. // m_bagList.Add(newItem);
  225. m_allBagDic.Add(newItem.guid, newItem);
  226. }
  227. return newItem;
  228. }
  229. /// <summary>
  230. /// 扣除道具,最后都这里
  231. /// </summary>
  232. /// <param name="item"></param>
  233. /// <param name="count"></param>
  234. /// <returns></returns>
  235. public bool DeductItem(ItemInfo item, long count)
  236. {
  237. if (item.config.ID <= 0)
  238. {
  239. return false;
  240. }
  241. if (item.count >= count)
  242. {
  243. item.count -= count;
  244. }
  245. else
  246. {
  247. return false;
  248. }
  249. EventManager.Instance.Dispatch(CustomEventType.ItemUpdate, new ItemUpdateData() { ItemInfo = item });
  250. AccountFileInfo.Instance.SaveItemData(item);
  251. return true;
  252. }
  253. public bool DeductItem(int itemId, long count)
  254. {
  255. ItemInfo item = GetItemInfo(itemId);
  256. return DeductItem(item, count);
  257. }
  258. public void DropHeroExp(Vector3 startPos_WorldPos, int showCount, int itemCount)
  259. {
  260. CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create();
  261. combatItemShowEventData.count = showCount;
  262. combatItemShowEventData.startPos_WorldPos = startPos_WorldPos;
  263. combatItemShowEventData.showName = "icon_res_upgrade_1";
  264. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.HeroExp;
  265. combatItemShowEventData.addValue = itemCount;
  266. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  267. }
  268. public void DropLevelExp(Vector3 startPos_WorldPos, int showCount, int itemCount)
  269. {
  270. CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create();
  271. combatItemShowEventData.count = showCount;
  272. combatItemShowEventData.startPos_WorldPos = startPos_WorldPos;
  273. combatItemShowEventData.showName = "icon_Energy";
  274. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.LevelExp;
  275. combatItemShowEventData.addValue = itemCount;
  276. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  277. }
  278. public void DropMonsterGold(Vector3 startPos_WorldPos, int showCount, int itemCount)
  279. {
  280. CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create();
  281. combatItemShowEventData.count = showCount;
  282. combatItemShowEventData.startPos_WorldPos = startPos_WorldPos;
  283. combatItemShowEventData.showName = "icon_Coin";
  284. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.MonsterGold;
  285. combatItemShowEventData.addValue = itemCount;
  286. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  287. }
  288. }
  289. }