ItemData.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using XGame.Framework.Data;
  4. namespace FL.Data
  5. {
  6. /// <summary>
  7. /// 背包格子数据
  8. /// </summary>
  9. public class BagItemData
  10. {
  11. public int tableId;
  12. public int num;
  13. }
  14. public class ItemData : DataSingleton<ItemData>, IDisposable
  15. {
  16. private Dictionary<int, BagItemData> _bagItemDataDic = new Dictionary<int, BagItemData>(); // 背包中的道具数据
  17. /// <summary>
  18. /// 添加一个物品道具数据
  19. /// </summary>
  20. /// <param name="tableId"></param>
  21. /// <param name="count"></param>
  22. public void AddItemData(int tableId, int count)
  23. {
  24. if (_bagItemDataDic.ContainsKey(tableId))
  25. {
  26. _bagItemDataDic[tableId].num = count;
  27. if (_bagItemDataDic[tableId].num == 0)
  28. {
  29. // 移除
  30. _bagItemDataDic.Remove(tableId);
  31. }
  32. return;
  33. }
  34. _bagItemDataDic.Add(tableId, new BagItemData()
  35. {
  36. tableId = tableId,
  37. num = count,
  38. });
  39. }
  40. /// <summary>
  41. /// 拥有的道具数量
  42. /// </summary>
  43. /// <param name="tableId"></param>
  44. /// <returns></returns>
  45. public int GetItemNum(int tableId)
  46. {
  47. return _bagItemDataDic.ContainsKey(tableId) ? _bagItemDataDic[tableId].num : 0;
  48. }
  49. /// <summary>
  50. /// 获取礼包|红包类型道具(道具页签),常规道具类型(材料页签)用来在背包界面显示
  51. /// </summary>
  52. /// <returns></returns>
  53. public Dictionary<int, BagItemData> GetBackpackData()
  54. {
  55. return _bagItemDataDic;
  56. }
  57. void IDisposable.Dispose()
  58. {
  59. if (_bagItemDataDic?.Count > 0)
  60. {
  61. _bagItemDataDic.Clear();
  62. }
  63. }
  64. }
  65. }