BagCellUtil_Tips.lua 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ---@class BagCellUtil @注释
  2. BagCellUtil = class()
  3. local this = BagCellUtil
  4. function this.SortItemList(itemList, maxHor, maxVet)
  5. local sortItem = {}
  6. local useCellDict = {}
  7. -- 缺省时使用背包的配置
  8. maxHor = maxHor or RoleManager.meData.bagInfo.bagHorSize
  9. maxVet = maxVet or RoleManager.meData.bagInfo.bagVetSize
  10. -- itemList要变了 深拷贝一下
  11. local copyItemList = itemList
  12. -- 开始放物品
  13. for _, item in pairs(copyItemList) do
  14. -- 把物品放到每个格子上
  15. local isPut = false
  16. for hor=1, maxHor do
  17. -- 放置完了, 不用在遍历
  18. if isPut == true then
  19. break
  20. end
  21. for vet=1, maxVet do
  22. if BagInfo.GetCanPutItemInBagWithUseCellDict(hor, vet, item.cfgId, useCellDict, maxHor, maxVet) then
  23. local putKey = BagInfo.GetIntWithHorAndVet(hor, vet)
  24. sortItem[putKey] = item
  25. isPut = true
  26. -- 更新物品占用表
  27. local holdVet, holdHor = BagInfo.GetBagSizeByCfgId(item.cfgId)
  28. for nowHor=hor, hor+holdHor-1 do
  29. for nowVet=vet, vet+holdVet-1 do
  30. useCellDict[BagInfo.GetIntWithHorAndVet(nowHor, nowVet)] = true
  31. end
  32. end
  33. break
  34. end
  35. end
  36. end
  37. end
  38. return sortItem
  39. end
  40. -- 给定包裹放置字典, 判断是否能将item放置在srcHor行,srcVet的位置
  41. -- maxHor, maxVet 缺省时使用背包的配置
  42. function this.CanPutItemInBag(srcHor, srcVet, item, putBagDict, maxHor, maxVet)
  43. local useCellDict = this.GetUseCellDict(putBagDict)
  44. return this.GetCanPutItemInBagWithUseCellDict(srcHor, srcVet, item, useCellDict, maxHor, maxVet)
  45. end
  46. ---@private
  47. --- 获得物品占用表
  48. ---@param putBagDict table<number,CommonProtos.Item>
  49. function this.GetUseCellDict(putBagDict)
  50. local UseCellDict = {}
  51. for putKey, item in pairs(putBagDict) do
  52. local hor, vet = BagInfo.GetHorAndVetWithInt(putKey)
  53. -- 物品占的行列
  54. local holdHor, holdVet = BagInfo.GetBagSizeByCfgId(item.cfgId)
  55. for nowHor=hor, hor+holdHor-1 do
  56. if UseCellDict[nowHor] == nil then
  57. UseCellDict[nowHor] = {}
  58. end
  59. for nowVet=vet, vet+holdVet-1 do
  60. UseCellDict[nowHor][nowVet] = true
  61. end
  62. end
  63. end
  64. return UseCellDict
  65. end