using System.Collections.Generic; using Common.Utility.CombatEvent; using Core.Utility; using Fort23.Core; using Fort23.UTool; 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 Dictionary allBagDic => m_allBagDic; 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, string guid = "") { string guidStr = string.IsNullOrEmpty(guid) ? itemId.ToString() : guid; ItemInfo item = GetItemInfo(itemId, guidStr); return AddItemCount(item, count); } /// /// 添加道具,装备和普通道具都可以 /// /// public void AddItem(List itemInfos) { for (int i = 0; i < itemInfos.Count; i++) { ItemInfo item = itemInfos[i]; LogTool.Log("获得了道具"+item.itemID); AddItem(item); // //掉落的数量 // long count = itemInfo.count.Value; // ItemInfo item = GetItemInfo(itemInfo); // // if (item.IsEquipItem()) // { // PlayerManager.Instance.eqController.AddEquipment(item); // } // // AddItemCount(item, count); } } /// /// 添加道具数量,最后都走这里 /// /// /// /// private bool AddItemCount(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 AddItem(ItemInfo item) { //掉落的数量 long count = item.count.Value; ItemInfo itemInfo = GetItemInfo(item); if (itemInfo.IsEquipItem()) { PlayerManager.Instance.eqController.AddEquipment(itemInfo); } AddItemCount(itemInfo, count); } /// /// 重新加载一次所有装备 /// 一般用于,分解装备之后 /// public void ReInitAllEqItem() { PlayerManager.Instance.eqController.allZyEqDic.Clear(); foreach (KeyValuePair keyValuePair in m_allBagDic) { PlayerManager.Instance.eqController.AddEquipment(keyValuePair.Value); } } public void RemoveItem(ItemInfo itemInfo, bool saveNow, bool zeroDel) { itemInfo.count.Value = 0; if (zeroDel) { m_allBagDic.Remove(itemInfo.guid); } AccountFileInfo.Instance.SaveItemData(itemInfo, saveNow); } public void ModifyItem(ItemInfo itemInfo, bool saveNow) { itemInfo.count.Value -= itemInfo.count.Value; AccountFileInfo.Instance.SaveItemData(itemInfo, saveNow); } /// /// 获取道具信息 /// 不传guid,默认itemId就是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; // } // // m_allBagDic.Add(itemInfo.guid, itemInfo); // // return itemInfo; // } /// /// 一般是掉了(随机)一个东西(ItemInfo),然后来背包找,是否之前掉过一样的 /// 之前有,就把有的返回回去,加数量 /// 之前无,就返回这个新东西 /// /// 重要:加入背包管理都是通过这个方法(后续看是否优) /// /// /// 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 = "") { 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); } } }