123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- 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<BagController>
- {
- /// <summary>
- /// 玩家的所有道具(包含货币)
- /// </summary>
- private List<ItemInfo> m_bagList = new List<ItemInfo>();
- // private List<ItemInfo> m_bagEqList = new List<ItemInfo>();
- /// <summary>
- /// 玩家的装备(按职业分类)
- /// </summary>
- // private Dictionary<int, List<ItemInfo>> m_EqDic = new Dictionary<int, List<ItemInfo>>();
- /// <summary>
- /// 外部访问用
- /// </summary>
- // public Dictionary<int, List<ItemInfo>> EqDic => m_EqDic;
- /// <summary>
- /// 外部访问用
- /// </summary>
- public List<ItemInfo> BagList => m_bagList;
- public void Init()
- {
- foreach (var itemData in AccountFileInfo.Instance.playerData.ItemListData)
- {
- ItemInfo itemInfo = new ItemInfo(itemData);
- m_bagList.Add(itemInfo);
- PlayerManager.Instance.eqController.Init(itemInfo);
- // AddEqToDic(itemInfo);
- }
- }
- #region 快速添加和扣除常用道具
- /// <summary>
- /// 加金币
- /// </summary>
- /// <param name="count"></param>
- public void AddCoin(long count)
- {
- AddItem(GlobalParam.Item_Coin_ID, count);
- }
- /// <summary>
- /// 扣金币
- /// </summary>
- /// <param name="count"></param>
- public bool DeductCoin(long count)
- {
- return DeductItem(GlobalParam.Item_Coin_ID, count);
- }
- /// <summary>
- /// 加钻石
- /// </summary>
- /// <param name="count"></param>
- public void AddDiamond(long count)
- {
- AddItem(GlobalParam.Item_Diamond_ID, count);
- }
- /// <summary>
- /// 扣钻石
- /// </summary>
- /// <param name="count"></param>
- public bool DeductDiamond(long count)
- {
- return DeductItem(GlobalParam.Item_Diamond_ID, count);
- }
- /// <summary>
- /// 加英雄经验
- /// </summary>
- /// <param name="count"></param>
- public void AddHeroExp(long count)
- {
- AddItem(GlobalParam.Item_HeroExp_ID, count);
- }
- /// <summary>
- /// 扣英雄经验
- /// </summary>
- /// <param name="count"></param>
- public bool DuctHeroExp(long count)
- {
- return DeductItem(GlobalParam.Item_HeroExp_ID, count);
- }
- /// <summary>
- /// 道具是否足够
- /// </summary>
- /// <param name="itemId"></param>
- /// <param name="count"></param>
- /// <returns></returns>
- public bool IsEnough(ItemInfo item, long count)
- {
- if (item.config.ID <= 0)
- {
- return false;
- }
- if (item.count >= count)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// 道具是否足够
- /// </summary>
- /// <param name="itemId"></param>
- /// <param name="count"></param>
- /// <returns></returns>
- 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);
- }
- public void AddItem(List<ItemInfo> itemInfos)
- {
- for (int i = 0; i < itemInfos.Count; i++)
- {
- ItemInfo itemInfo = itemInfos[i];
- LogTool.Log("获得了道具"+itemInfo.itemID);
- AddItem(itemInfo, itemInfo.count.Value);
- }
- }
- /// <summary>
- /// 添加道具,最后都走这里
- /// </summary>
- /// <param name="item"></param>
- /// <param name="count"></param>
- /// <returns></returns>
- 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<ItemInfo>());
- // m_EqDic[pro].Add(eqItemInfo);
- // }
- // }
- /// <summary>
- /// 添加装备道具
- /// </summary>
- /// <param name="item"></param>
- public void AddEquipmentItem(ItemInfo item)
- {
- long count = item.count.Value;
- ItemInfo eqItemInfo = GetItemInfoWithGuid(item);
- // AddEqToDic(eqItemInfo);
- PlayerManager.Instance.eqController.AddEquipment(eqItemInfo);
- // m_bagEqList.Add(eqItemInfo);
- AddItem(eqItemInfo, count);
- }
- public ItemInfo GetItemInfoWithGuid(ItemInfo itemInfo)
- {
- for (int i = 0; i < m_bagList.Count; i++)
- {
- ItemInfo item = m_bagList[i];
- if (!string.IsNullOrEmpty(item.guid) && item.guid == itemInfo.guid)
- {
- return item;
- }
- }
- if (!string.IsNullOrEmpty(itemInfo.guid))
- {
- //这里要设成0是因为,装备已经掉出来了,有了数量,第一次进背包时,先把数量去掉(否则会掉一个得2个)
- //接下来会加回去,看后续代码。
- itemInfo.count.Value = 0;
- m_bagList.Add(itemInfo);
- }
- return itemInfo;
- }
- /// <summary>
- /// 扣除道具,最后都这里
- /// </summary>
- /// <param name="item"></param>
- /// <param name="count"></param>
- /// <returns></returns>
- 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 ItemInfo GetItemInfo(int itemId)
- {
- for (int i = 0; i < m_bagList.Count; i++)
- {
- ItemInfo item = m_bagList[i];
- if (item.itemID == itemId)
- {
- return item;
- }
- }
- ItemInfo newItem = new ItemInfo(itemId);
- if (newItem.config.ID > 0)
- {
- m_bagList.Add(newItem);
- }
- return newItem;
- }
- 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);
- }
- }
- }
|