BagController.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 List<ItemInfo> m_bagEqList = new List<ItemInfo>();
  17. /// <summary>
  18. /// 玩家的装备(按职业分类)
  19. /// </summary>
  20. // private Dictionary<int, List<ItemInfo>> m_EqDic = new Dictionary<int, List<ItemInfo>>();
  21. /// <summary>
  22. /// 外部访问用
  23. /// </summary>
  24. // public Dictionary<int, List<ItemInfo>> EqDic => m_EqDic;
  25. /// <summary>
  26. /// 外部访问用
  27. /// </summary>
  28. public List<ItemInfo> BagList => m_bagList;
  29. public void Init()
  30. {
  31. foreach (var itemData in AccountFileInfo.Instance.playerData.ItemListData)
  32. {
  33. ItemInfo itemInfo = new ItemInfo(itemData);
  34. m_bagList.Add(itemInfo);
  35. PlayerManager.Instance.eqController.Init(itemInfo);
  36. // AddEqToDic(itemInfo);
  37. }
  38. }
  39. #region 快速添加和扣除常用道具
  40. /// <summary>
  41. /// 加金币
  42. /// </summary>
  43. /// <param name="count"></param>
  44. public void AddCoin(long count)
  45. {
  46. AddItem(GlobalParam.Item_Coin_ID, count);
  47. }
  48. /// <summary>
  49. /// 扣金币
  50. /// </summary>
  51. /// <param name="count"></param>
  52. public bool DeductCoin(long count)
  53. {
  54. return DeductItem(GlobalParam.Item_Coin_ID, count);
  55. }
  56. /// <summary>
  57. /// 加钻石
  58. /// </summary>
  59. /// <param name="count"></param>
  60. public void AddDiamond(long count)
  61. {
  62. AddItem(GlobalParam.Item_Diamond_ID, count);
  63. }
  64. /// <summary>
  65. /// 扣钻石
  66. /// </summary>
  67. /// <param name="count"></param>
  68. public bool DeductDiamond(long count)
  69. {
  70. return DeductItem(GlobalParam.Item_Diamond_ID, count);
  71. }
  72. /// <summary>
  73. /// 加英雄经验
  74. /// </summary>
  75. /// <param name="count"></param>
  76. public void AddHeroExp(long count)
  77. {
  78. AddItem(GlobalParam.Item_HeroExp_ID, count);
  79. }
  80. /// <summary>
  81. /// 扣英雄经验
  82. /// </summary>
  83. /// <param name="count"></param>
  84. public bool DuctHeroExp(long count)
  85. {
  86. return DeductItem(GlobalParam.Item_HeroExp_ID, count);
  87. }
  88. /// <summary>
  89. /// 道具是否足够
  90. /// </summary>
  91. /// <param name="itemId"></param>
  92. /// <param name="count"></param>
  93. /// <returns></returns>
  94. public bool IsEnough(ItemInfo item, long count)
  95. {
  96. if (item.config.ID <= 0)
  97. {
  98. return false;
  99. }
  100. if (item.count >= count)
  101. {
  102. return true;
  103. }
  104. else
  105. {
  106. return false;
  107. }
  108. }
  109. /// <summary>
  110. /// 道具是否足够
  111. /// </summary>
  112. /// <param name="itemId"></param>
  113. /// <param name="count"></param>
  114. /// <returns></returns>
  115. public bool IsEnough(int itemId, long count)
  116. {
  117. ItemInfo item = GetItemInfo(itemId);
  118. return IsEnough(item, count);
  119. }
  120. #endregion
  121. public bool AddItem(int itemId, long count)
  122. {
  123. ItemInfo item = GetItemInfo(itemId);
  124. return AddItem(item, count);
  125. }
  126. public void AddItem(List<ItemInfo> itemInfos)
  127. {
  128. for (int i = 0; i < itemInfos.Count; i++)
  129. {
  130. ItemInfo itemInfo = itemInfos[i];
  131. AddItem(itemInfo, itemInfo.count.Value);
  132. }
  133. }
  134. /// <summary>
  135. /// 添加道具,最后都走这里
  136. /// </summary>
  137. /// <param name="item"></param>
  138. /// <param name="count"></param>
  139. /// <returns></returns>
  140. private bool AddItem(ItemInfo item, long count)
  141. {
  142. if (item.config.ID <= 0 && string.IsNullOrEmpty(item.guid))
  143. {
  144. return false;
  145. }
  146. item.count += count;
  147. EventManager.Instance.Dispatch(CustomEventType.ItemUpdate, new ItemUpdateData() { ItemInfo = item });
  148. AccountFileInfo.Instance.SaveItemData(item);
  149. return true;
  150. }
  151. // private void AddEqToDic(ItemInfo eqItemInfo)
  152. // {
  153. // if (eqItemInfo.eqInfo == null)
  154. // {
  155. // return;
  156. // }
  157. //
  158. // int pro = eqItemInfo.eqInfo.basicEquipConfig.profession;
  159. //
  160. // if (m_EqDic.ContainsKey(pro))
  161. // {
  162. // m_EqDic[pro].Add(eqItemInfo);
  163. // }
  164. // else
  165. // {
  166. // m_EqDic.Add(pro, new List<ItemInfo>());
  167. // m_EqDic[pro].Add(eqItemInfo);
  168. // }
  169. // }
  170. /// <summary>
  171. /// 添加装备道具
  172. /// </summary>
  173. /// <param name="item"></param>
  174. public void AddEquipmentItem(ItemInfo item)
  175. {
  176. long count = item.count.Value;
  177. ItemInfo eqItemInfo = GetItemInfoWithGuid(item);
  178. // AddEqToDic(eqItemInfo);
  179. PlayerManager.Instance.eqController.AddEquipment(eqItemInfo);
  180. // m_bagEqList.Add(eqItemInfo);
  181. AddItem(eqItemInfo, count);
  182. }
  183. public ItemInfo GetItemInfoWithGuid(ItemInfo itemInfo)
  184. {
  185. for (int i = 0; i < m_bagList.Count; i++)
  186. {
  187. ItemInfo item = m_bagList[i];
  188. if (!string.IsNullOrEmpty(item.guid) && item.guid == itemInfo.guid)
  189. {
  190. return item;
  191. }
  192. }
  193. if (!string.IsNullOrEmpty(itemInfo.guid))
  194. {
  195. //这里要设成0是因为,装备已经掉出来了,有了数量,第一次进背包时,先把数量去掉(否则会掉一个得2个)
  196. //接下来会加回去,看后续代码。
  197. itemInfo.count.Value = 0;
  198. m_bagList.Add(itemInfo);
  199. }
  200. return itemInfo;
  201. }
  202. /// <summary>
  203. /// 扣除道具,最后都这里
  204. /// </summary>
  205. /// <param name="item"></param>
  206. /// <param name="count"></param>
  207. /// <returns></returns>
  208. public bool DeductItem(ItemInfo item, long count)
  209. {
  210. if (item.config.ID <= 0)
  211. {
  212. return false;
  213. }
  214. if (item.count >= count)
  215. {
  216. item.count -= count;
  217. }
  218. else
  219. {
  220. return false;
  221. }
  222. EventManager.Instance.Dispatch(CustomEventType.ItemUpdate, new ItemUpdateData() { ItemInfo = item });
  223. AccountFileInfo.Instance.SaveItemData(item);
  224. return true;
  225. }
  226. public bool DeductItem(int itemId, long count)
  227. {
  228. ItemInfo item = GetItemInfo(itemId);
  229. return DeductItem(item, count);
  230. }
  231. public ItemInfo GetItemInfo(int itemId)
  232. {
  233. for (int i = 0; i < m_bagList.Count; i++)
  234. {
  235. ItemInfo item = m_bagList[i];
  236. if (item.itemID == itemId)
  237. {
  238. return item;
  239. }
  240. }
  241. ItemInfo newItem = new ItemInfo(itemId);
  242. if (newItem.config.ID > 0)
  243. {
  244. m_bagList.Add(newItem);
  245. }
  246. return newItem;
  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. }