ItemData.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using FL.Data.Items;
  2. using System;
  3. using System.Collections.Generic;
  4. using XGame;
  5. using XGame.Framework.Data;
  6. namespace FL.Data
  7. {
  8. /// <summary>
  9. /// 背包格子数据
  10. /// </summary>
  11. public class BagItemData
  12. {
  13. public long id;
  14. public int tableId;
  15. public long num;
  16. public int girdIndex; // 背包格子索引
  17. public EquipItem equipItemData;
  18. }
  19. public class ItemData : DataSingleton<ItemData>, IDisposable
  20. {
  21. private Dictionary<int, BagItemData> _bagItemDataDic = new Dictionary<int, BagItemData>(); // 背包中的道具数据
  22. /// <summary>
  23. /// 添加一个物品道具数据
  24. /// </summary>
  25. /// <param name="tableId"></param>
  26. /// <param name="count"></param>
  27. public void AddItemData(int tableId, long count, bool bAdd = false)
  28. {
  29. // 如果背包中已拥有该道具直接改变物品数量
  30. if (OnChangeItemNum(tableId, count, bAdd))
  31. {
  32. return;
  33. }
  34. int girdIndex = GetEmptyGridIndex();
  35. AddBagItemData(girdIndex, new BagItemData()
  36. {
  37. id = UIDDefine.New(),
  38. tableId = tableId,
  39. num = count,
  40. girdIndex = girdIndex,
  41. });
  42. }
  43. /// <summary>
  44. /// 模拟数据测试用
  45. /// </summary>
  46. /// <returns></returns>
  47. public int GetEmptyGridIndex()
  48. {
  49. return _bagItemDataDic.Count + 1;
  50. }
  51. /// <summary>
  52. /// 添加背包格子数据
  53. /// </summary>
  54. /// <param name="index"></param>
  55. /// <param name="data"></param>
  56. public void AddBagItemData(int index, BagItemData data)
  57. {
  58. _bagItemDataDic.Add(index, data);
  59. }
  60. /// <summary>
  61. /// 拥有的道具数量
  62. /// </summary>
  63. /// <param name="tableId"></param>
  64. /// <returns></returns>
  65. public long GetItemNum(int tableId)
  66. {
  67. long count = 0;
  68. foreach (var item in _bagItemDataDic)
  69. {
  70. if (item.Value?.tableId == tableId)
  71. {
  72. count += item.Value.num;
  73. }
  74. }
  75. return count;
  76. }
  77. /// <summary>
  78. /// 减少物品数量
  79. /// </summary>
  80. /// <param name="tableId"></param>
  81. /// <param name="count"></param>
  82. public void ReduceItemNum(int tableId, long count)
  83. {
  84. for (int i = 1; i <= _bagItemDataDic.Count; i++)
  85. {
  86. if (_bagItemDataDic[i]?.tableId == tableId)
  87. {
  88. if (_bagItemDataDic[i].num > count)
  89. {
  90. _bagItemDataDic[i].num -= count;
  91. return;
  92. }
  93. else
  94. {
  95. count -= _bagItemDataDic[i].num;
  96. _bagItemDataDic[i] = null;
  97. }
  98. }
  99. }
  100. }
  101. /// <summary>
  102. /// 道具数量变化
  103. /// </summary>
  104. /// <param name="tableId"></param>
  105. /// <param name="count"></param>
  106. /// <returns></returns>
  107. public bool OnChangeItemNum(int tableId, long count, bool bAdd)
  108. {
  109. for (int i = 1; i <= _bagItemDataDic.Count; i++)
  110. {
  111. if (_bagItemDataDic[i]?.tableId == tableId)
  112. {
  113. _bagItemDataDic[i].num = bAdd ? (_bagItemDataDic[i].num + count) : count;
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
  119. void IDisposable.Dispose()
  120. {
  121. if (_bagItemDataDic?.Count > 0)
  122. {
  123. foreach (var item in _bagItemDataDic)
  124. {
  125. if (item.Value?.equipItemData != null)
  126. {
  127. item.Value.equipItemData.Dispose();
  128. }
  129. }
  130. _bagItemDataDic.Clear();
  131. }
  132. }
  133. }
  134. }