123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- using FL.Data.Items;
- using System;
- using System.Collections.Generic;
- using XGame;
- using XGame.Framework.Data;
- namespace FL.Data
- {
- /// <summary>
- /// 背包格子数据
- /// </summary>
- public class BagItemData
- {
- public long id;
- public int tableId;
- public long num;
- public int girdIndex; // 背包格子索引
- public EquipItem equipItemData;
- }
- public class ItemData : DataSingleton<ItemData>, IDisposable
- {
- private Dictionary<int, BagItemData> _bagItemDataDic = new Dictionary<int, BagItemData>(); // 背包中的道具数据
- /// <summary>
- /// 添加一个物品道具数据
- /// </summary>
- /// <param name="tableId"></param>
- /// <param name="count"></param>
- public void AddItemData(int tableId, long count, bool bAdd = false)
- {
- // 如果背包中已拥有该道具直接改变物品数量
- if (OnChangeItemNum(tableId, count, bAdd))
- {
- return;
- }
- int girdIndex = GetEmptyGridIndex();
- AddBagItemData(girdIndex, new BagItemData()
- {
- id = UIDDefine.New(),
- tableId = tableId,
- num = count,
- girdIndex = girdIndex,
- });
- }
- /// <summary>
- /// 模拟数据测试用
- /// </summary>
- /// <returns></returns>
- public int GetEmptyGridIndex()
- {
- return _bagItemDataDic.Count + 1;
- }
- /// <summary>
- /// 添加背包格子数据
- /// </summary>
- /// <param name="index"></param>
- /// <param name="data"></param>
- public void AddBagItemData(int index, BagItemData data)
- {
- _bagItemDataDic.Add(index, data);
- }
- /// <summary>
- /// 拥有的道具数量
- /// </summary>
- /// <param name="tableId"></param>
- /// <returns></returns>
- public long GetItemNum(int tableId)
- {
- long count = 0;
- foreach (var item in _bagItemDataDic)
- {
- if (item.Value?.tableId == tableId)
- {
- count += item.Value.num;
- }
- }
- return count;
- }
-
- /// <summary>
- /// 减少物品数量
- /// </summary>
- /// <param name="tableId"></param>
- /// <param name="count"></param>
- public void ReduceItemNum(int tableId, long count)
- {
- for (int i = 1; i <= _bagItemDataDic.Count; i++)
- {
- if (_bagItemDataDic[i]?.tableId == tableId)
- {
- if (_bagItemDataDic[i].num > count)
- {
- _bagItemDataDic[i].num -= count;
- return;
- }
- else
- {
- count -= _bagItemDataDic[i].num;
- _bagItemDataDic[i] = null;
- }
- }
- }
- }
- /// <summary>
- /// 道具数量变化
- /// </summary>
- /// <param name="tableId"></param>
- /// <param name="count"></param>
- /// <returns></returns>
- public bool OnChangeItemNum(int tableId, long count, bool bAdd)
- {
- for (int i = 1; i <= _bagItemDataDic.Count; i++)
- {
- if (_bagItemDataDic[i]?.tableId == tableId)
- {
- _bagItemDataDic[i].num = bAdd ? (_bagItemDataDic[i].num + count) : count;
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// 获取礼包|红包类型道具(道具页签),常规道具类型(材料页签)用来在背包界面显示
- /// </summary>
- /// <returns></returns>
- public Dictionary<int, BagItemData> GetBackpackData()
- {
- return _bagItemDataDic;
- }
- void IDisposable.Dispose()
- {
- if (_bagItemDataDic?.Count > 0)
- {
- foreach (var item in _bagItemDataDic)
- {
- if (item.Value?.equipItemData != null)
- {
- item.Value.equipItemData.Dispose();
- }
- }
- _bagItemDataDic.Clear();
- }
- }
- }
- }
|