GameCollectItem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using UnityEngine;
  2. using System;
  3. using System.Linq;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using Newtonsoft.Json.Linq;
  7. /// <summary>
  8. /// 玩家物品
  9. /// </summary>
  10. public class GameCollectItem
  11. {
  12. /// <summary>
  13. /// 物品
  14. /// </summary>
  15. public Dictionary<string, ItemVo> collectItemDic = new Dictionary<string, ItemVo>();
  16. /// <summary>
  17. /// 新道具记录
  18. /// </summary>
  19. public List<string> itemRecord = new List<string>();
  20. #region 七宗罪元素
  21. /// <summary>
  22. /// 七宗罪元素,这个不占背包格子,也不在背包里显示
  23. /// </summary>
  24. private Dictionary<string, ItemVo> collectElementDic = new Dictionary<string, ItemVo>();
  25. /// <summary>
  26. /// 刷新背包七宗罪元素数据
  27. /// </summary>
  28. /// <param name="data"></param>
  29. public void RefreshElement(JObject data)
  30. {
  31. if (data != null)
  32. {
  33. collectElementDic.Clear();
  34. LogHelper.Log(data);
  35. foreach (JProperty temp in data.Properties())
  36. {
  37. var itemtemp = new ItemVo();
  38. itemtemp.typeId = temp.Name;
  39. itemtemp.count = temp.Value.ToObject<int>();
  40. collectElementDic.Add(itemtemp.typeId, itemtemp);
  41. }
  42. }
  43. }
  44. /// <summary>
  45. /// 获取七宗罪元素的数据
  46. /// </summary>
  47. /// <returns></returns>
  48. public Dictionary<string, ItemVo> GetElementDic()
  49. {
  50. Dictionary<string, ItemVo> temp = new Dictionary<string, ItemVo>();
  51. foreach (string key in collectElementDic.Keys)
  52. {
  53. ItemVo vo = collectElementDic[key];
  54. temp.Add(vo.typeId, vo);
  55. }
  56. return temp;
  57. }
  58. #endregion
  59. /// <summary>
  60. /// 初始化已经拥有的物品数据
  61. /// </summary>
  62. /// <param name="data"></param>
  63. public void InitItemData(JObject data)
  64. {
  65. if (data["items"] != null)
  66. {
  67. collectItemDic.Clear();
  68. JObject items = (JObject)data["items"];
  69. foreach (JProperty temp in items.Properties())
  70. {
  71. // 普通道具只有id 和数量 -- gwang 2019年11月14日10:25:27
  72. var itemtemp = new ItemVo();
  73. itemtemp.typeId = temp.Name;
  74. itemtemp.count = temp.Value.ToObject<int>();
  75. if (itemtemp.count > 0)
  76. {
  77. collectItemDic.Add(itemtemp.typeId, itemtemp);
  78. }
  79. }
  80. }
  81. //if (data["segement"] != null) // 碎片模块单独出去了 --gwang 2019年11月14日10:25:19
  82. //{
  83. //}
  84. if (data["itemRecord"] != null)
  85. {
  86. this.itemRecord.Clear();
  87. JArray arr = JArray.Parse(data["itemRecord"].ToString());
  88. foreach (JToken id in arr)
  89. {
  90. this.itemRecord.Add(id.ToString());
  91. }
  92. }
  93. if (data["element"] != null)
  94. {
  95. collectElementDic.Clear();
  96. JObject items = (JObject)data["element"];
  97. foreach (JProperty temp in items.Properties())
  98. {
  99. // 七宗罪元素也只有id和数量 --gwang 2019年11月14日10:25:10
  100. var itemtemp = new ItemVo();
  101. itemtemp.typeId = temp.Name;
  102. itemtemp.count = temp.Value.ToObject<int>();
  103. collectElementDic.Add(itemtemp.typeId, itemtemp);
  104. }
  105. }
  106. #region 注释
  107. //if (data["equipment"] != null)
  108. //{
  109. // JObject equipment = (JObject)data["equipment"]; // {uid:{typeId:"",level:x,...},uid:{...},...}
  110. // foreach (JProperty temp in equipment.Properties())
  111. // {
  112. // #region 旧代码
  113. // //var itemtemp = new ItemVo();
  114. // //itemtemp.typeId = temp.Value["typeId"].ToObject<string>(); // typeId
  115. // //// 装备类道具, 额外存储了level、melt_level和吃的经验数据(未升级或强化过的不存储此字段). --gwang 2019年11月14日10:25:01
  116. // //if (temp.Value["level"] != null)
  117. // //{
  118. // // itemtemp.level = temp.Value["level"].ToObject<int>();
  119. // //}
  120. // //if (temp.Value["melt_level"] != null)
  121. // //{
  122. // // itemtemp.melt_level = temp.Value["melt_level"].ToObject<int>();
  123. // //}
  124. // //if (temp.Value["self_exp"] != null)
  125. // //{
  126. // // itemtemp.self_exp = temp.Value["self_exp"].ToObject<int>();
  127. // //}
  128. // //else
  129. // //{
  130. // // itemtemp.self_exp = 0;
  131. // //}
  132. // #endregion
  133. // var itemtemp = new EquipeMentVo();
  134. // itemtemp.typeId = temp.Value["typeId"].ToObject<string>(); // typeId
  135. // { // 武器专属字段,不升级之前没有初始化 (不好,要改-wg)
  136. // if (temp.Value["isLocked"] != null)
  137. // {
  138. // itemtemp.isLocked = temp.Value["isLocked"].ToObject<int>();
  139. // }
  140. // if (temp.Value["isUsing"] != null)
  141. // {
  142. // itemtemp.WhoIsUsing = temp.Value["isUsing"].ToObject<int>();
  143. // }
  144. // if (temp.Value["level"] != null)
  145. // {
  146. // itemtemp.level = temp.Value["level"].ToObject<int>();
  147. // }
  148. // if (temp.Value["maxStar"] != null)
  149. // {
  150. // itemtemp.maxStar = temp.Value["maxStar"].ToObject<int>();
  151. // }
  152. // if (temp.Value["starLevel"] != null)
  153. // {
  154. // itemtemp.starLevel = temp.Value["starLevel"].ToObject<int>();
  155. // }
  156. // }
  157. // itemtemp.uid = temp.Name; // uid
  158. // collectItemDic.Add(itemtemp.uid, itemtemp); // 这里, 装备index从1开始,其他道具按照6位的编号作为索引,应该不至于冲突 --王刚 2019年11月14日10:31:19
  159. // // 另外一个存在uid的就是英雄了, 从10001开始,5位编码,整个10001到99999段都没有别人用,应该也不至于不够用或者冲突了
  160. // }
  161. //}
  162. #endregion
  163. }
  164. /// <summary>
  165. /// 根据typeID来查询物品的数据信息
  166. /// </summary>
  167. /// <param name="type">类型或者uid</param>
  168. /// <returns> </returns>
  169. internal ItemVo GetRealItemInfoByType(string type)
  170. {
  171. if (collectItemDic.ContainsKey(type))
  172. {
  173. return collectItemDic[type];
  174. }
  175. return null;
  176. }
  177. /// <summary>
  178. /// 按子类型查询物品
  179. /// </summary>
  180. /// <param name="_type">子类型为None时返回全部道具</param>
  181. /// <returns></returns>
  182. public List<ItemVo> GetItemList(EItemSubType _type = EItemSubType.None)
  183. {
  184. if (_type == EItemSubType.None) { // 限定种类
  185. return collectItemDic.Values.ToList();
  186. }
  187. return collectItemDic.Values.Where(item => item.nMo.subType == (int)_type).ToList();
  188. }
  189. /// <summary>
  190. /// 取所有武器
  191. /// </summary>
  192. /// <returns></returns>
  193. public List<UserEquipmentVo> GetWeaponList()
  194. {
  195. return UserProxy.Instance.player.collectEquip.equipments.Values.ToList();
  196. }
  197. /// <summary>
  198. /// 添加物品
  199. /// </summary>
  200. /// <param name="ItemId"></param>
  201. /// <param name="num"></param>
  202. public void AddItems(string ItemId, int num) {
  203. if (collectItemDic.ContainsKey(ItemId))
  204. {
  205. collectItemDic[ItemId].count += num;
  206. }
  207. else {
  208. collectElementDic.Add(ItemId, new ItemVo() { typeId = ItemId, count = num });
  209. }
  210. }
  211. /// <summary>
  212. /// 检测是否有足够的道具
  213. /// </summary>
  214. /// <param name="type"></param>
  215. /// <param name="num"></param>
  216. /// <returns></returns>
  217. public bool CheckHasEngoughItem(string type, int num)
  218. {
  219. if (collectItemDic.ContainsKey(type))
  220. {
  221. if (collectItemDic[type].count >= num)
  222. {
  223. return true;
  224. }
  225. }
  226. return false;
  227. }
  228. /// <summary>
  229. /// 返回指定物品的数量
  230. /// </summary>
  231. /// <param name="type">typeid</param>
  232. /// <returns></returns>
  233. public int GetItemCount(string type)
  234. {
  235. if (collectItemDic.ContainsKey(type))
  236. {
  237. return collectItemDic[type].count;
  238. }
  239. return 0;
  240. }
  241. /// <summary>
  242. /// 消耗背包中的道具
  243. /// </summary>
  244. /// <param name="itemid"></param>
  245. /// <param name="num"></param>
  246. public void ConsumePacketItem(string itemid, int num)
  247. {
  248. if (collectItemDic.ContainsKey(itemid))
  249. {
  250. int curNum = collectItemDic[itemid].count;
  251. collectItemDic[itemid].count = curNum - num;
  252. if (collectItemDic[itemid].count <= 0)
  253. {
  254. collectItemDic.Remove(itemid);
  255. }
  256. }
  257. }
  258. public List<ItemVo> GetStoreData(EItemSubType _type = EItemSubType.None)
  259. {
  260. if (_type == EItemSubType.None)
  261. { // 限定种类
  262. return collectItemDic.Values.ToList();
  263. }
  264. List<ItemVo> list = new List<ItemVo>();
  265. List<ItemVo> tempList = new List<ItemVo>();
  266. foreach (KeyValuePair<string, ItemVo> kv in this.collectItemDic)
  267. {
  268. if (kv.Value.nMo.subType != (int)_type)
  269. {
  270. continue;
  271. }
  272. if (kv.Value.count > 99999)
  273. {
  274. int n = kv.Value.count - 99999;
  275. kv.Value.count = 99999;
  276. list.Add(kv.Value);
  277. ItemVo vo = new ItemVo();
  278. vo.typeId = kv.Key;
  279. vo.count = n;
  280. list.Add(vo);
  281. }
  282. else
  283. {
  284. list.Add(kv.Value);
  285. }
  286. }
  287. return list;
  288. }
  289. #region
  290. /// <summary>
  291. /// 物品的相关数据发生变化
  292. /// 通知UI层,重新刷新显示的事件触发
  293. /// </summary>
  294. public System.EventHandler RefreshPacketUIDataViewEvent;
  295. public System.EventHandler RefreshPropertyUIDataViewEvent;
  296. public void RefreshUI(string even)
  297. {
  298. //---------------Event触发通知---------------------
  299. if (RefreshPacketUIDataViewEvent != null)
  300. {
  301. RefreshPacketUIDataViewEvent.Invoke(even, EventArgs.Empty);
  302. }
  303. }
  304. public void RefreshPropertyUI(string even = null)
  305. {
  306. //---------------Event触发通知---------------------
  307. if (RefreshPropertyUIDataViewEvent != null)
  308. {
  309. RefreshPropertyUIDataViewEvent.Invoke(even, EventArgs.Empty);
  310. }
  311. }
  312. #endregion
  313. }