123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- using UnityEngine;
- using System;
- using System.Linq;
- using System.Collections;
- using System.Collections.Generic;
- using Newtonsoft.Json.Linq;
- /// <summary>
- /// 玩家物品
- /// </summary>
- public class GameCollectItem
- {
- /// <summary>
- /// 物品
- /// </summary>
- public Dictionary<string, ItemVo> collectItemDic = new Dictionary<string, ItemVo>();
- /// <summary>
- /// 新道具记录
- /// </summary>
- public List<string> itemRecord = new List<string>();
- #region 七宗罪元素
- /// <summary>
- /// 七宗罪元素,这个不占背包格子,也不在背包里显示
- /// </summary>
- private Dictionary<string, ItemVo> collectElementDic = new Dictionary<string, ItemVo>();
- /// <summary>
- /// 刷新背包七宗罪元素数据
- /// </summary>
- /// <param name="data"></param>
- public void RefreshElement(JObject data)
- {
- if (data != null)
- {
- collectElementDic.Clear();
- LogHelper.Log(data);
- foreach (JProperty temp in data.Properties())
- {
- var itemtemp = new ItemVo();
- itemtemp.typeId = temp.Name;
- itemtemp.count = temp.Value.ToObject<int>();
- collectElementDic.Add(itemtemp.typeId, itemtemp);
- }
- }
- }
- /// <summary>
- /// 获取七宗罪元素的数据
- /// </summary>
- /// <returns></returns>
- public Dictionary<string, ItemVo> GetElementDic()
- {
- Dictionary<string, ItemVo> temp = new Dictionary<string, ItemVo>();
- foreach (string key in collectElementDic.Keys)
- {
- ItemVo vo = collectElementDic[key];
- temp.Add(vo.typeId, vo);
- }
- return temp;
- }
- #endregion
- /// <summary>
- /// 初始化已经拥有的物品数据
- /// </summary>
- /// <param name="data"></param>
- public void InitItemData(JObject data)
- {
- if (data["items"] != null)
- {
- collectItemDic.Clear();
- JObject items = (JObject)data["items"];
- foreach (JProperty temp in items.Properties())
- {
- // 普通道具只有id 和数量 -- gwang 2019年11月14日10:25:27
- var itemtemp = new ItemVo();
- itemtemp.typeId = temp.Name;
- itemtemp.count = temp.Value.ToObject<int>();
- if (itemtemp.count > 0)
- {
- collectItemDic.Add(itemtemp.typeId, itemtemp);
- }
- }
- }
- //if (data["segement"] != null) // 碎片模块单独出去了 --gwang 2019年11月14日10:25:19
- //{
- //}
- if (data["itemRecord"] != null)
- {
- this.itemRecord.Clear();
- JArray arr = JArray.Parse(data["itemRecord"].ToString());
- foreach (JToken id in arr)
- {
- this.itemRecord.Add(id.ToString());
- }
- }
- if (data["element"] != null)
- {
- collectElementDic.Clear();
- JObject items = (JObject)data["element"];
- foreach (JProperty temp in items.Properties())
- {
- // 七宗罪元素也只有id和数量 --gwang 2019年11月14日10:25:10
- var itemtemp = new ItemVo();
- itemtemp.typeId = temp.Name;
- itemtemp.count = temp.Value.ToObject<int>();
- collectElementDic.Add(itemtemp.typeId, itemtemp);
- }
- }
- #region 注释
- //if (data["equipment"] != null)
- //{
- // JObject equipment = (JObject)data["equipment"]; // {uid:{typeId:"",level:x,...},uid:{...},...}
- // foreach (JProperty temp in equipment.Properties())
- // {
- // #region 旧代码
- // //var itemtemp = new ItemVo();
- // //itemtemp.typeId = temp.Value["typeId"].ToObject<string>(); // typeId
- // //// 装备类道具, 额外存储了level、melt_level和吃的经验数据(未升级或强化过的不存储此字段). --gwang 2019年11月14日10:25:01
- // //if (temp.Value["level"] != null)
- // //{
- // // itemtemp.level = temp.Value["level"].ToObject<int>();
- // //}
- // //if (temp.Value["melt_level"] != null)
- // //{
- // // itemtemp.melt_level = temp.Value["melt_level"].ToObject<int>();
- // //}
- // //if (temp.Value["self_exp"] != null)
- // //{
- // // itemtemp.self_exp = temp.Value["self_exp"].ToObject<int>();
- // //}
- // //else
- // //{
- // // itemtemp.self_exp = 0;
- // //}
- // #endregion
- // var itemtemp = new EquipeMentVo();
- // itemtemp.typeId = temp.Value["typeId"].ToObject<string>(); // typeId
- // { // 武器专属字段,不升级之前没有初始化 (不好,要改-wg)
- // if (temp.Value["isLocked"] != null)
- // {
- // itemtemp.isLocked = temp.Value["isLocked"].ToObject<int>();
- // }
- // if (temp.Value["isUsing"] != null)
- // {
- // itemtemp.WhoIsUsing = temp.Value["isUsing"].ToObject<int>();
- // }
- // if (temp.Value["level"] != null)
- // {
- // itemtemp.level = temp.Value["level"].ToObject<int>();
- // }
- // if (temp.Value["maxStar"] != null)
- // {
- // itemtemp.maxStar = temp.Value["maxStar"].ToObject<int>();
- // }
- // if (temp.Value["starLevel"] != null)
- // {
- // itemtemp.starLevel = temp.Value["starLevel"].ToObject<int>();
- // }
- // }
- // itemtemp.uid = temp.Name; // uid
- // collectItemDic.Add(itemtemp.uid, itemtemp); // 这里, 装备index从1开始,其他道具按照6位的编号作为索引,应该不至于冲突 --王刚 2019年11月14日10:31:19
- // // 另外一个存在uid的就是英雄了, 从10001开始,5位编码,整个10001到99999段都没有别人用,应该也不至于不够用或者冲突了
- // }
- //}
- #endregion
- }
- /// <summary>
- /// 根据typeID来查询物品的数据信息
- /// </summary>
- /// <param name="type">类型或者uid</param>
- /// <returns> </returns>
- internal ItemVo GetRealItemInfoByType(string type)
- {
- if (collectItemDic.ContainsKey(type))
- {
- return collectItemDic[type];
- }
- return null;
- }
- /// <summary>
- /// 按子类型查询物品
- /// </summary>
- /// <param name="_type">子类型为None时返回全部道具</param>
- /// <returns></returns>
- public List<ItemVo> GetItemList(EItemSubType _type = EItemSubType.None)
- {
- if (_type == EItemSubType.None) { // 限定种类
- return collectItemDic.Values.ToList();
- }
- return collectItemDic.Values.Where(item => item.nMo.subType == (int)_type).ToList();
- }
- /// <summary>
- /// 取所有武器
- /// </summary>
- /// <returns></returns>
- public List<UserEquipmentVo> GetWeaponList()
- {
- return UserProxy.Instance.player.collectEquip.equipments.Values.ToList();
- }
- /// <summary>
- /// 添加物品
- /// </summary>
- /// <param name="ItemId"></param>
- /// <param name="num"></param>
- public void AddItems(string ItemId, int num) {
- if (collectItemDic.ContainsKey(ItemId))
- {
- collectItemDic[ItemId].count += num;
- }
- else {
- collectElementDic.Add(ItemId, new ItemVo() { typeId = ItemId, count = num });
- }
- }
- /// <summary>
- /// 检测是否有足够的道具
- /// </summary>
- /// <param name="type"></param>
- /// <param name="num"></param>
- /// <returns></returns>
- public bool CheckHasEngoughItem(string type, int num)
- {
- if (collectItemDic.ContainsKey(type))
- {
- if (collectItemDic[type].count >= num)
- {
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// 返回指定物品的数量
- /// </summary>
- /// <param name="type">typeid</param>
- /// <returns></returns>
- public int GetItemCount(string type)
- {
- if (collectItemDic.ContainsKey(type))
- {
- return collectItemDic[type].count;
- }
- return 0;
- }
- /// <summary>
- /// 消耗背包中的道具
- /// </summary>
- /// <param name="itemid"></param>
- /// <param name="num"></param>
- public void ConsumePacketItem(string itemid, int num)
- {
- if (collectItemDic.ContainsKey(itemid))
- {
- int curNum = collectItemDic[itemid].count;
- collectItemDic[itemid].count = curNum - num;
- if (collectItemDic[itemid].count <= 0)
- {
- collectItemDic.Remove(itemid);
- }
- }
- }
- public List<ItemVo> GetStoreData(EItemSubType _type = EItemSubType.None)
- {
- if (_type == EItemSubType.None)
- { // 限定种类
- return collectItemDic.Values.ToList();
- }
- List<ItemVo> list = new List<ItemVo>();
- List<ItemVo> tempList = new List<ItemVo>();
- foreach (KeyValuePair<string, ItemVo> kv in this.collectItemDic)
- {
- if (kv.Value.nMo.subType != (int)_type)
- {
- continue;
- }
- if (kv.Value.count > 99999)
- {
- int n = kv.Value.count - 99999;
- kv.Value.count = 99999;
- list.Add(kv.Value);
- ItemVo vo = new ItemVo();
- vo.typeId = kv.Key;
- vo.count = n;
- list.Add(vo);
- }
- else
- {
- list.Add(kv.Value);
- }
- }
-
- return list;
- }
- #region
- /// <summary>
- /// 物品的相关数据发生变化
- /// 通知UI层,重新刷新显示的事件触发
- /// </summary>
- public System.EventHandler RefreshPacketUIDataViewEvent;
- public System.EventHandler RefreshPropertyUIDataViewEvent;
- public void RefreshUI(string even)
- {
- //---------------Event触发通知---------------------
- if (RefreshPacketUIDataViewEvent != null)
- {
- RefreshPacketUIDataViewEvent.Invoke(even, EventArgs.Empty);
- }
- }
- public void RefreshPropertyUI(string even = null)
- {
- //---------------Event触发通知---------------------
- if (RefreshPropertyUIDataViewEvent != null)
- {
- RefreshPropertyUIDataViewEvent.Invoke(even, EventArgs.Empty);
- }
- }
- #endregion
- }
|