BagController.cs 9.9 KB

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