BagController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using System.Collections.Generic;
  2. using Common.Utility.CombatEvent;
  3. using Core.Utility;
  4. using Fort23.Core;
  5. using GameLogic.Equipment;
  6. using UnityEngine;
  7. using Utility;
  8. namespace GameLogic.Bag
  9. {
  10. public class BagController : Singleton<BagController>
  11. {
  12. /// <summary>
  13. /// 玩家的所有道具(包含货币)
  14. /// </summary>
  15. // private List<ItemInfo> m_bagList = new List<ItemInfo>();
  16. private Dictionary<string, ItemInfo> m_allBagDic = new Dictionary<string, ItemInfo>();
  17. /// <summary>
  18. /// 外部访问用
  19. /// </summary>
  20. // public List<ItemInfo> BagList => m_bagList;
  21. public void Init()
  22. {
  23. foreach (var itemData in AccountFileInfo.Instance.playerData.ItemListData)
  24. {
  25. ItemInfo itemInfo = new ItemInfo(itemData);
  26. // m_bagList.Add(itemInfo);
  27. m_allBagDic.Add(itemInfo.guid, itemInfo);
  28. PlayerManager.Instance.eqController.Init(itemInfo);
  29. }
  30. }
  31. #region 快速添加和扣除常用道具
  32. /// <summary>
  33. /// 加金币
  34. /// </summary>
  35. /// <param name="count"></param>
  36. public void AddCoin(long count)
  37. {
  38. AddItem(GlobalParam.Item_Coin_ID, count);
  39. }
  40. /// <summary>
  41. /// 扣金币
  42. /// </summary>
  43. /// <param name="count"></param>
  44. public bool DeductCoin(long count)
  45. {
  46. return DeductItem(GlobalParam.Item_Coin_ID, count);
  47. }
  48. /// <summary>
  49. /// 加钻石
  50. /// </summary>
  51. /// <param name="count"></param>
  52. public void AddDiamond(long count)
  53. {
  54. AddItem(GlobalParam.Item_Diamond_ID, count);
  55. }
  56. /// <summary>
  57. /// 扣钻石
  58. /// </summary>
  59. /// <param name="count"></param>
  60. public bool DeductDiamond(long count)
  61. {
  62. return DeductItem(GlobalParam.Item_Diamond_ID, count);
  63. }
  64. /// <summary>
  65. /// 加英雄经验
  66. /// </summary>
  67. /// <param name="count"></param>
  68. public void AddHeroExp(long count)
  69. {
  70. AddItem(GlobalParam.Item_HeroExp_ID, count);
  71. }
  72. /// <summary>
  73. /// 扣英雄经验
  74. /// </summary>
  75. /// <param name="count"></param>
  76. public bool DuctHeroExp(long count)
  77. {
  78. return DeductItem(GlobalParam.Item_HeroExp_ID, count);
  79. }
  80. /// <summary>
  81. /// 道具是否足够
  82. /// </summary>
  83. /// <param name="itemId"></param>
  84. /// <param name="count"></param>
  85. /// <returns></returns>
  86. public bool IsEnough(ItemInfo item, long count)
  87. {
  88. if (item.config.ID <= 0)
  89. {
  90. return false;
  91. }
  92. if (item.count >= count)
  93. {
  94. return true;
  95. }
  96. else
  97. {
  98. return false;
  99. }
  100. }
  101. /// <summary>
  102. /// 道具是否足够
  103. /// </summary>
  104. /// <param name="itemId"></param>
  105. /// <param name="count"></param>
  106. /// <returns></returns>
  107. public bool IsEnough(int itemId, long count)
  108. {
  109. ItemInfo item = GetItemInfo(itemId);
  110. return IsEnough(item, count);
  111. }
  112. #endregion
  113. public bool AddItem(int itemId, long count)
  114. {
  115. ItemInfo item = GetItemInfo(itemId);
  116. return AddItem(item, count);
  117. }
  118. /// <summary>
  119. /// 添加道具,最后都走这里
  120. /// </summary>
  121. /// <param name="item"></param>
  122. /// <param name="count"></param>
  123. /// <returns></returns>
  124. private bool AddItem(ItemInfo item, long count)
  125. {
  126. if (item.config.ID <= 0 && string.IsNullOrEmpty(item.guid))
  127. {
  128. return false;
  129. }
  130. item.count += count;
  131. EventManager.Instance.Dispatch(CustomEventType.ItemUpdate, new ItemUpdateData() { ItemInfo = item });
  132. AccountFileInfo.Instance.SaveItemData(item);
  133. return true;
  134. }
  135. // private void AddEqToDic(ItemInfo eqItemInfo)
  136. // {
  137. // if (eqItemInfo.eqInfo == null)
  138. // {
  139. // return;
  140. // }
  141. //
  142. // int pro = eqItemInfo.eqInfo.basicEquipConfig.profession;
  143. //
  144. // if (m_EqDic.ContainsKey(pro))
  145. // {
  146. // m_EqDic[pro].Add(eqItemInfo);
  147. // }
  148. // else
  149. // {
  150. // m_EqDic.Add(pro, new List<ItemInfo>());
  151. // m_EqDic[pro].Add(eqItemInfo);
  152. // }
  153. // }
  154. /// <summary>
  155. /// 添加装备道具
  156. /// </summary>
  157. /// <param name="item"></param>
  158. public void AddEquipmentItem(ItemInfo item)
  159. {
  160. //掉落的数量
  161. long count = item.count.Value;
  162. // AddEqToDic(eqItemInfo);
  163. ItemInfo eqItemInfo = GetItemInfo(item);
  164. PlayerManager.Instance.eqController.AddEquipment(eqItemInfo);
  165. // m_bagEqList.Add(eqItemInfo);
  166. AddItem(eqItemInfo, count);
  167. }
  168. /// <summary>
  169. /// 一般是掉了(随机)一个东西(ItemInfo),然后来背包找,是否之前掉过一样的
  170. /// 一般用于有guid的item
  171. /// 比如掉了一个装备,装备有guid(组装的)
  172. /// 后续有其他类似装备这种,动态组织出来的有guid的物品,也可以走这里。宝石,词条等。
  173. /// </summary>
  174. /// <param name="itemInfo"></param>
  175. /// <returns></returns>
  176. public ItemInfo GetItemInfo(ItemInfo itemInfo)
  177. {
  178. string guidStr = string.IsNullOrEmpty(itemInfo.guid) ? itemInfo.itemID.ToString() : itemInfo.guid;
  179. if (m_allBagDic.TryGetValue(guidStr, out ItemInfo item))
  180. {
  181. return item;
  182. }
  183. //这里要设成0是因为,随机掉落的东西,第一次进背包时,先把数量去掉(否则会掉会多加1个)
  184. //该方法结束后,有统一加数量的代码,看后续代码。
  185. itemInfo.count.Value = 0l;
  186. m_allBagDic.Add(itemInfo.guid, itemInfo);
  187. return itemInfo;
  188. }
  189. /// <summary>
  190. /// 获取道具信息
  191. /// 不传guid,默认itemId就是guid
  192. /// </summary>
  193. /// <param name="itemId"></param>
  194. /// <param name="guid"></param>
  195. /// <returns></returns>
  196. public ItemInfo GetItemInfo(int itemId, string guid = "")
  197. {
  198. // for (int i = 0; i < m_bagList.Count; i++)
  199. // {
  200. // ItemInfo item = m_bagList[i];
  201. // if (item.itemID == itemId)
  202. // {
  203. // return item;
  204. // }
  205. // }
  206. string guidStr = string.IsNullOrEmpty(guid) ? itemId.ToString() : guid;
  207. if (m_allBagDic.TryGetValue(guidStr, out ItemInfo item))
  208. {
  209. return item;
  210. }
  211. ItemInfo newItem = new ItemInfo(itemId);
  212. if (newItem.config.ID > 0)
  213. {
  214. // m_bagList.Add(newItem);
  215. m_allBagDic.Add(newItem.guid, newItem);
  216. }
  217. return newItem;
  218. }
  219. /// <summary>
  220. /// 扣除道具,最后都这里
  221. /// </summary>
  222. /// <param name="item"></param>
  223. /// <param name="count"></param>
  224. /// <returns></returns>
  225. public bool DeductItem(ItemInfo item, long count)
  226. {
  227. if (item.config.ID <= 0)
  228. {
  229. return false;
  230. }
  231. if (item.count >= count)
  232. {
  233. item.count -= count;
  234. }
  235. else
  236. {
  237. return false;
  238. }
  239. EventManager.Instance.Dispatch(CustomEventType.ItemUpdate, new ItemUpdateData() { ItemInfo = item });
  240. AccountFileInfo.Instance.SaveItemData(item);
  241. return true;
  242. }
  243. public bool DeductItem(int itemId, long count)
  244. {
  245. ItemInfo item = GetItemInfo(itemId);
  246. return DeductItem(item, count);
  247. }
  248. public void DropHeroExp(Vector3 startPos_WorldPos, int showCount, int itemCount)
  249. {
  250. CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create();
  251. combatItemShowEventData.count = showCount;
  252. combatItemShowEventData.startPos_WorldPos = startPos_WorldPos;
  253. combatItemShowEventData.showName = "icon_res_upgrade_1";
  254. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.HeroExp;
  255. combatItemShowEventData.addValue = itemCount;
  256. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  257. }
  258. public void DropLevelExp(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_Energy";
  264. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.LevelExp;
  265. combatItemShowEventData.addValue = itemCount;
  266. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  267. }
  268. public void DropMonsterGold(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_Coin";
  274. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.MonsterGold;
  275. combatItemShowEventData.addValue = itemCount;
  276. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  277. }
  278. }
  279. }