AutoPickupBySpriteCommand.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. local AutoPickupBySpriteCommand = class('AutoPickupBySpriteCommand', framework.SimpleCommand)
  2. local proxyUtils = requireProxy("proxyUtils")
  3. local tSort = table.sort
  4. local mAbs = math.abs
  5. local function diffLen(x, y)
  6. return math.max(math.abs(x), math.abs(y))
  7. end
  8. function AutoPickupBySpriteCommand:ctor()
  9. end
  10. function AutoPickupBySpriteCommand:execute(notification)
  11. local player = global.gamePlayerController:GetMainPlayer()
  12. if not player then
  13. return nil
  14. end
  15. local PickupSpriteProxy = global.Facade:retrieveProxy(global.ProxyTable.PickupSpriteProxy)
  16. if not PickupSpriteProxy:getPickupSpriteState() then
  17. return
  18. end
  19. local facade = global.Facade
  20. local autoProxy = facade:retrieveProxy(global.ProxyTable.Auto)
  21. -- local target = nil
  22. local pMapX = player:GetMapX()
  23. local pMapY = player:GetMapY()
  24. -- local Range =
  25. -- only tips
  26. local bagProxy = facade:retrieveProxy(global.ProxyTable.Bag)
  27. local bagfull = bagProxy:isToBeFull()
  28. local range = PickupSpriteProxy:getPickupRange()
  29. -- find target
  30. local mode = PickupSpriteProxy:getPickSpriteMode()
  31. local spriteID = PickupSpriteProxy:getPickSpriteID()
  32. local itemVec = nil
  33. local nItemVec = 0
  34. if spriteID and (mode == 1 or mode == 3) then--小精灵附近
  35. itemVec, nItemVec = self:monsterMode(range, true, spriteID)
  36. else
  37. itemVec, nItemVec = global.dropItemManager:FindDropItemInCurrViewField(range, true)
  38. end
  39. local mainPlayerID = player:GetID()
  40. local pickLen = nil
  41. local currPickLen = nil
  42. local targetCount = 0
  43. local nums = 0
  44. local strs = {}
  45. local dropItemMakeindex
  46. for i = 1, nItemVec do
  47. local dropItem = itemVec[i]
  48. if dropItem and self:isPickable(dropItem, mainPlayerID, bagfull) then
  49. dropItemMakeindex = dropItem:GetMakeIndex()
  50. if not dropItemMakeindex or dropItemMakeindex == 0 then --金币没有唯一id
  51. dropItemMakeindex = dropItem:GetName()
  52. end
  53. local temps = string.format("%s,%s,%s", dropItemMakeindex, dropItem:GetMapX(), dropItem:GetMapY())
  54. table.insert(strs, temps)
  55. nums = nums + 1
  56. if nums == 35 then
  57. break
  58. end
  59. end
  60. end
  61. if #strs > 0 then
  62. PickupSpriteProxy:ReqPickUpSomething(table.concat(strs, ";"))
  63. end
  64. end
  65. function AutoPickupBySpriteCommand:monsterMode(range, sortNearToFarFromPlayer, actorID)
  66. local sprite = global.actorManager:GetActor(actorID)
  67. if not sprite then
  68. return {}, 0
  69. end
  70. if nil == sortNearToFarFromPlayer then
  71. sortNearToFarFromPlayer = false
  72. end
  73. local function inRange(a, range)
  74. if -1 == range then return true end
  75. local p = sprite
  76. local mX = p:GetMapX()
  77. local mY = p:GetMapY()
  78. local dX = a:GetMapX() - mX
  79. local dY = a:GetMapY() - mY
  80. local diffA = (mAbs(dX) > mAbs(dY)) and mAbs(dX) or mAbs(dY)
  81. return diffA <= range
  82. end
  83. local allitem, ncount = global.dropItemManager:FindDropItemInCurrViewFieldAll()
  84. local ownerID = global.MMO.ACTOR_ID_INVALID
  85. local recvVec = {}
  86. local num = 0
  87. for i = 1, ncount do
  88. local m = allitem[i]
  89. if m and (global.MMO.ACTOR_ID_INVALID == ownerID or m:CheckOwnerID(ownerID)) and inRange(m, range) then
  90. num = num + 1
  91. recvVec[num] = m
  92. end
  93. end
  94. -- Need to sort ?
  95. if sortNearToFarFromPlayer and num > 1 then
  96. local p = sprite
  97. local mX = p:GetMapX()
  98. local mY = p:GetMapY()
  99. local function distCmp(a, b)
  100. local dX = a:GetMapX() - mX
  101. local dY = a:GetMapY() - mY
  102. local diffA = (mAbs(dX) > mAbs(dY)) and mAbs(dX) or mAbs(dY)
  103. dX = b:GetMapX() - mX
  104. dY = b:GetMapY() - mY
  105. local diffB = (mAbs(dX) > mAbs(dY)) and mAbs(dX) or mAbs(dY)
  106. return diffA < diffB
  107. end
  108. tSort(recvVec, distCmp)
  109. end
  110. return recvVec, num
  111. end
  112. function AutoPickupBySpriteCommand:isPickable(dropItem, mainPlayerID, bagfull)
  113. if not dropItem then
  114. return false
  115. end
  116. if dropItem:IsIngored() then
  117. return false
  118. end
  119. if bagfull and dropItem:GetTypeIndex() ~= 0 then -- 背包满可以捡金币
  120. return false
  121. end
  122. -- check pick state
  123. if dropItem:GetPickState() >= 1 or dropItem:IsPickTimeout() then
  124. return false
  125. end
  126. -- check owner
  127. if not proxyUtils:AutoPickItemEnable(dropItem, mainPlayerID, true) then
  128. return false
  129. end
  130. return true
  131. end
  132. return AutoPickupBySpriteCommand