using System.Collections.Generic; using Common.Utility.CombatEvent; using Core.Utility; using Fort23.Core; using GameLogic.Equipment; using UnityEngine; using Utility; namespace GameLogic.Bag { public class BagController : Singleton { /// /// 玩家的所有道具(包含货币) /// // private List m_bagList = new List(); private Dictionary m_allBagDic = new Dictionary(); /// /// 外部访问用 /// // public List BagList => m_bagList; public void Init() { foreach (var itemData in AccountFileInfo.Instance.playerData.ItemListData) { ItemInfo itemInfo = new ItemInfo(itemData); // m_bagList.Add(itemInfo); m_allBagDic.Add(itemInfo.guid, itemInfo); PlayerManager.Instance.eqController.Init(itemInfo); } } #region 快速添加和扣除常用道具 /// /// 加金币 /// /// public void AddCoin(long count) { AddItem(GlobalParam.Item_Coin_ID, count); } /// /// 扣金币 /// /// public bool DeductCoin(long count) { return DeductItem(GlobalParam.Item_Coin_ID, count); } /// /// 加钻石 /// /// public void AddDiamond(long count) { AddItem(GlobalParam.Item_Diamond_ID, count); } /// /// 扣钻石 /// /// public bool DeductDiamond(long count) { return DeductItem(GlobalParam.Item_Diamond_ID, count); } /// /// 加英雄经验 /// /// public void AddHeroExp(long count) { AddItem(GlobalParam.Item_HeroExp_ID, count); } /// /// 扣英雄经验 /// /// public bool DuctHeroExp(long count) { return DeductItem(GlobalParam.Item_HeroExp_ID, count); } /// /// 道具是否足够 /// /// /// /// public bool IsEnough(ItemInfo item, long count) { if (item.config.ID <= 0) { return false; } if (item.count >= count) { return true; } else { return false; } } /// /// 道具是否足够 /// /// /// /// public bool IsEnough(int itemId, long count) { ItemInfo item = GetItemInfo(itemId); return IsEnough(item, count); } #endregion public bool AddItem(int itemId, long count) { ItemInfo item = GetItemInfo(itemId); return AddItem(item, count); } /// /// 添加道具,最后都走这里 /// /// /// /// private bool AddItem(ItemInfo item, long count) { if (item.config.ID <= 0 && string.IsNullOrEmpty(item.guid)) { return false; } item.count += count; EventManager.Instance.Dispatch(CustomEventType.ItemUpdate, new ItemUpdateData() { ItemInfo = item }); AccountFileInfo.Instance.SaveItemData(item); return true; } // private void AddEqToDic(ItemInfo eqItemInfo) // { // if (eqItemInfo.eqInfo == null) // { // return; // } // // int pro = eqItemInfo.eqInfo.basicEquipConfig.profession; // // if (m_EqDic.ContainsKey(pro)) // { // m_EqDic[pro].Add(eqItemInfo); // } // else // { // m_EqDic.Add(pro, new List()); // m_EqDic[pro].Add(eqItemInfo); // } // } /// /// 添加装备道具 /// /// public void AddEquipmentItem(ItemInfo item) { //掉落的数量 long count = item.count.Value; // AddEqToDic(eqItemInfo); ItemInfo eqItemInfo = GetItemInfo(item); PlayerManager.Instance.eqController.AddEquipment(eqItemInfo); // m_bagEqList.Add(eqItemInfo); AddItem(eqItemInfo, count); } /// /// 一般是掉了(随机)一个东西(ItemInfo),然后来背包找,是否之前掉过一样的 /// 一般用于有guid的item /// 比如掉了一个装备,装备有guid(组装的) /// 后续有其他类似装备这种,动态组织出来的有guid的物品,也可以走这里。宝石,词条等。 /// /// /// public ItemInfo GetItemInfo(ItemInfo itemInfo) { string guidStr = string.IsNullOrEmpty(itemInfo.guid) ? itemInfo.itemID.ToString() : itemInfo.guid; if (m_allBagDic.TryGetValue(guidStr, out ItemInfo item)) { return item; } //这里要设成0是因为,随机掉落的东西,第一次进背包时,先把数量去掉(否则会掉会多加1个) //该方法结束后,有统一加数量的代码,看后续代码。 itemInfo.count.Value = 0l; m_allBagDic.Add(itemInfo.guid, itemInfo); return itemInfo; } /// /// 获取道具信息 /// 不传guid,默认itemId就是guid /// /// /// /// public ItemInfo GetItemInfo(int itemId, string guid = "") { // for (int i = 0; i < m_bagList.Count; i++) // { // ItemInfo item = m_bagList[i]; // if (item.itemID == itemId) // { // return item; // } // } string guidStr = string.IsNullOrEmpty(guid) ? itemId.ToString() : guid; if (m_allBagDic.TryGetValue(guidStr, out ItemInfo item)) { return item; } ItemInfo newItem = new ItemInfo(itemId); if (newItem.config.ID > 0) { // m_bagList.Add(newItem); m_allBagDic.Add(newItem.guid, newItem); } return newItem; } /// /// 扣除道具,最后都这里 /// /// /// /// public bool DeductItem(ItemInfo item, long count) { if (item.config.ID <= 0) { return false; } if (item.count >= count) { item.count -= count; } else { return false; } EventManager.Instance.Dispatch(CustomEventType.ItemUpdate, new ItemUpdateData() { ItemInfo = item }); AccountFileInfo.Instance.SaveItemData(item); return true; } public bool DeductItem(int itemId, long count) { ItemInfo item = GetItemInfo(itemId); return DeductItem(item, count); } public void DropHeroExp(Vector3 startPos_WorldPos, int showCount, int itemCount) { CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create(); combatItemShowEventData.count = showCount; combatItemShowEventData.startPos_WorldPos = startPos_WorldPos; combatItemShowEventData.showName = "icon_res_upgrade_1"; combatItemShowEventData.showType = CombatItemShowEventData.ShowType.HeroExp; combatItemShowEventData.addValue = itemCount; EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData); } public void DropLevelExp(Vector3 startPos_WorldPos, int showCount, int itemCount) { CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create(); combatItemShowEventData.count = showCount; combatItemShowEventData.startPos_WorldPos = startPos_WorldPos; combatItemShowEventData.showName = "icon_Energy"; combatItemShowEventData.showType = CombatItemShowEventData.ShowType.LevelExp; combatItemShowEventData.addValue = itemCount; EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData); } public void DropMonsterGold(Vector3 startPos_WorldPos, int showCount, int itemCount) { CombatItemShowEventData combatItemShowEventData = CombatItemShowEventData.Create(); combatItemShowEventData.count = showCount; combatItemShowEventData.startPos_WorldPos = startPos_WorldPos; combatItemShowEventData.showName = "icon_Coin"; combatItemShowEventData.showType = CombatItemShowEventData.ShowType.MonsterGold; combatItemShowEventData.addValue = itemCount; EventManager.Instance.Dispatch(CustomEventType.Combat_ItemShow, combatItemShowEventData); } } }