using FL.Data.Items; using System; using System.Collections.Generic; using XGame; using XGame.Framework.Data; namespace FL.Data { /// /// 背包格子数据 /// public class BagItemData { public long id; public int tableId; public long num; public int girdIndex; // 背包格子索引 public EquipItem equipItemData; } public class ItemData : DataSingleton, IDisposable { private Dictionary _bagItemDataDic = new Dictionary(); // 背包中的道具数据 /// /// 添加一个物品道具数据 /// /// /// 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, }); } /// /// 模拟数据测试用 /// /// public int GetEmptyGridIndex() { return _bagItemDataDic.Count + 1; } /// /// 添加背包格子数据 /// /// /// public void AddBagItemData(int index, BagItemData data) { _bagItemDataDic.Add(index, data); } /// /// 拥有的道具数量 /// /// /// public long GetItemNum(int tableId) { long count = 0; foreach (var item in _bagItemDataDic) { if (item.Value?.tableId == tableId) { count += item.Value.num; } } return count; } /// /// 减少物品数量 /// /// /// 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; } } } } /// /// 道具数量变化 /// /// /// /// 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; } /// /// 获取礼包|红包类型道具(道具页签),常规道具类型(材料页签)用来在背包界面显示 /// /// public Dictionary 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(); } } } }