BagController.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. /// <summary>
  127. /// 添加道具,最后都走这里
  128. /// </summary>
  129. /// <param name="item"></param>
  130. /// <param name="count"></param>
  131. /// <returns></returns>
  132. private bool AddItem(ItemInfo item, long count)
  133. {
  134. if (item.config.ID <= 0 && string.IsNullOrEmpty(item.guid))
  135. {
  136. return false;
  137. }
  138. item.count += count;
  139. EventManager.Instance.Dispatch(CustomEventType.ItemUpdate, new ItemUpdateData() { ItemInfo = item });
  140. AccountFileInfo.Instance.SaveItemData(item);
  141. return true;
  142. }
  143. // private void AddEqToDic(ItemInfo eqItemInfo)
  144. // {
  145. // if (eqItemInfo.eqInfo == null)
  146. // {
  147. // return;
  148. // }
  149. //
  150. // int pro = eqItemInfo.eqInfo.basicEquipConfig.profession;
  151. //
  152. // if (m_EqDic.ContainsKey(pro))
  153. // {
  154. // m_EqDic[pro].Add(eqItemInfo);
  155. // }
  156. // else
  157. // {
  158. // m_EqDic.Add(pro, new List<ItemInfo>());
  159. // m_EqDic[pro].Add(eqItemInfo);
  160. // }
  161. // }
  162. /// <summary>
  163. /// 添加装备道具
  164. /// </summary>
  165. /// <param name="item"></param>
  166. public void AddEquipmentItem(ItemInfo item)
  167. {
  168. long count = item.count.Value;
  169. ItemInfo eqItemInfo = GetItemInfoWithGuid(item);
  170. // AddEqToDic(eqItemInfo);
  171. PlayerManager.Instance.eqController.AddEquipment(eqItemInfo);
  172. // m_bagEqList.Add(eqItemInfo);
  173. AddItem(eqItemInfo, count);
  174. }
  175. public ItemInfo GetItemInfoWithGuid(ItemInfo itemInfo)
  176. {
  177. for (int i = 0; i < m_bagList.Count; i++)
  178. {
  179. ItemInfo item = m_bagList[i];
  180. if (!string.IsNullOrEmpty(item.guid) && item.guid == itemInfo.guid)
  181. {
  182. return item;
  183. }
  184. }
  185. if (!string.IsNullOrEmpty(itemInfo.guid))
  186. {
  187. //这里要设成0是因为,装备已经掉出来了,有了数量,第一次进背包时,先把数量去掉(否则会掉一个得2个)
  188. //接下来会加回去,看后续代码。
  189. itemInfo.count.Value = 0;
  190. m_bagList.Add(itemInfo);
  191. }
  192. return itemInfo;
  193. }
  194. /// <summary>
  195. /// 扣除道具,最后都这里
  196. /// </summary>
  197. /// <param name="item"></param>
  198. /// <param name="count"></param>
  199. /// <returns></returns>
  200. public bool DeductItem(ItemInfo item, long count)
  201. {
  202. if (item.config.ID <= 0)
  203. {
  204. return false;
  205. }
  206. if (item.count >= count)
  207. {
  208. item.count -= count;
  209. }
  210. else
  211. {
  212. return false;
  213. }
  214. EventManager.Instance.Dispatch(CustomEventType.ItemUpdate, new ItemUpdateData() { ItemInfo = item });
  215. AccountFileInfo.Instance.SaveItemData(item);
  216. return true;
  217. }
  218. public bool DeductItem(int itemId, long count)
  219. {
  220. ItemInfo item = GetItemInfo(itemId);
  221. return DeductItem(item, count);
  222. }
  223. public ItemInfo GetItemInfo(int itemId)
  224. {
  225. for (int i = 0; i < m_bagList.Count; i++)
  226. {
  227. ItemInfo item = m_bagList[i];
  228. if (item.itemID == itemId)
  229. {
  230. return item;
  231. }
  232. }
  233. ItemInfo newItem = new ItemInfo(itemId);
  234. if (newItem.config.ID > 0)
  235. {
  236. m_bagList.Add(newItem);
  237. }
  238. return newItem;
  239. }
  240. public void DropHeroExp(Vector3 startPos_WorldPos, int showCount, int itemCount)
  241. {
  242. CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create();
  243. combatItemShowEventData.count = showCount;
  244. combatItemShowEventData.startPos_WorldPos = startPos_WorldPos;
  245. combatItemShowEventData.showName = "icon_res_upgrade_1";
  246. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.HeroExp;
  247. combatItemShowEventData.addValue = itemCount;
  248. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  249. }
  250. public void DropLevelExp(Vector3 startPos_WorldPos, int showCount, int itemCount)
  251. {
  252. CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create();
  253. combatItemShowEventData.count = showCount;
  254. combatItemShowEventData.startPos_WorldPos = startPos_WorldPos;
  255. combatItemShowEventData.showName = "icon_Energy";
  256. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.LevelExp;
  257. combatItemShowEventData.addValue = itemCount;
  258. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  259. }
  260. public void DropMonsterGold(Vector3 startPos_WorldPos, int showCount, int itemCount)
  261. {
  262. CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create();
  263. combatItemShowEventData.count = showCount;
  264. combatItemShowEventData.startPos_WorldPos = startPos_WorldPos;
  265. combatItemShowEventData.showName = "icon_Coin";
  266. combatItemShowEventData.showType = CombatItemShowEventData.ShowType.MonsterGold;
  267. combatItemShowEventData.addValue = itemCount;
  268. EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData);
  269. }
  270. }
  271. }